6
votes

My chrome extension background script is not being loaded. I followed Googles guide for them but still nothing. I'm not sure if there is another way to check but it isn't in Inspect Element and what the script should be doing isn't happening.

http://developer.chrome.com/extensions/background_pages.html

manifest.json file

{
"manifest_version": 2,

"name": "WebDevFriend",
"description": "blah blah blah",
"version": "1.0",

"permissions": [
    "bookmarks",
    "tabs",
    "http://*/*"    ],  

"background": {
    "scripts": ["js/settings.js"],
},

"browser_action": {
    "default_icon": "images/icon.png",
    "default_popup": "html/popup.html"
}
}

settings.js file

chrome.windows.onCreated.addListener(function(window){
  chrome.windows.getAll(function(windows){
      var length = windows.length;
      if (length == 2) {
          chrome.tabs.executeScript(null, {file: "content_script.js"});
      }
  });
});
document.write('hello');
1
If you go to the extensions page in developer mode there is a link called _generated_background_page.html. Clicking on that will allow you to view the background page in devtools. - BeardFist
Another thing is you should replace document.write('hello'); to console.log('hello');. Because this is a "background page", not a "foreground page", so you dont see anything from document.write(). - Jacob Dam
after changing background scripts, use chrome to "Load unpacked" from extensions menu, to apply changes in background scripts.And please use alert() function instead of document.write() to check if background script is working. - AmirHossein Rezaei

1 Answers

5
votes

First you can't view the background page like a regular html page. The only way is to view its contents with the Chrome Developer Tools.

enter image description here

just click on generated_background_page.html in the extensions section of your browser.

Second use the console.log() statement to log messages.