JingMatrix / ChromeXt

UserScript and DevTools supports for Chromium based and WebView based browsers

Home Page:https://jingmatrix.github.io/ChromeXt/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GM_setClipboard error

WhiteSevs opened this issue · comments

function GM_setClipboard(data, info = "text/plain") {
let type = info.mimetype || info;
if (!type.includes("/")) type = type == "html" ? "text/html" : "text/plain";
const blob = new Blob([data], { type });
data = [new ClipboardItem({ [type]: blob })];
return new Promise((resolve) => {
window.addEventListener(
"focus",
() => navigator.clipboard.write(data).then(() => resolve()),
{ once: true }
);
LockedChromeXt.unlock(key).dispatch("focus");
});
}

有些时候使用navigator.clipboard.write会出现DOMException: Write permission denied.错误导致无法写入剪贴板。
建议:对它加上catch,失败的话使用再进行第二种方式document.execCommand("copy")

Actually, no evidence suggests that document.execCommand("copy") could do a better job than navigator.clipboard.write.
If this error is not reproducible in a definite way, then I think the best solution for the UserScipt is to catch the error and call GM_setClipboard again.

It would be great if you can find a way to reproduce it, so that we know what goes wrong.

复制22222222使用的是ChromeXt的API,未成功复制,复制444444444使用的是我自己的API,成功复制

e2d428b043f4dcd15a415d3ad999a17e_20230917_16313183.mp4