My mistake, the last line of the script should be })(this);.
or not use an iife to make window called global at all…
Yeah, but that is another of those weird JS things. It doesn’t really make sense for the global variable to be named window. Or for the Window instance to be the global variable.
With a function wrapper like this the code works in node.js as well.
I’ve never heard about a NoModificationAllowedError, but the most likely cause for the timeout is that you are not allowed to read the clipboard (at that time).
Try to read the clipboard at the same tiem you grab the clicked element (in a mousedown/click/whatever user action DOM handler).
As for the other error, you could try to remove the transfer.clearData(); line. I really can’t remember what that one is supposed to do. If that fixes the problem, I’ll update the code.
Just to clarify, you are suggesting that I should invoke it on the background page (where the right click menu is called), and not the foreground page.
That could work as well (with the right permission), but I was suggesting you do it in the content scripts, just earlier, when the DOM mousedown event fires (which should work without any specific permissions).
I am pretty sure I started with the reading function as a copy of the clipboard writing function. When writing, you get a mutable DataTransfer, which you need to clear first (in case the user had something selected).
Then writing the transfer seems to be immutable. I just forgot the .clearData() in the function. It seems that prevously it was a no-op (so I didn’t notice), but that changed and now it threw.