如何在某些网站扩展页面操作上显示通知
how to show the notification on certain websites extension page action
我正在开发适用于某些网站的扩展页面操作 我想在用户访问特定网站时添加通知 我对地址栏中的图标不满意,通知如何显示当用户访问特定站点时?
我有这些代码
背景,在地址栏中显示特定站点的图标
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (~tab.url.indexOf('specificsite.com.br')) {
chrome.pageAction.show(tabId);
}
});
通知代码
createNotification();
audioNotification();
function audioNotification(){
var yourSound = new Audio('alert.mp3');
yourSound.play();
}
function createNotification(){
var opt = {type: "basic",title: "Your Title",message: "Your message",iconUrl: "128.png"}
chrome.notifications.create("notificationName",opt,function(){});
//include this line if you want to clear the notification after 5 seconds
setTimeout(function(){chrome.notifications.clear("notificationName",function(){});},10000);
}
您可以使用message passing通过content scripts来检测某些网站的切换,然后通知后台页面以显示该页面的通知。您的内容脚本应使用 chrome.runtime.sendMessage
发送消息,后台页面应使用 chrome.runtime.onMessage.addListener
:
收听
我创建了示例代码并测试了它是否适用于我:
内容脚本(myscript.js):
if(onCertainWebsitesNeedNotificationAppearTrue) {
// send message to background script
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
});
}
背景页:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
//alert("good");
if (request.greeting == "hello")
createNotification();
});
function createNotification(){
var opt = {type: "basic",title: "Your Title",message: "Your message",iconUrl: "128.png"}
chrome.notifications.create("notificationName",opt,function(){});
//include this line if you want to clear the notification after 5 seconds
setTimeout(function(){chrome.notifications.clear("notificationName",function(){});},10000);
}
另外请记住在清单中注册内容脚本的代码和权限,例如:
"permissions": ["notifications"],
"content_scripts": [
{
"matches": ["http://www.certainwebsiteone.com/*", "http://certainwebsitetwo.com/*"],
"js": ["myscript.js"]
}
]
我正在开发适用于某些网站的扩展页面操作 我想在用户访问特定网站时添加通知 我对地址栏中的图标不满意,通知如何显示当用户访问特定站点时?
我有这些代码
背景,在地址栏中显示特定站点的图标
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (~tab.url.indexOf('specificsite.com.br')) {
chrome.pageAction.show(tabId);
}
});
通知代码
createNotification();
audioNotification();
function audioNotification(){
var yourSound = new Audio('alert.mp3');
yourSound.play();
}
function createNotification(){
var opt = {type: "basic",title: "Your Title",message: "Your message",iconUrl: "128.png"}
chrome.notifications.create("notificationName",opt,function(){});
//include this line if you want to clear the notification after 5 seconds
setTimeout(function(){chrome.notifications.clear("notificationName",function(){});},10000);
}
您可以使用message passing通过content scripts来检测某些网站的切换,然后通知后台页面以显示该页面的通知。您的内容脚本应使用 chrome.runtime.sendMessage
发送消息,后台页面应使用 chrome.runtime.onMessage.addListener
:
我创建了示例代码并测试了它是否适用于我:
内容脚本(myscript.js):
if(onCertainWebsitesNeedNotificationAppearTrue) {
// send message to background script
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
});
}
背景页:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
//alert("good");
if (request.greeting == "hello")
createNotification();
});
function createNotification(){
var opt = {type: "basic",title: "Your Title",message: "Your message",iconUrl: "128.png"}
chrome.notifications.create("notificationName",opt,function(){});
//include this line if you want to clear the notification after 5 seconds
setTimeout(function(){chrome.notifications.clear("notificationName",function(){});},10000);
}
另外请记住在清单中注册内容脚本的代码和权限,例如:
"permissions": ["notifications"],
"content_scripts": [
{
"matches": ["http://www.certainwebsiteone.com/*", "http://certainwebsitetwo.com/*"],
"js": ["myscript.js"]
}
]