【问题标题】:Trigger Chrome extension notifications from content script从内容脚本触发 Chrome 扩展通知
【发布时间】:2013-03-02 19:37:06
【问题描述】:

我目前正在尝试这样做,以便我的扩展程序在被调用时会显示通知。

当点击图标时,background.js 会在页面中执行一个脚本。这是我的 background.js 文件的样子:

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript(null,{file: "buy.js"});
  }
);

chrome.extension.onRequest.addListener(
  function (request, sender, sendResponse) {
    var notify = webkitNotifications.createNotification(
      'face.png',
      'Item Sniped!',
      'Item sniper sniped an item!'
    );
    notify.show();
  }
);

是的,我确实在 manifest.json 中设置了所有权限。我的清单文件:

{
   "manifest_version": 2,
   "name": "Sniper",
   "version": "1.5",
   "description": "Sniper",
   "browser_action": {
     "default_icon": "face.png",
     "default_title": "Limited Sniper"
   },
   "background": { "scripts": ["background.js"] },
   "permissions": [
     "notifications",
     "tabs",
     "http://*/*"
   ]
}

我知道我的 background.js 文件中需要有一个侦听器,但是否可以从 buy.js(执行的脚本)向 background.js 发送请求以发出通知?

【问题讨论】:

  • 请注意,这个问题中的很多信息已经过时了。 webkitNotifications 已弃用。

标签: javascript google-chrome-extension


【解决方案1】:

是的。内容脚本不能做某事。 see here

因此您必须向 background.js 发送请求以发出通知。 如果你的通知有一个图标,记得在 manifest.json 中注册它:

 "web_accessible_resources":["face.png"]

但是,内容脚本有一些限制。他们不能: 使用 chrome.* API(chrome.extension 的部分除外)使用变量或 由其扩展页面定义的函数使用变量或 由网页或其他内容脚本定义的功能

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 2011-11-02
    • 2019-11-05
    • 1970-01-01
    • 2014-02-04
    • 2011-04-25
    • 2015-12-24
    相关资源
    最近更新 更多