I don't see use for background.js or background.html just yet. It seems like just a middle man. I can easily municate with all my content scripts by using the following code in popup.html/popup.js (browser action):
if (e.target.id === "blue"){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, {"message": "blue" });
});
}
then on my content.js :
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if( request.message === "blue" ) {
document.body.style.backgroundColor="blue";
console.log("Content: Changing to blue");
}
if( request.message === "black" ) {
document.body.style.backgroundColor="black";
}
});
P.S Hope this helps someone out, I already have a working extension ! :D
I don't see use for background.js or background.html just yet. It seems like just a middle man. I can easily municate with all my content scripts by using the following code in popup.html/popup.js (browser action):
if (e.target.id === "blue"){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, {"message": "blue" });
});
}
then on my content.js :
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if( request.message === "blue" ) {
document.body.style.backgroundColor="blue";
console.log("Content: Changing to blue");
}
if( request.message === "black" ) {
document.body.style.backgroundColor="black";
}
});
P.S Hope this helps someone out, I already have a working extension ! :D
Share Improve this question asked Jan 11, 2016 at 0:42 ritchieritchie 4551 gold badge4 silver badges13 bronze badges 5- I ran into this the other day.. I think maybe it could have special permissions for persisting data or something? I would like to know. – gegillam Commented Jan 11, 2016 at 0:53
- @JaromandaX I wanted to know what the purpose of background.js is ? It seems like just a middle man. – ritchie Commented Jan 11, 2016 at 0:59
- @gegillam I think it's for managing scripts I don't know. I was able to do fine with just municating with browser action and content scripts didn't really see a need for background.js. – ritchie Commented Jan 11, 2016 at 1:00
- @JaromandaX yes , many times. I usually read alot before asking questions on here. – ritchie Commented Jan 11, 2016 at 1:08
- @JaromandaX you're suppose to be helpful not criticize those who are trying to learn. Please don't bother responding. – ritchie Commented Jan 11, 2016 at 1:14
2 Answers
Reset to default 2a file named background.js
has no significance in chrome extensions, you must be asking about Background Pages or Event Pages
These pages do not have to have any particular name, they can be called fred.js
or wilma.js
- their "special purpose" is only realised if one adds them to the extension through the chrome extension manifest like so:
// background page
"background": {
"scripts": ["fred.js"]
},
// event page
"background": {
"scripts": ["wilma.js"],
"persistent": false
},
The functionality offered by and use case for such pages is in the above linked documentation pages
From the docs
Extensions are event based programs used to modify or enhance the Chrome browsing experience. Events are browser triggers, such as navigating to a new page, removing a bookmark, or closing a tab. Extensions monitor these events in their background script, then react with specified instructions.