paste data from clipboard using document.execCommand(“paste”); within firefox extension

后端 未结 1 1922
执笔经年
执笔经年 2020-12-11 10:25

I am trying to paste clipboard data into a variable that gets fed into and fired via XMLhttprequest POST message.

I have created a firefox user.js with this code to

相关标签:
1条回答
  • 2020-12-11 11:08

    What you need is not the execCommand but you need to read the data from the clipboard. Your addon is in privelaged scope so you don't need to worry about those preferences. (user.js is firefox-addon right?)

    See here:

    • MDN :: Using the Clipboard
    • MDN :: nsIClipboard

    This way you can read the contents into the var pastedContents.

    Here is your example with the above worked in:

    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);
    
    0 讨论(0)
提交回复
热议问题