Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在chrome extensions中加载新页面时显示通知_Javascript_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript 在chrome extensions中加载新页面时显示通知

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

我正在google chrome中开发一个扩展。我想在当前活动选项卡中加载任何新页面时显示桌面通知

但是我写的代码不能正常工作

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)

当通知未按预期显示时,您可以检查以下项目:

  • 将“通知”添加到manifest.json中的权限

    "permissions": ["notifications"]
    
  • 通知API目前仅支持Windows、Chrome OS和Mac

  • create函数的“option”参数必须包括通知标题、消息和iconUrl。如果您错过了其中任何一个,rich通知将无法正常工作而不会出现任何错误

    var opt = {
        type: "basic",
        title: "Primary Title",
        message: "Primary message to display",
        iconUrl: "url_to_small_icon"
    }
    

  • 希望这有帮助。

    您在控制台中看到任何错误吗?请检查背景页上的控制台。这些不是正确的引号字符。但不确定这是否是复制粘贴错误<代码>类型:“basic”应该是
    类型:“basic”
    控制台在“chrome.notifications.create”附近显示错误(“错误是,未捕获的语法错误:意外标记。@特洛伊木马程序请正确写入您的引号。使用“”