对于Google Chrome扩展程序,我需要捕获网页中的选定文本并将其发送到网络服务。我被卡住了!
首先,我尝试了一个书签,但是Mac上的Chrome似乎存在一些书签问题,因此我决定编写一个扩展程序。
我在ext中使用以下代码:
function getSelText(){
var txt = 'nothing';
if (window.getSelection){
txt = "1" + window.getSelection();
} else if (document.getSelection) {
txt = "2" + document.getSelection();
} else if (document.selection) {
txt = "3" + document.selection.createRange().text;
} else txt = "wtf";
return txt;
}
var selection = getSelText();
alert("selection = " + selection);
单击扩展名图标时,我得到一个“ 1”。因此,我认为在浏览器窗口之外进行选择的行为导致浏览器不再将文本视为“已选择”。
只是理论而已...
有什么想法吗?
湖上湖