Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background Scripts vs Content Scripts in Chrome Extensions

I read about Background page and Content scripts at developer.chrome.com but I'm confused with them, I cannot understand when to use background scripts and when to use content scripts. For example:

manifest.json:

{     "name": "Hello World",     "version": "2.0",     "manifest_version": 2,     "background":      {         "scripts": ["background.js"]     },     "content_scripts":     [         {             "matches": ["http://*/*", "https://*/*"],             "js": ["js/myScript.js"]         }     ],     "permissions": ["tabs", "http://*/*"],     "browser_action":     {         "default_icon": "icon.png"     } } 

If background.js is:

// Called when the user clicks on the browser action. chrome.browserAction.onClicked.addListener(function(tab) {   alert("test"); }); 

It works well, but if I put the same code above in myScript.js, it doesn't work.

So I don't know which script should be located in background.js, and which should be located in content scripts.

like image 806
Dark Light Avatar asked Oct 19 '12 10:10

Dark Light


People also ask

What are background scripts?

Background scripts are the place to put code that needs to maintain long-term state, or perform long-term operations, independently of the lifetime of any particular web pages or browser windows.

What does background js do in Chrome extension?

js') is a JavaScript script that runs once our extension either gets installed or the user refreshes the extension manually. The background script doesn't actually have access to any of the webpages your user is viewing, so your background script can't manipulate the DOM.

Do Chrome extensions run in background?

Depending on the extensions you have installed, sometimes Google Chrome will continue to run in the background on your computer after closing it. You might notice this, especially after setting up a new computer and when you install a fresh version of the browser.


2 Answers

Actually, Content scripts are JavaScript files that run in the context of web pages. By using the standard Document Object Model (DOM), they can read details of the web pages the browser visits, or make changes to them.

A common need for extensions is to have a single long-running script to manage some task or state. Background pages to the rescue.The background page is an HTML page that runs in the extension process. It exists for the lifetime of your extension, and only one instance of it at a time is active.

like image 200
The Alpha Avatar answered Sep 21 '22 08:09

The Alpha


I know this question was ages ago, but I recently got into Chrome extension development and had the exact question as you. So I hope my answer will shed some light on this confusion.

You have specified myScript.js as your content script. The reason that you code didn't work when put inside the content script is because it needs to listen for the browser button click event. However, content script only has limited access to Chrome api, mostly chrome.runtime event. It can't detect chrome.browserAction.onClick event.

The background script, on the other hand, has access to a full array of Chrome apis.

When to use background script as opposed to content script depends on your extension goal. Do you want to simply change the presentation of a web page? Then you only need content script, not background script. For example, if you want to make an extension to toggle dark mode on any web page, you can do without background script.

What if you want to save your user preferences for a list of sites on which your extension will automatically toggle dark mode? Then add a background script that: - stores their preferences to Chrome storage.

  • detect when the user has landed on a site in the list
  • send a message to the content script to instruct it to toggle the dark mode.

That's not the best example, but my point is background script is needed when you need to process common functions and persist user experience.

like image 25
bytrangle Avatar answered Sep 18 '22 08:09

bytrangle