Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep Chrome Extension running on page reload

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?

like image 912
shanomurphy Avatar asked Oct 04 '16 03:10

shanomurphy


2 Answers

I 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 == "complete" && 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
}
like image 174
shanomurphy Avatar answered Oct 10 '22 04:10

shanomurphy


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.com") >-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.

like image 39
ViewSource Avatar answered Oct 10 '22 04:10

ViewSource