Do not show button if clipboard not available

This commit is contained in:
Daan Sprenkels 2020-04-05 18:46:40 +02:00
parent 1dd0d17ba5
commit 09481f47e6
3 changed files with 24 additions and 8 deletions

View File

@ -27,4 +27,8 @@ pre {
.fail { .fail {
color: #800000; color: #800000;
} }
.hidden {
display: none;
}

View File

@ -1,5 +1,6 @@
"use strict"; "use strict";
const COPY_TO_CLIPBOARD_CONTAINER_ID = "copyToClipboardContainer";
const COPY_TO_CLIPBOARD_STATUS_ID = "copyToClipboardStatus"; const COPY_TO_CLIPBOARD_STATUS_ID = "copyToClipboardStatus";
let copyToClipboardCtr = 0; let copyToClipboardCtr = 0;
@ -13,7 +14,9 @@ function copyToClipboardSuccess() {
function copyToClipboardFail(cause) { function copyToClipboardFail(cause) {
let statusElem = document.getElementById(COPY_TO_CLIPBOARD_STATUS_ID); let statusElem = document.getElementById(COPY_TO_CLIPBOARD_STATUS_ID);
if (!cause) cause = "unknown error"; if (!cause) cause = "unknown error";
statusElem.innerText = "copy failed: " + cause; let msg = "copy failed: " + cause;
console.log(msg);
statusElem.innerText = msg;
statusElem.classList.remove("success"); statusElem.classList.remove("success");
statusElem.classList.add("fail"); statusElem.classList.add("fail");
} }
@ -22,11 +25,7 @@ function copyToClipboard(url) {
let copyEventIdx = ++copyToClipboardCtr; let copyEventIdx = ++copyToClipboardCtr;
if (!window.navigator.clipboard) { if (!window.navigator.clipboard) {
let msg = "could not access clipboard"; copyToClipboardFail("could not access clipboard");
if (window.location.protocol !== "https:") {
msg += ": website not using HTTPS"
}
copyToClipboardFail(msg);
return; return;
} }
window.navigator.clipboard.writeText(url).then(() => { window.navigator.clipboard.writeText(url).then(() => {
@ -37,3 +36,16 @@ function copyToClipboard(url) {
copyToClipboardFail(); 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");
}
})();

View File

@ -7,7 +7,7 @@
{{end}} {{end}}
{{define "body"}} {{define "body"}}
<pre> <pre id="copyToClipboardContainer" class="hidden">
<button onclick="copyToClipboard('{{.RootURL}}/{{.Paste.Key}}{{.FileExt}}')">Copy URL to clipboard</button> <span id="copyToClipboardStatus"></span> <button onclick="copyToClipboard('{{.RootURL}}/{{.Paste.Key}}{{.FileExt}}')">Copy URL to clipboard</button> <span id="copyToClipboardStatus"></span>
</pre> </pre>
<pre> <pre>