This calls a function, which should pull the contents of the clipboard, but it doesn’t:
function checkclip(){
bg = browser.extension.getBackgroundPage(); // get the background page
bg.document.body.innerHTML= ""; // clear the background page
// add a DIV, contentEditable=true, to accept the paste action
var helperdiv = bg.document.createElement("div");
document.body.appendChild(helperdiv);
helperdiv.contentEditable = true;
// focus the helper div's content
var range = document.createRange();
range.selectNode(helperdiv);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
helperdiv.focus();
Unfortunately, this is what I get on the console:
focus on: #document id: undefined content_script.js:18:7
focus on: TEXTAREA id: message content_script.js:18:7
Check read clipboard: context_menu.js:36:1
So, the function runs, but does not get the contents of the clipboard.
This is a crucial functionality of the addon that I am trying to recreate with Webextensions.
Yes, I’m trying to make sure that this pre 63 release firefox compatible, so using that API is decidedly non-optimal.
There are apps out there that do access the clipboard contents now, but I have been unable to tease out how they do it when I look at the (never documented) code, as I am a newbie on Javascript.
The thing is that I know of some people, myself included, who are using FF 56 because the extensions are still missing.
I found the solution here. I’ve incorporated the fix to the minor fix he was looking for:
function readClipboard () {
field = document.activeElement;
let area = document.createElement("textarea");
//document.body.appendChild 1(area);
field.parentNode.append(area);
area.contentEditable = true;
area.textContent = '';
area.select();
console.log('Pre-paste: ' + area.value);
console.log(document.execCommand("paste"));
console.log('Post-paste: ' + area.value);
area.setAttribute("style","display:none;");
return area.value;
}
It returns the contents of the clipboard when running from a CONTENT script.
I should note that I do not understand the above script, I’m a novice Javascript jockey, but I have been able to get the selection and the clipboard contents, which means that I can replace the clipboard and selection tokens with the value of using regexp:
I’m not sure it’s a good idea to target anything before 60 unless you are 100% sure your core users are there. For my add-on there is less than 2% of users running < 60 version. Also Firefox 60 ESR is currently the latest version supported by Firefox. So running anything lower is a BIG security risk.
Also supporting old versions will make your life only harder as there may be missing features (such as easy access to Clipboard using new API). If you are creating new add-on, I would totally target the latest release (unless you want to support ESR 60 users).
Regarding the code - the most important part is document.execCommand("paste") - no need to wap it in console.log.
If you are novice to JavaScirpt, make sure to read as much MDN as possible. It’s full of good examples.