弹出窗口关闭时运行chrome扩展进程

时间:2018-05-15 07:38:03

标签: javascript google-chrome google-chrome-extension

基本上我正在尝试为Chrome创建一个自动访问者扩展程序,以便在特定时间后打开网站的网址。当我打开弹出窗口时一切正常,但弹出窗口关闭时无效。我试图找到一种方法来运行自动访问者扩展,即使弹出窗口已经关闭我已经在Stack Overflow上阅读了关于这种现象的多个问题,但没有一个澄清我在寻找什么。

这是我的清单文件:

的manifest.json

{
  "manifest_version": 2,
  "name": "Auto Visitor",
  "description": "This extension will visit multiple pages you saved in extension",
  "version": "1.0",

  "browser_action": {
   "default_icon": "icon.png",
   "default_popup": "popup.html"
  },
  "background": {
    "scripts": [
      "background.js"
    ],
    "persistent": false
  },
  "permissions": [
   "activeTab",
   "storage",
   "tabs",
   "http://*/",
   "https://*/"
   ]
}

即使弹出窗口关闭,我想要运行的后台文件:

background.js

// this will work only when you denie the background script in manifest
chrome.runtime.onInstalled.addListener(function(details) {
    var initTime = 5;
    chrome.storage.local.set({"avtime": initTime}, function() {});
});
reloadMainTab();
function reloadMainTab() {
 chrome.storage.local.get('avurl', function (result) {
      var urlsToLoad = result.avurl;
      console.log(urlsToLoad);
      if(urlsToLoad==undefined){
        // do nothing
      }else{
          var urlsArr = urlsToLoad.split(",");
              chrome.storage.local.get('avtime', function (result) {
                  var thisTime = result.avtime;
                  /*
                   setting it to -1 because it gets incremented by 1 
                   when it goes into getSelected method
                  */
                  var index=-1;
                  setInterval(function(){
                    if(index < urlsArr.length-1){ 
                      chrome.tabs.getSelected(function (tab) {
                        // console.log('index in get selected'+index)
                        chrome.tabs.update(tab.id,{url: urlsArr[index]});
                      });
                      index++;
                    }else{
                      index=-1;
                    }  
                  }, thisTime+"000");

              });
      }
  });
}

任何帮助都会非常感激

0 个答案:

没有答案