简体   繁体   中英

Chrome Extension get UI element from background script

I am starting to develop a chrome extension. In my case I have the following manifest.json:

{
    "name": "mY Notifier",
    "version": "1.0",
    "manifest_version": 2,
    "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    },
    "background": {
        "scripts": [
            "myscript.js"
        ],
        "persistent": false
    },
    "permissions": [
        "tabs","alarms"
    ]
}

So, as you can see i have a default popup html page and a background script.

I need a Background script because I need to update the icon badge from time to time when the Extension is enabled (not just if the user clicks on the icon). This works fine.

In my popup.html i have a couple of checkboxes. Now in my background script i want to check which of the checkboxes are enabled. How can i do this? I cannot refer to my ui elements in popup.html using elementById. How can this be done?

You can't access to popup.html from myscript.js because popup.html don't loaded before the click on a button. You can call functon of myscript.js from popup.html :

popup.js code:

var bg = chrome.extension.getBackgroundPage();
bg.someFunction(stateOfCkeckboxes);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM