我正在尝试将剪贴板数据粘贴到一个变量中,该变量通过 XMLhttprequest POST 消息输入并触发。
我使用此代码创建了一个 firefox user.js,以基于此 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");
我需要更改第二行中的“mydomain”吗?我不希望任何网站有权访问。只是我的内部 Firefox 扩展。
这是我到目前为止的代码。剪贴板内容应通过 XMLHttpRequest 发送 POST 方法。 XMLHttpRequest
有效,因为我一直将它用于其他变量。
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);
我很感激任何帮助。 谢谢
最佳答案
您需要的不是execCommand
,而是您需要从剪贴板读取数据。您的插件处于特权范围内,因此您无需担心这些首选项。 (user.js 是 firefox-addon 对吧?)
看这里:
这样您就可以将内容读入 var pastedContents
中。
这是您使用上述内容的示例:
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);
关于javascript - 使用 document.execCommand ("paste"从剪贴板粘贴数据);在 Firefox 扩展中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26545871/