JavaScript Keep Chrome Extension running on page reload keep chrome extension open,keep chrome extension corrupted,keep chrome

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?

Answer:1

I was working with object properties in javascript and I got stuck with a problem that I couldn't find out. In this javascript program I have created object properties for name and age and I have ...

I was working with object properties in javascript and I got stuck with a problem that I couldn't find out. In this javascript program I have created object properties for name and age and I have ...

  1. object defineproperty not working
  2. object fit property not working in ie
  3. object fit property not working

If I manually click on button1 and then on button2 I get the whole html code (410 KiB). However If I try to automate this process with the code below, I only get a part of it, without the html ...

If I manually click on button1 and then on button2 I get the whole html code (410 KiB). However If I try to automate this process with the code below, I only get a part of it, without the html ...

  1. html download generated file
  2. download generated html

I learned that a for iterates on each the array's element, even if a pointed case doesn't exist, while a forEach skips the unexisting cases. Moreover, when a for is writen, a counter must be ...

I learned that a for iterates on each the array's element, even if a pointed case doesn't exist, while a forEach skips the unexisting cases. Moreover, when a for is writen, a counter must be ...

  1. difference between foreach and map
  2. difference between foreach and for loop
  3. difference between foreach and iterator
  4. difference between foreach and for loop javascript
  5. difference between foreach and map javascript
  6. difference between foreach and foreachordered
  7. difference between foreach and for
  8. difference between foreach and foreachremaining
  9. difference between foreach and stream
  10. difference between foreach and enhanced for loop
  11. difference between foreach and filter
  12. difference between foreach and while loop
  13. difference between foreach and for javascript
  14. difference between foreach and for loop c#
  15. difference between foreach and every
  16. difference between foreach and foreachpartition
  17. difference between foreach and map js
  18. difference between foreach and map scala
  19. difference between foreach and forvalues stata
  20. difference between foreach and each

Newly into the world of development so please be patient if i lack xp. I need to put 5 specific colors (I'm assuming using an array and shuffle function) in my 5 circles randomly. HTML <div id="...

Newly into the world of development so please be patient if i lack xp. I need to put 5 specific colors (I'm assuming using an array and shuffle function) in my 5 circles randomly. HTML <div id="...

  1. shuffle colors array