使用document.execCommand(“paste”)粘贴剪贴板中的数据;在Firefox扩展 [英] paste data from clipboard using document.execCommand("paste"); within firefox extension
问题描述
我用这个代码创建了一个firefox user.js文件根据此建议来增加对剪贴板的访问。
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 \\\
);
}
};
req.send(null);
我很感激任何帮助。
谢谢
您需要的不是 execCommand
但你需要从剪贴板读取数据。你的插件是privelaged范围,所以你不必担心这些偏好。 (user.js是firefox-addon吧?)
请看这里:
通过这种方式,您可以将内容读入到var pastedContents
$ p
$ b $ 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 \\\
);
}
};
req.send(null);
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 increase access to clipboard 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 line two? I do not want any sites to have access. Just my internal firefox extension.
I have read several guides here and here as well as mozilla.
Here is the code I have so far. The clipboard contents should get sent POST method via XMLHttpRequest
. XMLHttpRequest
works, as I have been using 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. Thank you
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:
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);
这篇关于使用document.execCommand(“paste”)粘贴剪贴板中的数据;在Firefox扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!