Skip to content

Commit 46b606e

Browse files
committed
fix: Handling of promises for insecure contexts
1 parent e179e1f commit 46b606e

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

resources/static/script.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,20 @@ const TR = (i, row) => {
375375
const copyShortUrl = (shortLink, doCopy) => {
376376
const fullLink = `${SITE_URL}/${shortLink}`;
377377
const linkElt = `<a href=${fullLink} target="_blank">${fullLink}</a>`;
378-
const copyPromise = doCopy // We want to use it only for the UI in some cases
379-
? navigator.clipboard.writeText(fullLink)
380-
: Promise.resolve();
378+
379+
let copyPromise;
380+
if (
381+
typeof ClipboardItem === "function" &&
382+
typeof navigator.clipboard === "object"
383+
) {
384+
copyPromise = doCopy // We want to use it only for the UI in some cases
385+
? navigator.clipboard.writeText(fullLink)
386+
: Promise.resolve();
387+
} else {
388+
copyPromise = Promise.reject(
389+
new Error("Unable to get access to clipboard."),
390+
);
391+
}
381392

382393
copyPromise
383394
.then(() =>
@@ -572,7 +583,10 @@ const submitForm = () => {
572583
};
573584

574585
let ok = false;
575-
if (typeof ClipboardItem && navigator.clipboard.write) {
586+
if (
587+
typeof ClipboardItem === typeof Function &&
588+
typeof navigator.clipboard === typeof Function
589+
) {
576590
const copyPromise = new ClipboardItem({
577591
"text/plain": fetch(url, payload)
578592
.then((res) => {

0 commit comments

Comments
 (0)