【问题标题】:contextMenu not working in Chrome Packaged AppcontextMenu 在 Chrome 打包应用程序中不起作用
【发布时间】:2013-12-07 16:25:06
【问题描述】:

我有一个 Chrome 打包应用程序,但我似乎不知道如何向它添加 contextMenus。

这是我的清单:

{
  "name": "Dialer",
  "version": "0.1",
  "manifest_version": 2,
  "permissions": [
    "contextMenus",
    "audioCapture"
  ],
  "app": {
    "background": {
      "scripts": ["background.js"]
    }
  },
  "icons": {
    "128":"icon_128.png"
  }
}

这是我的 background.js:

chrome.app.runtime.onLaunched.addListener(function() {
    chrome.contextMenus.create ({
        title: "%s", 
        contexts: ["all"],
        id: "right-click"
    });
});

我做错了什么?右键单击时菜单项不出现。

谢谢

【问题讨论】:

  • 返回的错误是什么(使用回调检查 chrome.runtime.lastError)?设置 id 与处理右键单击有什么关系?
  • 奇怪的是我在控制台中没有收到任何错误。 id 只是我输入的内容。当我在控制台中运行代码时,我得到“未定义”。
  • 来自文档:“请注意,如果在创建过程中发生错误,您可能在创建回调触发之前不会发现”。创建回调是 chrome.contextMenus.create 的可选第二个参数:`chrome.contextMenus.create({...}, function() { console.log(chrome.runtime.lastError); });
  • @MatrixFrog 感谢您的解释。我将代码添加到 chrome.runtime.lastError 并在控制台中未定义。有什么想法吗?

标签: javascript google-chrome google-chrome-extension google-chrome-app


【解决方案1】:

您可能需要查看 Chrome 应用示例,网址为 https://github.com/GoogleChrome/chrome-app-samples/tree/master/context-menu 了解它的工作原理。

如您所见,上下文菜单将出现在您的 Chrome 应用程序窗口中:

chrome.app.runtime.onLaunched.addListener(function() {
  chrome.app.window.create('main.html', {bounds:{ width: 300, height: 300}});
});

chrome.runtime.onInstalled.addListener(function() {
  // When the app gets installed, set up the context menus
  chrome.contextMenus.create({
    title: 'Click on me',
    id: 'contextMenuId',
    contexts: [ 'all' ]
  });
});

如果您正在寻找 Chrome 浏览器中的上下文菜单,您可能需要创建一个 Chrome 扩展程序。见http://developer.chrome.com/extensions/contextMenus.html

【讨论】:

    猜你喜欢
    • 2013-05-22
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    相关资源
    最近更新 更多