Javascript 在chrome extensions中加载新页面时显示通知
我正在google chrome中开发一个扩展。我想在当前活动选项卡中加载任何新页面时显示桌面通知 但是我写的代码不能正常工作Javascript 在chrome extensions中加载新页面时显示通知,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我正在google chrome中开发一个扩展。我想在当前活动选项卡中加载任何新页面时显示桌面通知 但是我写的代码不能正常工作 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo) { if (changeInfo.status === 'complete') { chrome.tabs.executeScript(tabId, { chrome.notifications.create(‘id
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo) {
if (changeInfo.status === 'complete') {
chrome.tabs.executeScript(tabId, {
chrome.notifications.create(‘id1’, {
type: ‘basic’,
iconUrl: ‘icon.png’,
title: ‘Review Baed Surfing’,
message: ‘Check URL now by clicking FAKE URL icon infront on Address Bar!’,
priority: 0
},
function() { /* Error checking goes here */}
);
});
}
});
[已解决]
我在代码中做了以下更改
chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
if (changeInfo.status == 'complete' && tab.active) {
chrome.notifications.create('id1',{
type: 'basic',
iconUrl: 'icon.png',
title: 'Review Baed Surfing',
message: 'Check URL now by clicking FAKE URL icon infront on Address Bar!',
priority: 0
},
function() { /* Error checking goes here */}
);
}
});
现在它可以正常工作了
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo) {
if (changeInfo.status === 'complete') {
chrome.tabs.executeScript(tabId, {
chrome.notifications.create(‘id1’, {
type: ‘basic’,
iconUrl: ‘icon.png’,
title: ‘Review Baed Surfing’,
message: ‘Check URL now by clicking FAKE URL icon infront on Address Bar!’,
priority: 0
},
function() { /* Error checking goes here */}
);
});
}
});
谢谢,我认为您只需要在后台脚本中调用chrome.notifications.create(),chrome.tabs.executeScript只接受要注入的文件url或代码(css、js) 当通知未按预期显示时,您可以检查以下项目:
"permissions": ["notifications"]
var opt = {
type: "basic",
title: "Primary Title",
message: "Primary message to display",
iconUrl: "url_to_small_icon"
}
希望这有帮助。您在控制台中看到任何错误吗?请检查背景页上的控制台。这些不是正确的引号字符。但不确定这是否是复制粘贴错误<代码>类型:“basic”应该是
类型:“basic”
控制台在“chrome.notifications.create”附近显示错误(“错误是,未捕获的语法错误:意外标记。@特洛伊木马程序请正确写入您的引号。使用“”