简体   繁体   English

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 上工作。

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)
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM