内容脚本的桌面通知 [英] Desktop notifications from content scripts
问题描述
我正在尝试从内容脚本中显示一个简单的桌面通知代码,但是它似乎不起作用.我已在maifest.json文件中添加了权限.在内容脚本中显示它们是否受到限制?
I am trying to show a simple desktop notification code from a content script, but it doesn't seem to work.. I have added the permissions in the maifest.json file. Is there a restriction on showing them from the content script ?
推荐答案
是的,通知使用Chrome特定的API,并且内容脚本仅对常规javascript等有效. .google.com/chrome/extensions/background_pages.html"rel =" nofollow noreferrer>背景页面是可以运行所有chrome专用API的地方...首先,您需要在 manifest.json 文件-像这样:
Yes, notifications use Chrome specific API, and the content script is only valid for general javascript etc... The background page is where all chrome specific API's are capable of running... First you'll need to register your background page in the manifest.json file - like this:
"background_page": "background.html",
也在清单文件中,允许所需的权限:
Also in the manifest file, Allow the required permissions:
"permissions": [ "notifications" ],
然后,您在后台页面中的脚本应如下所示:
Then your script in the background page should look like this :
<script>
setTimeout("setNotification();",1);
function setNotification(){
var n
if (window.webkitNotifications.checkPermission() != 0){
setNotification();
return false;
}
n = window.webkitNotifications.createHTMLNotification('http://www.your-notification-address.com');
n.show();}
</script>
这篇关于内容脚本的桌面通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!