Firefox 51.0.1
I’m trying to do a Firefox webextension to copy the page title/selection/link title to clipboard when I select a context menu option but it doesn’t work.
I’ve seen these examples: context-menu-demo and selection-to-clipboard.
But:
If I create a contextmenu with a background script I can’t copy to clipboard.
And if I try to create a contextmenu with a content_scripts it doesn’t work: TypeError: browser.contextMenus is undefined.
Rather, you can’t use it in content scripts. It should work in popups and other views of the extension.
I experimented a bit with document.execCommand('copy'). It seems to me that, in order to succesfully copy (outside a “short-lived event handler”), you need the "clipboardWrite" permission and the frame you call execCommand in must be focused within it’s window. Which means it will fail if you call it from the devtools, but succeeds if you call it in a window in the background, but also not if the tab is not visible.
If this is true, it also means that execCommand will never work in the background page.
Which currently leaves to possible ways to implement what you want:
Use the browser.contextMenu API in the background page. From the click handler open a window in the background, successfully call execCommand('copy') in the focused tab therein, then close the window. This is quite ugly and requires "clipboardWrite".