3

I would like to know how to run the background script of a Chrome extension for only specific/specified domain/s please?

For example, if an extension is meant to run only on pages of Google.com, so there is no reason to keep the background script running on any other domains.

In my manifest file I have set "matches" but I can still see the background script running on every domain and tab.

...

"background": {
    "matches": [
        "*://*.google.com/*"
    ],
    "scripts": ["scripts/background.js"],
    "persistent": false
},

"content_scripts": [
    {
        "matches": [
            "*://*.google.com/*"
        ],
        "css": ["styles/default.css"],
        "js": ["scripts/jquery.js", "scripts/default.js"]
    }
],

"permissions": [
    "storage",
    "declarativeContent",
    "tabs"
],

...

Edit
To add more info that will help understand my goal:
I have a content script and a popup, the popup is being used as a remote control with options to choose from and play with, that will eventually effects the page.
When changing an option in the popup, it sends it's value to the background/event script, there it being temporary saved in a variable, and then being sent to the content script, where it actually being executing and show up on the page to the user.
And I want that only when the user leaves the specific domain, the background/event script will save the settings to storage, so by that there will be only a single storage saving task and NOT each time the user is changing a setting in the popup.
After the settings got saved to storage and the user left, I want nothing to run it the background anymore please.

Gil Goldshlager
  • 651
  • 1
  • 8
  • 22

1 Answers1

3

You are misunderstanding what the background page is for, and what "persistent": false means.

The background page does not run "for" any domain; it just runs. A single copy per extension. Have a look at the Architecture Overview.

However, if you are concerned that it consumes resources, you add "persistent": false to the manifest. This makes it an Event page, that is unloaded when it's not doing work.

If your event page is woken up only by content scripts, then you have achieved your goal: it won't be running when it's not needed.

It's entirely up to you to properly construct the background page so it's idle when you don't need it. Since you haven't told what it's supposed to be doing - well..

Do read the Event page documentation, there are important restrictions you need to understand.

Xan
  • 74,770
  • 16
  • 179
  • 206
  • thank you once again for the help, I edited my post (below the manifest code block) and added additional info with the goal I'm trying to achieve, please let me know what you think and how it should be done. – Gil Goldshlager Mar 15 '15 at 13:49
  • You don't have to use a background page at all. Use `chrome.storage` API that is available for both content script and popup, and the popup can directly message a content script. – Xan Mar 15 '15 at 13:50
  • I did just that before, and found about the per-minute save limit. when the user leaves the domain, then it should be the only time to save the changes to storage, isn't it? – Gil Goldshlager Mar 15 '15 at 14:02
  • Per minute save limit only applies to `chrome.storage.sync` area. You can do as many writes as you like to/from `local`. – Xan Mar 15 '15 at 14:05
  • Ohh...good to know that! but then how do I know when to save it to the sync storage? and anyway think that I got a popup with a range input where values are from 0 to 100, if I'm dragging the range bar from the min to the max it will perfome 100 savings, that can't be good, and if I want to only save when the popup is being close then from my understanding I must use a background script to that and listen to the on disconnect call, can I do that from the content-script too please? – Gil Goldshlager Mar 15 '15 at 14:11
  • We're stretching the discussion that's appropriate for comments; you probably should make a separate question about it. – Xan Mar 15 '15 at 14:14
  • Thanks I really appreciate your time, posted a new separate question here: http://stackoverflow.com/questions/29062657/handling-storage-in-chrome-extension – Gil Goldshlager Mar 15 '15 at 15:53
  • The meat of @Xan's answer is still correct, but it might be useful to note that the concept of Event pages has been melded with Background pages, at least in the official docs. If you set "persistent": false on your background page, you have what was previously referred to as an "Event page". – sloreti May 13 '20 at 20:24