Reading Multiline, Word, and HTML Data From the Clipboard

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’m sure you know https://github.com/tc39/proposal-global

Thanks for the fix.

I am getting one of two errors now:

In FF 56:
Error: Timeout after 1000ms dom.js:209:10

In FF 63 Beta:
NoModificationAllowedError: Modifications are not allowed for this document dom.js:198
The context menu is firing off:

case "test_clipboard":
    browser.tabs.sendMessage(tab.id,{runwhat: "zzBBCode", ParseArg: "[b]{{selection}}[/b]"});
break;

And the variable is passing to the content script: (much simplified to test just access to the clipboard)

(function(global) {

const { readFromClipboard, } = global.es6lib_dom

var clickedElement = null;



document.addEventListener("mousedown", function(event){
    //right click
    if(event.button == 2) { 
        clickedElement = event.target;
    }
}, true);

browser.runtime.onMessage.addListener(function(RunParse, CommandString, sendResponse) {
var CallFunction = RunParse.runwhat;
var ArgString = RunParse.ParseArg;
       CommandParse (ArgString);
        sendResponse({value: clickedElement.value});
});



async function CommandParse(ArgString) {

console.log("Confirm passing Argstring: ",ArgString);
    // ...
    const moop = (await readFromClipboard('text/plain'));
    console.log('`moop` is:', moop);
    // ...


console.log("After Read from Clipboard:");

}

})(this);

On the web console, I get:

Confirm passing Argstring: [b]{{selection}}[/b]

And in the browser console, I error messages.

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).

Thank you.

You sir, are a king among men, may the Flying Spaghetti Monster bless you with his noodly appendage.

Commenting out transfer.clearData(); fixed the issue on FF 63 beta, and multiline, html, and word clipboard contents work great.

FF56 still does not work, but I figure that if someone wants to test it out who is still using that, they can use FF portable.

I am going to work my way back to see what is the earliest version where this works.

(on edit) It works on 57.0, but not on pre-Quantum FF.

Well, thank you too! I see you are a man of culture.

I’ve updated the published version of es6lib without the transfer.clearData().

Do you remember why you included the code so in the first place?

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.