从内容脚本触发Chrome扩展程序通知

问题描述:

我目前正在尝试使其扩展程序在调用时显示通知。从内容脚本触发Chrome扩展程序通知

当图标被点击时,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一个请求(执行脚本)背景。 js做出通知?

+0

请注意定义由它们的扩展的页面使用变量或 函数定义的变量或 功能。在这个问题上的很多信息已经过时了。 'webkitNotifications'已弃用。 – Xan 2015-05-29 12:09:36

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

所以你必须发送请求到background.js来做出通知。 ,如果您的通知有图标,rememmber以记数它在你的manifest.json:

"web_accessible_resources":["face.png"] 

然而,内容脚本有一定的局限性。他们不能:使用 铬*的API(除了chrome.extension的部分)使用的网页或其他内容的脚本