Add copy buttons

This commit is contained in:
Laura Hausmann 2023-04-07 21:02:14 +02:00
parent b70ad17032
commit 65ab7f2941
Signed by: zotan
GPG key ID: D044E84C5BE01605
4 changed files with 62 additions and 14 deletions

View file

@ -18,14 +18,20 @@ else {
</h2>
<a class="action-muted">[..]</a>
<a href="?@IndexModel.Encode(Model.PathOneup)" id="back" class="entry-muted cfont"> Go back</a>
@if (Model.LogConditionsMet) {
<br/>
<a class="action-muted">[--]</a>
<span id="log" onclick="get('/log?artist=@Model.LogArtist&album=@Model.LogAlbum&url=@Model.LogUrl')" class="entry-muted cfont" style="cursor: pointer"> Log playback</span>
}
<br/>
<a class="action-muted">[--]</a>
<a href="/playlist/@(Request.QueryString).m3u" class="entry-muted cfont"> Download playlist</a>
@if (Model.LogConditionsMet) {
<br/>
<a class="action-muted">[--]</a>
<span id="log" onclick="log_playback('/log?artist=@Model.LogArtist&album=@Model.LogAlbum&url=@Model.LogUrl')" class="entry-muted cfont" style="cursor: pointer">Log playback</span>
<span class="action-muted">/</span>
<span id="copy_hash_np" onclick="copy_hash_np()" class="entry-muted cfont" style="cursor: pointer">Copy #np</span>
<span class="action-muted">/</span>
<span id="copy_journal_full" onclick="copy_journal_full()" class="entry-muted cfont" style="cursor: pointer">Copy journal (full)</span>
<span class="action-muted">/</span>
<span id="copy_journal_album" onclick="copy_journal_album()" class="entry-muted cfont" style="cursor: pointer">Copy journal (album only)</span>
}
<br/>
<br/>
@foreach (var dir in Model.Dirs) {
@ -44,7 +50,7 @@ else {
@if (System.IO.File.Exists(lrcpath)) {
<a class="action" href="?@Html.Raw(IndexModel.Encode(Model.Path + "/" + lrcfile))" target="_blank">[LRC]</a>
}
else if (Directory.GetFiles(@Model.Fullpath, "*.lrc").Length != 0) {
else if (Directory.GetFiles(Model.Fullpath, "*.lrc").Length != 0) {
<a class="action">[---]</a>
}
<a class="file" href="@Html.Raw(IndexModel.Encode(Model.Fullpath + "/" + file))"> @file</a>
@ -53,11 +59,45 @@ else {
}
<script>
function get(url) {
function log_playback(url) {
fetch(url).then(function(response) {
return response.text();
}).then(function(text) {
document.getElementById('log').innerText = ' Log playback (' + text + ')';
document.getElementById('log').innerText = 'Log playback (' + text + ')';
});
}
function reset_copy_labels() {
document.getElementById('copy_hash_np').innerText = 'Copy #np';
document.getElementById('copy_journal_full').innerText = 'Copy journal (full)';
document.getElementById('copy_journal_album').innerText = 'Copy journal (album only)';
}
function copy_hash_np() {
copyToClipboard('[#np](@Log.NowPlayingUrl) @Model.CopyArtist - @Model.CopyAlbum');
reset_copy_labels();
document.getElementById('copy_hash_np').innerText = 'Copy #np (Copied!)';
}
function copy_journal_full() {
copyToClipboard('[@Model.CopyArtist](' + getUrl('@Model.LogArtist') + ') - [@Model.CopyAlbum](' + getUrl('@Model.LogArtist', '@Model.LogAlbum') + ')')
reset_copy_labels();
document.getElementById('copy_journal_full').innerText = 'Copy journal (full) (Copied!)';
}
function copy_journal_album() {
copyToClipboard(', [@Model.CopyAlbum](' + getUrl('@Model.LogArtist', '@Model.LogAlbum') + ')')
reset_copy_labels();
document.getElementById('copy_journal_album').innerText = 'Copy journal (album only) (Copied!)';
}
function getUrl(artist, album) {
let url = location.protocol + '//' + location.host + '/?/' + artist;
if (album) {
url += '/' + album;
}
return url;
}
</script>

View file

@ -20,6 +20,8 @@ namespace webmusic.Pages {
public string LogArtist;
public string LogAlbum;
public string LogUrl;
public string CopyArtist;
public string CopyAlbum;
public void OnGet() {
if (Request.QueryString.HasValue)
@ -50,8 +52,10 @@ namespace webmusic.Pages {
if (Log.StatisticsEnabled && Request.Headers["Remote-User"].Equals(Log.StatisticsUser) && Files.Any()) {
var pathparts = Displaypath.Split(System.IO.Path.DirectorySeparatorChar);
if (pathparts.Length > 2) {
LogAlbum = HttpUtility.UrlEncode(pathparts[Index.FromEnd(1)]);
LogArtist = HttpUtility.UrlEncode(pathparts[Index.FromEnd(2)]);
CopyAlbum = pathparts[Index.FromEnd(1)];
CopyArtist = pathparts[Index.FromEnd(2)];
LogAlbum = HttpUtility.UrlPathEncode(pathparts[Index.FromEnd(1)]);
LogArtist = HttpUtility.UrlPathEncode(pathparts[Index.FromEnd(2)]);
LogUrl = Request.GetEncodedUrl();
LogConditionsMet = true;
}

View file

@ -7,12 +7,12 @@ using Microsoft.AspNetCore.Mvc;
namespace webmusic.Pages;
[ApiController]
[Route("/log")]
[ApiController, Route("/log")]
public class Log : Controller {
public const bool StatisticsEnabled = true;
public const string StatisticsUser = "zotan";
public const string NowPlayingUrl = "https://zotan.pw/np";
private const string StatisticsUrl = "https://zotan.pw/np/log";
private const string StatisticsSource = "music.ztn.sh";
private readonly string _statisticsToken = System.IO.File.Exists(".bearer_token") ? System.IO.File.ReadAllLines(".bearer_token")[0] : "";
@ -24,8 +24,8 @@ public class Log : Controller {
Response.StatusCode = (int)res;
return res switch {
HttpStatusCode.Created => $"Logged",
HttpStatusCode.Accepted => $"Skipped",
HttpStatusCode.Created => "Logged",
HttpStatusCode.Accepted => "Skipped",
HttpStatusCode.Forbidden => "Invalid token",
_ => $"Error {Response.StatusCode} \"{res}\" occured"
};

View file

@ -276,3 +276,7 @@ const formatTime = (secs) => {
document.addEventListener('DOMContentLoaded', initState);
document.addEventListener('keydown', handleKeyEvent);
function copyToClipboard(str) {
navigator.clipboard.writeText(str);
}