[英]Trigger Chrome extension notifications from content script
I currently am trying to make it so my extension will show a notification when called on to do so. 我目前正在尝试这样做,所以我的扩展程序会在调用时显示通知。
When the icon is clicked, background.js executes a script into the page. 单击图标时,background.js会在页面中执行脚本。 This is what my background.js file looks like:
这是我的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();
}
);
And yes, I did set up all of the permissions in manifest.json. 是的,我确实在manifest.json中设置了所有权限。 My manifest file:
我的清单文件:
{
"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://*/*"
]
}
I know that I need to have a listener in my background.js file, but is it even possible to send a request from buy.js (The script that is executed) to background.js to make the notification? 我知道我需要在background.js文件中有一个监听器,但是甚至可以从buy.js(执行的脚本)向background.js发送请求来发出通知吗?
yes. 是。 content scripts can not do something.
内容脚本无法做某事。 see here
看这里
so you have to send a request to background.js to make the notification. 所以你必须向background.js发送一个请求来发出通知。 and, if your notification has a icon, rememmber to regist it in you manifest.json:
并且,如果您的通知有图标,请记住在您的manifest.json中注册它:
"web_accessible_resources":["face.png"]
However, content scripts have some limitations.
但是,内容脚本有一些限制。 They cannot: Use chrome.* APIs (except for parts of chrome.extension) Use variables or functions defined by their extension's pages Use variables or functions defined by web pages or by other content scripts
它们不能:使用chrome。* API(chrome.extension的部分除外)使用由其扩展名页面定义的变量或函数使用由网页或其他内容脚本定义的变量或函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.