Google chrome extension 如何在chrome.ContextMenu中放置选择文本?

Google chrome extension 如何在chrome.ContextMenu中放置选择文本?,google-chrome-extension,Google Chrome Extension,如何在上下文菜单中添加选择文本 我想创建一个Chrome扩展,其工作方式类似于Google Chrome中的右键单击搜索功能(即右键单击所选文本->搜索“选择文本”) 我想这是一个很好的例子,但我不知道如何让它起作用 background.js: chrome.runtime.onInstalled.addListener(function () { var context = "selection"; var title = "Search"; var id = chrome.c

如何在上下文菜单中添加选择文本

我想创建一个Chrome扩展,其工作方式类似于Google Chrome中的右键单击搜索功能(即右键单击所选文本->搜索“选择文本”)

我想这是一个很好的例子,但我不知道如何让它起作用

background.js:

chrome.runtime.onInstalled.addListener(function () {
  var context = "selection";
  var title = "Search";
  var id = chrome.contextMenus.create({
    "title": title,
    "contexts": [context],
    "id": "context" + context
  });
});

// add click event
chrome.contextMenus.onClicked.addListener(onClickHandler);

// The onClicked callback function.
function onClickHandler(info, tab) {
  var sText = info.selectionText;
  var url = "https://www.google.com/search?source=hp&q=" + encodeURIComponent(sText);
  window.open(url, '_blank');
};
在“搜索”中添加
%s
,更多信息请参阅。