1
我遇到了一个非常大的问题,为了从网页中以某种方式获取选定的文本,然后使用contextmenu处理它......当我解决Chrome
,Firefox
和IE11
,我发现我不能得到Firefox
和Internet Explorer
时选定的文本是在输入框中输入选定的文本....上下文菜单如果文本是输入标记/文本框,则JavaScript无法获取所选文本
...我已经寻找天以某种方式解决,最后我已成功...
这是原始脚本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);
\t clipboard.set(selectionText); //this line copy the selected text to Clipboard
}
但我不知道,我怎么能解决让莫名其妙的文字时,它是在一个输入框... <INPUT> ...</INPUT>
解决方案如下...
什么是真棒解决方案!很好地输入!总是为那些想出问题并回来分享答案的人提供支持! – Noitidart 2015-01-16 03:01:33
#Noitidart谢谢 – RobiGo 2015-02-11 14:08:11