Paste data from the clipboard using document.execCommand ("paste"); in the firefox extension

I am trying to paste data into the clipboard into a variable that receives and sends via the HTTPHttpRquest POST message.

I created firefox user.js with this code to increase buffer access based on this recommendation .

user_pref("capability.policy.policynames", "allowclipboard");
user_pref("capability.policy.allowclipboard.sites", "mydomain");
user_pref("capability.policy.allowclipboard.Clipboard.cutcopy", "allAccess");
user_pref("capability.policy.allowclipboard.Clipboard.paste", "allAccess");

Do I need to change "mydomain" in the second line? I do not want access to any sites. Just my internal firefox extension.

I read several guides here and here , as well as mozilla .

Here is the code I have. The contents of the clipboard must be sent using the POST method XMLHttpRequest. XMLHttpRequestworks as I used it for other variables.

 var pastetext = document.execCommand('paste');
 var req = new XMLHttpRequest();
 req.open('POST', pastetext, true);
 req.onreadystatechange = function(aEvt) {
     if (req.readyState == 4) {
         if (req.status == 200)
             dump(req.responseText);
         else
             dump("Error loading page\n");
     }
 };
 req.send(null);

I am grateful for any help. Thanks you

+2
source share
1 answer

You need not execCommand, but you need to read data from the clipboard. Your addon is in private mode, so you do not need to worry about these preferences. (user.js is firefox-addon right?)

Look here:

That way you can read the contents in var pastedContents.

Here is your example with the above:

var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
trans.addDataFlavor("text/unicode");
Services.clipboard.getData(trans, Services.clipboard.kGlobalClipboard);
var pastetextNsiSupports = {};
var pastetextNsiSupportsLength = {};
trans.getTransferData("text/unicode", pastetextNsiSupports, pastetextNsiSupportsLength);

var pastetext = pastetextNsiSupports.value.QueryInterface(Ci.nsISupportsString).data;
 var req = new XMLHttpRequest();
 req.open('POST', pastetext, true);
 req.onreadystatechange = function(aEvt) {
     if (req.readyState == 4) {
         if (req.status == 200)
             dump(req.responseText);
         else
             dump("Error loading page\n");
     }
 };
 req.send(null);
+2
source

All Articles