diff --git a/assets/css/main.css b/assets/css/main.css index 54f9979..746a77a 100644 --- a/assets/css/main.css +++ b/assets/css/main.css @@ -19,4 +19,16 @@ body { pre { padding-left: 4ex; /* approx 4 monospaced spaces */ -} \ No newline at end of file +} + +.success { + color: #008000; +} + +.fail { + color: #800000; +} + +.hidden { + display: none; +} diff --git a/assets/js/copyclipboard.js b/assets/js/copyclipboard.js new file mode 100644 index 0000000..9967dd9 --- /dev/null +++ b/assets/js/copyclipboard.js @@ -0,0 +1,51 @@ +"use strict"; + +const COPY_TO_CLIPBOARD_CONTAINER_ID = "copyToClipboardContainer"; +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"; + let msg = "copy failed: " + cause; + console.log(msg); + statusElem.innerText = msg; + statusElem.classList.remove("success"); + statusElem.classList.add("fail"); +} + +function copyToClipboard(url) { + let copyEventIdx = ++copyToClipboardCtr; + + if (!window.navigator.clipboard) { + copyToClipboardFail("could not access clipboard"); + return; + } + window.navigator.clipboard.writeText(url).then(() => { + if (copyToClipboardCtr !== copyEventIdx) return; + copyToClipboardSuccess(); + }, () => { + if (copyToClipboardCtr !== copyEventIdx) return; + copyToClipboardFail(); + }); +} + +(function () { + if (!window.navigator.clipboard) { + let msg = "cannot access clipboard"; + if (window.location.protocol !== "https:") { + msg += ": website not using https" + } + console.error(msg); + } else { + let container = document.getElementById(COPY_TO_CLIPBOARD_CONTAINER_ID); + container.classList.remove("hidden"); + } +})(); diff --git a/assets/templates/html/pasteMeta.html.tmpl b/assets/templates/html/pasteMeta.html.tmpl index e697c30..c0c8026 100644 --- a/assets/templates/html/pasteMeta.html.tmpl +++ b/assets/templates/html/pasteMeta.html.tmpl @@ -2,7 +2,14 @@ '{{.Paste.Key}}{{.FileExt}}' metadata - rushlink {{end}} +{{define "head-append"}} + +{{end}} + {{define "body"}} +
 {{.RootURL}}/{{.Paste.Key}}{{.FileExt}}
 ---