我目前正在使用Chrome扩展程序,如果getElementId不在页面中,它将通知..很遗憾,富通知未显示在contennt脚本中。我应该使用Message Passing吗?
load.js
con = document.getElementById('content');
if (con !=null){
var options = {
type:"basic",
title:"Error",
message:"Error",
iconUrl:"logo1.png"
}
chrome.notifications.create(options, callback);
function callback()
{
console.log('yey');
}
}
的manifest.json
{
"manifest_version": 2,
"name": "CRM Online Chrome Extension",
"description": "License authentication key for the clients.",
"version": "1.0",
"background": {
"scripts": [
"background.js"
],
"persistent": true
},
"content_scripts":[
{
"matches":[ "*://*/*",
"*://*/*"
],
"js":["payload.js","load.js"]
}
],
"browser_action": {
"default_title": "Sample",
"default_icon": "logo1.png",
"default_popup": "popup.html"
},
"permissions": [
"notifications",
"tabs",
"*://*/*",
"*://*/*"
]
// "options_page": "option.html"
}
答案 0 :(得分:2)
您必须从内容脚本send a message到后台页面,后者可以在收到消息时创建通知。
例如:
<强> background.js 强>
chrome.runtime.onMessage.addListener(function(message){
if (message.value == "contentPresent"){ //if the message received is the one we sent from our content script
chrome.notifications.create({ //create notificacion
type:"basic",
title:"Error",
message:"Error",
iconUrl:"logo1.png"
}, function(){
console.log("yey");
});
}
});
<强> content.js 强>
if (document.getElementById("content")) { //if the element with id "content" is found...
chrome.runtime.sendMessage({value:"contentPresent"}); //send a message to the background script
}