document.execCommand("paste") not working on some page


(Rappazf) #1

With Nightly and FF 57.04 It seems that the command

document.execCommand("paste");

is not working on same page:

   area = document.createElement("textarea");
   area.setAttribute("id", "fcb_txt_area");
   if (field) {
       field.parentNode.append(area);
   } 
   else {
         document.body.appendChild(area); 
   }
   area.contentEditable = true;
   area.textContent = '';
   area.select();
   console.log('Pre-paste: ' + area.value);
   document.execCommand("paste");
   var value = area.value;
    console.log('Post-paste: ' + area.value);
    return value;

On these page the Post-paste value is empty.

Strangely it is not consistent: when I load a page the it does not work but it seems that if I run a
document.execCommand('copy', false, null);
on the same page using my extension without reloading, then the paste command succeed …

Any idea someone ?
Thanks
F


(Martin Giger) #2

Where do you run the code you’re showing from? Since document.executeCommand has to be called from a user-action.


(Rappazf) #3

It’s run from a content script. Here is the manifest

{
   "background": {
      "scripts": [ "js/storage.js", "js/background.js"]
   },
  
   "icons": {
       "16": "img/icon-16x16.png",
       "48": "img/icon-48x48.png"
   },
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
     "js": ["js/content.js"]
  }
],
   "manifest_version": 2,
 "description": "__MSG_panel_label__",
  "name": "__MSG_extName__",
   "permissions": [ "storage", "contextMenus", "http://*/*", "https://*/*", "activeTab", "tabs","clipboardRead", "clipboardWrite" ],
   "version": "0.0.2",
    "options_ui": {
		"page":"options.html"
     },
	"default_locale": "en",
 "applications": {
    "gecko": {
      "id": "skimtheclipboardy@rappazf.ch",
      "strict_min_version": "54.0"
    }
  },

	"commands": {
		"one": {
		"suggested_key": {
			"default": "Ctrl+Shift+U",
			"mac": "Command+Shift+U"
		},
		"description": "run shortkey one"
		},
	"two": {
		"suggested_key": {
			"default": "Ctrl+Shift+B",
			"mac": "Command+Shift+B"
		},
		"description": "run shortkey two"
	},	
	"three": {
		"suggested_key": {
			"default": "Ctrl+Shift+C",
			"mac": "Command+Shift+C"
		},
		"description": "run shortkey three"
	}
	}
 
}

(Martin Giger) #4

So code’s the whole content of the content script?


(Rappazf) #5

You may test the extension
here

and here for example of a page that sometime fails

when using the extension to filter in pasting with >4 in the search field on top of the page

content.js
https://sourceforge.net/p/skimtheclipboard4ff/code/ci/default/tree/js/content.js


(Rappazf) #6

I have made a simpler example that shows this behaviour
content.js

document.addEventListener(“keyup”, paste);

function paste(){
var field = document.activeElement;
var area = document.getElementById(“paste_area”);
if (area) {
// console.log(“Found existing area”);
area.setAttribute(“style”, “visibility:visible;”);
// area.setAttribute(“style”, “display:inline;”);
}
else {
// console.log(“Creating new area”);
area = document.createElement(“textarea”);
area.setAttribute(“id”, “paste_area”);
if (field) {
field.parentNode.append(area);
}
else {
document.body.appendChild(area);
}
area.contentEditable = true;
}

 area.select();
 area.textContent = '';
 console.log('Pre-paste: ' + area.value);
 console.log(document.execCommand("paste"));
 var value = area.value;
 console.log('Post-paste: ' + area.value);

}

manifest.json

{
“description”: “get the clipboard content”,
“manifest_version”: 2,
“name”: “paste_test”,
“applications”: {
“gecko”: {
“id”: "paste_test@example.com"
}
},
“permissions”: [
“clipboardRead”,
“clipboardWrite”
],
“version”: “1.0”,
“browser_action”: {
“browser_style”: true,
“default_title”: “Paste_test”
},
“content_scripts”: [
{
“matches”: ["<all_urls>"],
“js”: [“content.js”]
}
]
}

Open nigthly and load this test extension, open the console
Now go to http://rzblx1.uni-regensburg.de/ezeit/fl.phtml?bibid=KUBFR&colors=7&lang=en
Select some text on the page and right click to copy the selection in the clipboard
Click in the search field
Use the shift key to run a keyup event : no text is pasted, even if true is return on the console.
Strangely if you open a empty notepad file, you paste there the clipboard content, reselect it and copy it again, the test works on the page…
It seems that with copying a part of the text from the same page that I want to paste it, I lost the clipboard access.
This behaviour is not show in all the page, for example in this page http://www.unifr.ch/dokpe/en a text copied from the page is always pasted in the custom google search field on the left.

I think I can fill a bug with that …


(Rappazf) #7

https://bugzilla.mozilla.org/show_bug.cgi?id=1433371


(Rappazf) #8

Am I correct in saying that with a priority of P3 (backlog)!, this bug won’t be corrected in future releases of Firefox ?

F.


(Martin Giger) #9

No. P3 means staff will get around to it, it just won’t be in the next dev cycles releases (so that’s 60 and 61) for staff. P5 would mean it would only happen if non-staff implemented it, however.