1 year ago
#46111
jqbotan
navigator.clipboard.writeText() fails within an unpacked Edge or Chrome extension
My goal is to make an extension for myself(and hopefully others) that adds a more left-handed user friendly copy link address keybind(Y or A) for Microsoft Edge. But as stated navigator.clipboard.writeText()
fails without any meaningful indicator to the problem and nothing is copied to system clipboard.
Environment:
- Windows 10
- Chrome 97.0.4692.71
- Edge 97.0.1072.55
The JS code:
const CONTEXT_MENU_ID = "MY_COPY_LINK";
async function copyLink(info, tab) {
if (info.menuItemId !== CONTEXT_MENU_ID) {
return;
}
var urlToCopy = info.linkUrl;
console.log(urlToCopy + " was copied.");
if(navigator.clipboard) {
navigator.clipboard.writeText(urlToCopy).then(function() {
/* clipboard successfully set */
console.log("success");
}, function() {
/* clipboard write failed */
console.log("failed");
});
}
else {
console.log("clipboard undefined");
}
}
chrome.contextMenus.create({
title: "Cop&y Link Address",
contexts:["link"],
id: CONTEXT_MENU_ID
});
chrome.contextMenus.onClicked.addListener(copyLink)
Manifest file:
{
"manifest_version": 2,
"name": "copylinkaddress",
"version": "1",
"permissions": ["contextMenus","clipboardWrite"],
"background": {
"scripts": ["background.js"],
"persistent": true
}
}
I've tried using write()
only with a text/plain
ClipboardItem
but same result, it just fails.
javascript
google-chrome
google-chrome-extension
microsoft-edge
microsoft-edge-extension
0 Answers
Your Answer