Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

javascript - Chrome上下文菜单扩展程序:将选定的文本保存到变量中(Chrome contextmenu extension: save selected text into a variable)

I am trying to create an extension that will search the selected word into a dictionary.

(我正在尝试创建一个扩展程序,它将选择的单词搜索到词典中。)

I am trying to get the selected text into a variable in JavaScript but it's not working.

(我正在尝试将选定的文本转换成JavaScript中的变量,但无法正常工作。)

Here is the code:

(这是代码:)

var search= {
    "id": "searchurl",
    "title": "Search on dictionary",
    "contexts": ["selection"]

};

chrome.contextMenus.create(search);

chrome.contextMenus.onClicked.addListener(function(clickData){
    "use strict";
    if (clickData.menuItemId === "searchurl" && clickData.selectionText){
        var urlkey = Document.getSelection().toString();
        alert(urlkey);

    }

});

I have included jquery to make it work on chrome.

(我包括了jquery,使其可以在chrome上使用。)

  ask by Abid translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

clickData.selectionText already has your selected text.

(clickData.selectionText已经具有您选择的文本。)

var search = {
    "id": "searchurl",
    "title": "Search on dictionary",
    "contexts": ["selection"]
};

chrome.contextMenus.create(search);

chrome.contextMenus.onClicked.addListener(function(clickData) {
    if (clickData.menuItemId === "searchurl" && clickData.selectionText) {
        alert(clickData.selectionText)
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...