内容菜单和打开标签的问题 - Chrome扩展程序 [英] Issues with Content Menu and Opening Tabs - Chrome Extension

查看:205
本文介绍了内容菜单和打开标签的问题 - Chrome扩展程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网页上检测到以下代码未显示选择的问题。

im having issue with the below code not showing up with selection is detected on the webpage.

目前,当我选择文字时,上下文菜单未显示。

Currently when i am selecting text the context menu is not showing up.

 function getword(info,tab) {

 if (info.menuItemId == "google") {
console.log("Google" + info.selectionText + " was clicked.");
chrome.tabs.create({ 
    url: "http://www.google.com/search?q=" + info.selectionText,
})
 } else {
 console.log("Bing" + info.selectionText + " was clicked.");
  chrome.tabs.create({ 
    url: "http://www.bing.com/search?q=" +  info.selectionText,
 })
}
};

chrome.contextMenus.onClicked.addListener(getword);

chrome.runtime.onInstalled.addListener(function() {
  var contexts = ["page","selection","link","editable"];
  for (var i = 0; i < contexts.length; i++) {
    var context = contexts[i];
    var title = "Google Search";
    var id = chrome.contextMenus.create({"title": title, "contexts":[context],
                                     "id": "google"});
    console.log("'" + context + "' item:" + id);
   }
   chrome.contextMenus.create({"title": "Bing Search", "id": "child1"});

 });


推荐答案

价值id属性必须是唯一的。如果您查看背景页面的控制台

The value of the "id" property needs to be unique. You will see the following error if you view the console of your background page:

contextMenus.create: Cannot create item with duplicate id google
    at chrome-extension://ghbcieomgcdedebllbpimfgakljlleeb/background.js:23:34 

请勿致电 chrome。每个上下文的contextMenus.create ,但是将上下文列表分配给上下文键:

chrome.runtime.onInstalled.addListener(function() {
  var contexts = ["page","selection","link","editable"];
  var title = "Google Search";
  chrome.contextMenus.create({
    "title": title,
    "contexts": contexts,
    "id": "google"
  });
  // ...
});

这篇关于内容菜单和打开标签的问题 - Chrome扩展程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆