0

Firstly, yes i googled the problem (for 4 hours yesterday), and the answers didnt help in my case ;)

Im creating a browser extension and i want to have a little popup to manage the Settings of the Addon. The popup.html has a script tag on popup.js, in which i write the values to the local storage: (in this i write the value of a checkbox)

chrome.storage.local.set({enabled: checked}, function() {
        console.log('Value is set to ' + checked);
});

Problem 1: If i write it to storage like that it says its written, but i cant find the value in the debugger. I can find a value if i set it using "localStorage.setItem"

Going on, in my content script i obviously want to read out the value, and here i cant read it with chrome.storage.local.get NOR with localStorage.getItem.

If i do a console.log in my content script, it outputs to the console of the website im visiting and not in the addon popups console. Shouldnt the content script be in the addon scope too?

How can i simply exchange values between my content script and my settings popup?

Here is my manifest.json:

{
    "name": "test",
    "version": "0.1",
    "manifest_version": 2,
    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "run_at": "document_start",
            "js": ["randomize.js"]
        }
    ],
    
    "browser_action": {
        "default_popup": "popup.html",
        "default_title": "Settings"
    },

    "options_ui": {
        "page": "popup.html"
    },

    "permissions": ["storage"]
}
H. Beck
  • 11
  • 6
  • 1) The popup is a separate page displayed in that small window, which is why it has its own console. 2) The content scripts run in the web page so they use the web page's console. 3) In the content script you should use chrome.storage.local.get as shown in any example, there are plenty so maybe there's a problem in the way you're using it. Show your code in content script. 4) [Inspect extension's chrome.storage in devtools](https://stackoverflow.com/a/32471596) – wOxxOm Feb 17 '21 at 16:23
  • ok, with the addon you mentioned i was able to inspect chrome.storage.local too :) and my popup sets the value correctly, so its just a problem with reading the value in content script. i try to read it like that: chrome.storage.local.get(['enabled'], function(enabled) { console.log('Value currently is ' + enabled.key); }); Console says the value is empty, so i looked into storage explorer, and saw that the actual open page has not access to chrome.storage, but the permission is in my manifest, so i dont understand why... – H. Beck Feb 17 '21 at 16:36
  • You can use devtools to set breakpoints (in the callback) and debug your code so you can see the contents of variables. You will see that `enabled.key` should be `enabled.enabled`. – wOxxOm Feb 17 '21 at 16:37
  • ah right, then i got that wrong, now it looks better in the console :) One thing is a bit confusing, the storage-explorer-addon on the actual page doesnt show chrome.storage (only window.localstorage) but seems like the content script can read it now anyways. Thank you very much! – H. Beck Feb 17 '21 at 16:41
  • Next Problem, when i want to write it to a local variable and work with the value, then it says its undefined :/ – H. Beck Feb 17 '21 at 17:10
  • Probably this: [Returning Chrome storage API value without function](https://stackoverflow.com/q/38425751) – wOxxOm Feb 17 '21 at 17:33
  • *gulp*, that is a pretty big overhead to work with a value read from storage. but ok, ill try to work through this, thank you :) – H. Beck Feb 17 '21 at 17:54

0 Answers0