Chrome扩展程序上下文菜单未显示 [英] Chrome extension context menu not showing up

查看:122
本文介绍了Chrome扩展程序上下文菜单未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个上下文菜单项添加到一个Chrome应用程序中,它根本没有显示出来。我读过的所有内容似乎都表明我在这里做的是正确的事情,但显然我没有。



background.js:

  var clickHandler = function(e){
console.log('testing testing');
}
chrome.contextMenus.create({
title:Click Me,
contexts:[page,selection,image, link],
onclick:clickHandler
});

manifest.json:

  {
update_url:https://clients2.google.com/service/update2/crx,
name:Test,
description:Test,
manifest_version:2,
version:3.2.3,
kiosk_enabled:true,
图标: {
128:icon_128.png,
16:icon_16.png
},
app:{
background :{
scripts:[background.js],
persistent:false
}
},
permissions:[
http:// * /,https:// * /,webview,storage,power,alwaysOnTopWindows,idle,contextMenus
]
}

没有记录错误或类似的东西。该项目根本不显示。我错过了什么?

解决方案

您错过了便签 contextMenu 文档中:


函数(可选) onclick

单击菜单项时将被调用的函数。活动页面不能使用它;相反,他们应该为 chrome.contextMenus.onClicked




注册监听器确实有活动页面persistent):false

Chrome会卸载该页面,并且对 clickHandler 的引用可以走开。相反,事件页面机制确保如果您使用 addListener 注册了一个事件,页面将被重新加载, addListener

所以:

  var clickHandler = function(e){
console.log('testing testing');


chrome.contextMenus.create({
title:Click Me,
contexts:[page,selection, image,link]
});

//必须在事件页面加载时同步调用,
//例如在顶层代码中
chrome.contextMenus.onClicked.addListener(clickHandler);


I'm trying to add a context menu item to a chrome app and it's not showing up at all. Everything I've read seems to indicate that I'm doing the right thing here, but evidently I'm not.

background.js:

var clickHandler = function(e) {
  console.log('testing testing');
}
chrome.contextMenus.create({
  "title": "Click Me",
  "contexts": ["page", "selection", "image", "link"],
  "onclick" : clickHandler
});

manifest.json:

{
    "update_url": "https://clients2.google.com/service/update2/crx",
    "name": "Test",
    "description": "Test",
    "manifest_version": 2,
    "version": "3.2.3",
    "kiosk_enabled": true,
    "icons": {
        "128": "icon_128.png",
        "16": "icon_16.png"
    },
    "app": {
        "background": {
            "scripts": [ "background.js" ],
            "persistent": false
        }
    },
    "permissions": [
        "http://*/", "https://*/", "webview", "storage", "power", "alwaysOnTopWindows", "idle", "contextMenus"
    ]
}

There are no errors being logged or anything like that. The item simply does not show up. What am I missing here?

解决方案

You missed a small note in the contextMenu documentation:

function (optional) onclick
A function that will be called back when the menu item is clicked. Event pages cannot use this; instead, they should register a listener for chrome.contextMenus.onClicked

You do have an Event page ("persistent": false), so it applies to you.

Chrome unloads the page, and the reference to clickHandler can get lost. On the contrary, Event page mechanism ensures that if you registered an event with addListener the page will be loaded again, addListener applied again and then your listener executed.

So:

var clickHandler = function(e) {
  console.log('testing testing');
}

chrome.contextMenus.create({
  "title": "Click Me",
  "contexts": ["page", "selection", "image", "link"]
});

// Must be synchronously called on event page load,
//   for instance in the top level code
chrome.contextMenus.onClicked.addListener(clickHandler);

这篇关于Chrome扩展程序上下文菜单未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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