cdoern
cdoern

Reputation: 97

Chrome Extension background script not running when published

I have been working on a chrome extension for some time now.

I have a background JS file which is not persistent. While testing the extension locally the background.js file was properly printing results to the console and firing when necessary.

However, once uploaded, it seems that background.js no longer executes when it is supposed to and does not print anything to the console.

Here is the background.js code:

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) ;
                  })
              });
            }
            })
        }
      }
  });
  })
  }); 
  });
    

and here is my manifest:

{
    "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
  }

Upvotes: 0

Views: 1284

Answers (1)

cdoern
cdoern

Reputation: 97

As stated in the comments, the issue was the fact that I left the .onInstalled around the two listeners for tabs. Moving these listeners into a global context fixed the problem, the .onInstalled isn't necessary for my extension

Upvotes: 2

Related Questions