Chrome 扩展程序后台脚本在发布时未运行

cdorn

一段时间以来,我一直在研究 chrome 扩展。

我有一个不持久的后台 JS 文件。在本地测试扩展时,background.js 文件正确地将结果打印到控制台并在必要时触发。

但是,一旦上传,background.js 似乎不再按预期执行并且不会向控制台打印任何内容。

这是 background.js 代码:

chrome.runtime.onInstalled.addListener(function() {
 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){ // listener for when first loading the page
   var url = tab.url;
   console.log(url);
   if(url != undefined && changeInfo.status == "complete"){ // if the page is done loading, this is done bc .onUpdated has issues with multiple misfires
     console.log(url);
     var sites = [];
     chrome.storage.sync.get({list:[]}, function(data){ // retrieve list of blackListed sites
      sites = data.list;
      console.log(sites);
     for(var i = 0; i < sites.length; i++){
      if(tab.url.includes(sites[i])){
        chrome.storage.sync.get(['time'], function(data){
          if(data.time != null){ // if we are in an active session
          chrome.storage.sync.get(['userCausedHours'], function(data2) {
              console.log(data2.userCausedHours)
              chrome.storage.sync.set({'userCausedHours': data2.userCausedHours + 1}, function() {
                  console.log('hours = ' + data2.userCausedHours + 1) ;
                })
            });
          }
          })
        }
      }
   })
}
});
  chrome.tabs.onActivated.addListener(function(activeInfo) { // listener for when flipping back and forth between pages
    var sites = [];
        chrome.storage.sync.get({list:[]}, function(data){ // retrieve list of blackListed sites
              sites = data.list;
              console.log(sites);
  // how to fetch tab url using activeInfo.tabid
  chrome.tabs.get(activeInfo.tabId, function(tab){
      console.log(tab.url);
      for(var i = 0; i < sites.length; i++){
        if(tab.url.includes(sites[i])){
          chrome.storage.sync.get(['time'], function(data){
            if(data.time != null){ // if we are in an active session
            chrome.storage.sync.get(['userCausedHours'], function(data2) {
                console.log(data2.userCausedHours)
                chrome.storage.sync.set({'userCausedHours': data2.userCausedHours + 1}, function() {
                    console.log('hours = ' + data2.userCausedHours + 1) ;
                  })
              });
            }
            })
        }
      }
  });
  })
  }); 
  });
    

这是我的清单:

{
    "name": "Serenity",
    "version": "1.01",
    "description": "Productivity made fun",
    "permissions" :["tabs","storage"],
    "background" :{
        "scripts": ["background.js"],
        "persistent": false

    },
    "options_page": "options.html",
    "browser_action": {
      "default_popup": "popup.html",
      "default_icon": {
          "16": "images2/favicon-16x16.png",
          "32": "images2/favicon-32x32.png",
          "128": "images2/favicon-128.png"
        }

  },
    "icons": {
      "16": "images2/favicon-16x16.png",
      "32": "images2/favicon-32x32.png",
      "128": "images2/favicon-128.png"
      },
    "manifest_version": 2
  }
cdorn

正如评论中所述,问题在于我将 .onInstalled 留在了两个侦听器周围以供选项卡使用。将这些侦听器移动到全局上下文中解决了问题,我的扩展不需要 .onInstalled

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Chrome扩展程序中动态运行后台脚本?

Chrome扩展程序后台脚本中的AngularJS

确切地说,Chrome扩展程序中的后台脚本何时开始运行?

Chrome扩展程序:Background.html中的iframe导致后台脚本调整运行2倍

Chrome扩展程序:后台脚本仅在第一次正常运行

即使Chrome未运行,如何运行Chrome Hangouts扩展程序?

在Chrome扩展程序后台脚本上接收CORS策略

Chrome扩展程序后台页面和内容脚本同步

Chrome扩展程序:后台脚本捕获网络和HTTP错误

Chrome扩展程序:如何使后台等待执行脚本?

Chrome扩展程序:仅在刷新后,后台脚本才会启动

Chrome邮件扩展程序:从注入脚本到后台

Chrome扩展程序未在文档开始时运行脚本并且未打印内容

使用ReactJS构建时,Chrome扩展程序内容脚本未注入DOM

从chrome扩展程序发布数据

Chrome扩展程序内容脚本未触发代码

未注入 Chrome 扩展程序简单脚本

Chrome扩展程序的内容脚本未注入特定网站

Chrome扩展程序:发布时没有定价选项

Chrome扩展程序脚本仅在显示弹出窗口时运行

Chrome扩展程序内容脚本永远不会运行

我可以将网页中的消息直接发布到扩展程序的后台脚本中,而不添加Chrome中的content-script吗?

Chrome扩展程序中的内容脚本和后台脚本到底是什么?

在页面加载时以chrome扩展名运行js脚本

Chrome扩展程序:加载所有其他页面脚本后运行扩展程序的脚本

用户脚本可在Tampermonkey中使用,但在Chrome中作为未打包的扩展程序加载时无法使用

Chrome通知未从Chrome扩展程序后台页面显示

无法在Chrome扩展程序中从后台页面向内容脚本发送消息

Chrome扩展程序后台页面以编程方式注入的内容脚本多次注入