我遇到了一个非常大的问题,以某种方式从网页中获取所选文本,然后使用上下文菜单处理...当我解决Chrome
,Firefox
和IE11
时,我发现当所选文字位于输入框中时,我无法在Firefox
和Internet Explorer
中获取所选文字。
我已经搜索了几天以某种方式解决,最后我取得了成功......
这是Internet Explorer
var parentwin = external.menuArguments
var selectedText = getSel();
function getSel(){
var w=window,d=parentwin.document,gS='getSelection';
return (''+(w[gS]?w[gS]():d[gS]?d[gS]():d.selection.createRange().text)).replace(/(^\s+|\s+$)/g,'');
}

这适用于FireFox
var contextMenu = require("sdk/context-menu");
var tabs = require("sdk/tabs");
var clipboard = require("sdk/clipboard");
var {Cc,Ci} = require('chrome');
var simpleGetLibrary = null;
var menuItem = contextMenu.Item({
label: "Get selected text",
context: contextMenu.SelectionContext(),
contentScript: 'self.on("click", function () {' +
' var text = window.getSelection().toString();' +
' self.postMessage(text);' +
'});',
onMessage: function (selectionText) {
console.log(selectionText);
clipboard.set(selectionText); //this line copy the selected text to Clipboard
}

但是我不知道,当它在输入框中时,我怎么能解决这个问题...... <INPUT> ...</INPUT>
解决方案如下......
答案 0 :(得分:1)
Internet Explorer
IE11
var parentwin = external.menuArguments
var selectedText = getSel();
function getSel(){
var w=parentwin.window,d=parentwin.document,gS='getSelection';
var selectedText;
var rv=(''+(w[gS]?w[gS]():d[gS]?d[gS]():d.selection.createRange().text)).replace(/(^\s+|\s+$)/g,'');
parentwin.console.log("the selected text is in first try is:"+rv);
if (rv=="") {
var allinput = parentwin.document.getElementsByTagName("input"); //I get an array of all input tags
var index;
var newsel;
var found="false";
for (index = 0; index < allinput.length; index++) {
parentwin.console.log("index: "+index);
newsel="";
parentwin.console.log("newsel= ");
try {
var ss = allinput[index].selectionStart;
parentwin.console.log("ss");
var se = allinput[index].selectionEnd;
parentwin.console.log("se");
if (typeof ss === "number" && typeof se === "number") {
newsel=allinput[index].value.substring(ss, se);
found=newsel;
if (newsel.length>0){
rv=newsel;
found="true";
allinput[index].selectionEnd=allinput[index].selectionStart;
newsel="";
}
}
}
catch(err){}
parentwin.console.log("input fields "+index+"/"+allinput.length+" fieldname:"+allinput[index].getAttribute("name")+" selection: "+newsel);
if (found=="true"){
index=allinput.length;
}
}
parentwin.console.log("found=:"+found);
}
return rv;
}
&#13;
而且Firefox
有点棘手,因为我无法访问DOM,所以我需要在ContentScript构建中解决这个问题,然后直接发送结果..因为我可以&# 39;解决如何只获取选择,我发送输入框的值,如果不是全部选中则不重要...
var contextMenu = require("sdk/context-menu");
var tabs = require("sdk/tabs");
var clipboard = require("sdk/clipboard");
var preferences = require("sdk/simple-prefs").prefs;
var {Cc,Ci} = require('chrome');
var simpleGetLibrary = null;
var menuItem = contextMenu.Item({
label: "Get selected text",
context: contextMenu.SelectionContext(),
contentScript: 'self.on("click", function (node, data) {' +
' var text = window.getSelection().toString();' +
' console.log("Selected node is: "+node.nodeName);' +
' console.log("Selected value is: "+node.value);' +
' if (text.lenght>0)' +
' self.postMessage(text);' +
' else ' +
' if (node.nodeName=="INPUT")' +
' self.postMessage(node.value);' +
'});',
onMessage: function (selectionText) {
console.log("Selection sent for processing is: "+selectionText);
clipboard.set(selectionText); //this line copy the selected text to Clipboard
}
&#13;