admin管理员组文章数量:1279120
I'm trying to build a simple chrome extension that inserts/hides a div when the browser action (extension icon) is toggled. I've got the basics working but I would like the extension to remain in it's 'on-state' (i.e div inserted) when the page reloads. It should only be removed when toggled off.
Currently on each reload everything resets.
Here's what I have so far:
manifest.json
{
"name": "My Extension",
"version": "0.0.1",
"manifest_version": 2,
"background": {
"scripts": ["background.js"],
"persistent": false
},
"browser_action": {
"default_icon": "icon-off.png"
},
"permissions": [
"tabs"
]
}
background.js
toggle = false;
chrome.browserAction.onClicked.addListener(function(tab) {
toggle = !toggle;
var status = 'off';
if(toggle) {
status = 'on';
}
// Toggle the icon
chrome.browserAction.setIcon({path: 'icon-'+status+'.png', tabId:tab.id});
// Execute script & pass a variable
chrome.tabs.executeScript(tab.id, {
code: 'var extension_status = "'+status+'";'
}, function() {
chrome.tabs.executeScript(tab.id, {file: 'inject.js'});
});
});
inject.js
// ID for inserted element
var my_div_id = 'foo';
// The div (returns null if doesn't exist)
var my_div = document.getElementById(my_div_id);
// If on for first time
if( extension_status == 'on' && !my_div ) {
// Create div
var div = document.createElement('div');
div.id = my_div_id;
div.textContent = 'hello';
// Insert into page
document.body.appendChild(div);
}
// When toggled off hide
else if( extension_status == 'off' ) {
my_div.style.display = 'none';
}
// When Toggled back on again show again
else {
my_div.style.display = 'block';
}
Do I need to pass a value back to my background.js file? or is there a better way to handle this?
I'm trying to build a simple chrome extension that inserts/hides a div when the browser action (extension icon) is toggled. I've got the basics working but I would like the extension to remain in it's 'on-state' (i.e div inserted) when the page reloads. It should only be removed when toggled off.
Currently on each reload everything resets.
Here's what I have so far:
manifest.json
{
"name": "My Extension",
"version": "0.0.1",
"manifest_version": 2,
"background": {
"scripts": ["background.js"],
"persistent": false
},
"browser_action": {
"default_icon": "icon-off.png"
},
"permissions": [
"tabs"
]
}
background.js
toggle = false;
chrome.browserAction.onClicked.addListener(function(tab) {
toggle = !toggle;
var status = 'off';
if(toggle) {
status = 'on';
}
// Toggle the icon
chrome.browserAction.setIcon({path: 'icon-'+status+'.png', tabId:tab.id});
// Execute script & pass a variable
chrome.tabs.executeScript(tab.id, {
code: 'var extension_status = "'+status+'";'
}, function() {
chrome.tabs.executeScript(tab.id, {file: 'inject.js'});
});
});
inject.js
// ID for inserted element
var my_div_id = 'foo';
// The div (returns null if doesn't exist)
var my_div = document.getElementById(my_div_id);
// If on for first time
if( extension_status == 'on' && !my_div ) {
// Create div
var div = document.createElement('div');
div.id = my_div_id;
div.textContent = 'hello';
// Insert into page
document.body.appendChild(div);
}
// When toggled off hide
else if( extension_status == 'off' ) {
my_div.style.display = 'none';
}
// When Toggled back on again show again
else {
my_div.style.display = 'block';
}
Do I need to pass a value back to my background.js file? or is there a better way to handle this?
Share Improve this question edited Oct 7, 2016 at 6:35 Xan 77.6k18 gold badges197 silver badges217 bronze badges asked Oct 4, 2016 at 3:22 shanomurphyshanomurphy 7066 silver badges9 bronze badges 1- Among other things, right now your status in thew background is not per-tab (even if the icon you set is per tab, and content script state is, by necessity, per tab). Think about it for a start. – Xan Commented Oct 4, 2016 at 8:17
2 Answers
Reset to default 8I did eventually figure out that I needed to add a listener for chrome.tabs.onUpdated as @ViewSource's excellent answer also stated. Here's the final working example:
manifest.json
{
"name": "My Extension",
"version": "0.0.1",
"manifest_version": 2,
"background": {
"scripts": ["background.js"],
"persistent": false
},
"browser_action": {
"default_icon": "icons/icon-off.png"
}
"permissions": [
"activeTab",
"<all_urls>"
]
}
background.js
var toggle = false;
var status = 'off';
var the_tab_id = '';
function set_status() {
toggle = !toggle;
status = 'off';
if(toggle) { status = 'on'; }
}
function toggle_extension(tab){
// Set icon
chrome.browserAction.setIcon({ path: 'icons/icon-'+status+'.png', tabId:tab.id });
// Pass variable & execute script
chrome.tabs.executeScript({ code: 'var extension_status = "'+status+'"' });
chrome.tabs.executeScript({ file: 'inject.js' });
// Set the tab id
the_tab_id = tab.id;
}
function my_listener(tabId, changeInfo, tab) {
// If updated tab matches this one
if (changeInfo.status == "plete" && tabId == the_tab_id && status == 'on') {
toggle_extension(tab);
}
}
chrome.browserAction.onClicked.addListener(function(tab) {
set_status();
toggle_extension(tab);
});
chrome.tabs.onUpdated.addListener(my_listener);
inject.js
if( extension_status == 'on' ) {
// do stuff
}
I had the same problame myself.
After the page reload, you need to toggle the extension again, as you did when the user push the browserAction button, just without the - toggle = !toggle;
because the user didn't change the extension's state.
So, how do you know when the tab reload? using tabs.onUpdated
Your new background page should look like this:
var toggle = false;
var status = 'off';
chrome.browserAction.onClicked.addListener(function(tab) {
set_status();
toggle_extansion()
});
//add listener
chrome.tabs.onUpdated.addListener(your_listener);
function your_listener(tabId, changeInfo, tab) {
//Check that the extension should work on the updated tab
if (tab.url.search("://www.thesite.") >-1){
//toggle the extansion as you already did
toggle_extansion();
}
}
function toggle_extansion(){
// Toggle the icon
chrome.browserAction.setIcon({path: 'icon-'+status+'.png', tabId:tab.id});
// Execute script & pass a variable
chrome.tabs.executeScript(tab.id, {
code: 'var extension_status = "'+status+'";'
}, function() {
chrome.tabs.executeScript(tab.id, {file: 'inject.js'});
});
}
function set_status(){
toggle = !toggle;
if(toggle) {
status = 'on';
}
}
Note: There is no easy way to listen only to tabs containig the matches sites in your extension's manifest.
本文标签: javascriptKeep Chrome Extension running on page reloadStack Overflow
版权声明:本文标题:javascript - Keep Chrome Extension running on page reload - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741242986a2364343.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论