Add copy-to-clipboard button to meta page

Fixes #42.
This commit is contained in:
Daan Sprenkels 2020-04-05 18:14:02 +02:00
parent a766d5d596
commit 1dd0d17ba5
3 changed files with 54 additions and 0 deletions

View File

@ -19,4 +19,12 @@ body {
pre {
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
{{end}}
{{define "head-append"}}
<script type="text/javascript" src="/js/copyclipboard.js" defer></script>
{{end}}
{{define "body"}}
<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>
---
{{if and (ne .Paste.State.String "deleted") .CanDeleteBool}}