内容脚本通知

时间:2012-06-07 02:12:43

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

遇到麻烦,并没有在这里找到任何可以回答我所拥有的东西:/

清单:

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

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',  // icon url - can be relative
      'Hello!',  // notification title
      'Oh hellow!'  // notification body text
    );
});

Buy.js [还有更多内容,但这是通知部分]:

chrome.extension.sendRequest({msg: "Sup?"}, function(response) { // optional callback -     gets response
    console.log(response.returnMsg);
});

我基本上希望内容脚本能够创建通知,但我不知道是否有可能坚持使用js脚本作为背景:/

感谢您的帮助, 亚历

2 个答案:

答案 0 :(得分:0)

background property仅适用于使用version 2的清单。如果您想支持此功能,则需要将清单更新为以下内容;

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

请注意,我还将minimum_chrome_version属性设置为18,因为清单版本2只能在定位此版本的Chrome或更新版本时使用。

答案 1 :(得分:0)

我想你错过了调用notify.show();在你的background.js

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    var notify = webkitNotifications.createNotification(
      'face.png',  // icon url - can be relative
      'Hello!',  // notification title
      'Oh hellow!'  // notification body text
    );
    notify.show();
});

http://code.google.com/chrome/extensions/notifications.html#api