Add copy-to-clipboard button to meta page #44

Merged
electricdusk merged 2 commits from issue-42 into master 2020-04-14 16:08:09 +02:00
3 changed files with 54 additions and 0 deletions
Showing only changes of commit 1dd0d17ba5 - Show all commits

View File

@ -20,3 +20,11 @@ body {
pre { pre {
padding-left: 4ex; /* approx 4 monospaced spaces */ padding-left: 4ex; /* approx 4 monospaced spaces */
} }
.success {
color: #008000;
}
.fail {
color: #800000;
}

View File

@ -0,0 +1,39 @@
"use strict";
const COPY_TO_CLIPBOARD_STATUS_ID = "copyToClipboardStatus";
let copyToClipboardCtr = 0;
function copyToClipboardSuccess() {
let statusElem = document.getElementById(COPY_TO_CLIPBOARD_STATUS_ID);
statusElem.innerText = "URL copied!";
statusElem.classList.remove("fail");
statusElem.classList.add("success");
}
function copyToClipboardFail(cause) {
let statusElem = document.getElementById(COPY_TO_CLIPBOARD_STATUS_ID);
if (!cause) cause = "unknown error";
statusElem.innerText = "copy failed: " + cause;
statusElem.classList.remove("success");
statusElem.classList.add("fail");
}
function copyToClipboard(url) {
let copyEventIdx = ++copyToClipboardCtr;
if (!window.navigator.clipboard) {
let msg = "could not access clipboard";
if (window.location.protocol !== "https:") {
msg += ": website not using HTTPS"
}
copyToClipboardFail(msg);
return;
}
window.navigator.clipboard.writeText(url).then(() => {
if (copyToClipboardCtr !== copyEventIdx) return;
copyToClipboardSuccess();
}, () => {
if (copyToClipboardCtr !== copyEventIdx) return;
copyToClipboardFail();
});
}

View File

@ -2,8 +2,15 @@
'{{.Paste.Key}}{{.FileExt}}' metadata - rushlink '{{.Paste.Key}}{{.FileExt}}' metadata - rushlink
{{end}} {{end}}
{{define "head-append"}}
<script type="text/javascript" src="/js/copyclipboard.js" defer></script>
{{end}}
{{define "body"}} {{define "body"}}
<pre> <pre>
<button onclick="copyToClipboard('{{.RootURL}}/{{.Paste.Key}}{{.FileExt}}')">Copy URL to clipboard</button> <span id="copyToClipboardStatus"></span>
</pre>
<pre>
<a href="{{.RootURL}}/{{.Paste.Key}}{{.FileExt}}">{{.RootURL}}/{{.Paste.Key}}{{.FileExt}}</a> <a href="{{.RootURL}}/{{.Paste.Key}}{{.FileExt}}">{{.RootURL}}/{{.Paste.Key}}{{.FileExt}}</a>
--- ---
{{if and (ne .Paste.State.String "deleted") .CanDeleteBool}} {{if and (ne .Paste.State.String "deleted") .CanDeleteBool}}