My chrome extension consists from background.js
and content.js
which I launch from background.js
. I have two icons that show if the extension is on or off and for that I use the following script in background.js
based on this example:
var toggle = false;
chrome.browserAction.onClicked.addListener(function(tab) {
var config = {allPages: []};
toggle = !toggle;
if(toggle){
chrome.browserAction.setIcon({path: "on.png"});
chrome.tabs.executeScript(null, {
code: 'var config = ' + JSON.stringify(config)
}, function() {
chrome.tabs.executeScript(null, { file: "jquery.js" }, function() {
chrome.tabs.executeScript(null, { file: "content.js" });
});
});
console.log('active ');
}
else{
chrome.browserAction.setIcon({path: "off.png"});
chrome.tabs.executeScript(null, {code:"console.log('not active')"});
}
});
The icon toggling works fine, but script executing doesn't. The content script works even if the icon shows that the extension is off.
My code in content.js
is the following:
$(document).ready(function(){
var url = document.location.href;
var pageModel = {
url: url
};
config.allPages.push(pageModel);
chrome.storage.local.set({'config': config}, function () {
console.log('savedPageModelNew: ', config);
});
});
How can I toggle not only icons but script executing also? To be frank my script in content.js
is rather huge and I can't put it whole here. What I need is the content script that works when the extension is on and "pauses" when the extension is off. Maybe someone has an answer?
I really hope so. Thanks a lot in advance!
P.S. Here is also my manifest.json:
{
"manifest_version": 2,
"name": "ChromeExt",
"version": "1.0",
"permissions": [
"activeTab",
"tabs",
"<all_urls>",
"contextMenus",
"storage",
"https://*/*", "http://*/*"
],
"browser_action": {
"default_title": "Inactive.",
"default_icon": "off.png"
},
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["http://*///*//", "https://*/*"],
"css": ["mainStyles.css"]
}
]
}