1

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.

jqbotan
  • 11
  • 2
  • How does it fail? Does it log the failure message? Does it not log anything at all? – yaakov Jan 13 '22 at 10:29
  • @yainspan Yes it logs and enters the second anonymous function verified via DevTools console. If I add an argument(err) to failure clause, `writeText()` still fails and error message becomes `failed DOMException: Document is not focused.` Tried to breakpoint and watch but I'm not sure what to look for exactly. – jqbotan Jan 13 '22 at 11:09
  • @wOxxOm Is it related to v3 even though I defined mine as v2? But fwiw I did try first as v3 and same as the bug you linked, I kept getting `navigator.clipboard` as undefined. What perplexes me is that an exact extension of what I'm trying to do exists for Firefox, which I used as reference. Theirs is v2 manifest as well and a very simple `writeText()`. – jqbotan Jan 13 '22 at 12:00
  • Ah, you're lucky then: you can use a textarea + document.execCommand in your background script. – wOxxOm Jan 13 '22 at 12:10
  • @wOxxOm Wow ok, that worked beautifully. But as I understand, this method is deprecated/not recommended anymore? Still wonder why code above doesn't work though. – jqbotan Jan 13 '22 at 13:19
  • Your code doesn't work because the background script is not visible. This workaround won't work in the future when MV2 is removed from the browser. The solution is to wait for https://crbug.com/1160302 to be fixed. – wOxxOm Jan 13 '22 at 14:14
  • Yeah that's my fear and why I used navigator.clipboard instead. I understand, will monitor that bug then. Thank you for your time and help. – jqbotan Jan 13 '22 at 23:22

0 Answers0