Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Chrome Extension - background script

Tags:

After messing around with Chrome Extension I noticed that when you are on the chrome://extensions page a background script initiated in the manifest file will run where as if you are just browsing the internet or on another other page besides the extension page the background script will not run.

Here is what I mean:

In my Manifest file:

"background": {     "scripts": ["jquery-latest.js","background.js"]   }, 

Now in the background.js file:

$(document).ready(function(){     alert("working"); }); 

I use a simple alert function to see if this will work and found out that alert("working"); only gets displayed when I am on the chrome://extension directory. If I go to google.com or something of that sort, no cigar.

My question lies in, why does this happen? How do I change it so it does alert no matter what.

like image 486
David Biga Avatar asked Jul 08 '13 20:07

David Biga


People also ask

How do I put background script extensions in Chrome?

The background script should be viewed as "running in the background of the Chrome browser". Your desired effect (running a script for every page) is actually a task for content scripts. To learn more, read https://developer.chrome.com/extensions/overview.html#arch.

What is a background script?

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 is Chrome background page extension?

Background pages are implicit pages which contain background scripts. A background script is a single long-running script to manage some task or state. It exists for the lifetime of your extension, and only one instance of it at a time is active.

Can Chrome Extensions run in the background?

Chrome 10 Now Lets Extensions Run in the Background.


1 Answers

The background script should be viewed as "running in the background of the Chrome browser".
Your desired effect (running a script for every page) is actually a task for content scripts.

To learn more, read https://developer.chrome.com/extensions/overview.html#arch.

like image 89
Rob W Avatar answered Sep 22 '22 04:09

Rob W