1

I'm trying to create a feature for an extension that has a checkbox, if the checkbox is checked the extension will stop collecting data, and if the checkbox is unchecked the extension will collect data. The feature works when the extension popup is open, and when I'm using the devtool. When the popup is closed and the checkbox is unchecked, it doesn't collect any data

I store the checkbox status in localstorage:

chrome.storage.local.set(
    {
      Privacy: document.getElementById("Privacy").checked,
    }

I call function to collect data if the checkbox is uncheck:

chrome.tabs.onActivated.addListener(function (activeInfo) {
  chrome.storage.local.get(["Privacy"], function (result) {
    if (result.Privacy != true || result.Privacy == null) {
      getTabInfo((activeTabId = activeInfo.tabId));
      console.log("inside privacy");
    } else {
      console.log("inside tabs.onActivated:");
      console.log("this is result.Privacy: ");
      console.log(result.Privacy);
    }
  });
})

I have tried : result.Privacy == false, that doesn't work either. I know its not collecting data because I don't see any data stored in the database. This is a sample of my manifest.json:

"background": 
{
    "scripts": ["js/background.js", "js/firebase.js"],
    "persistent": true
},


"permissions": [
        "tabs",
        "activeTab",
        "scripting",
        "*://*/*",
        "storage",
        "identity"
    ],
Suji
  • 767
  • 3
  • 12
  • 28
  • 1) The popup's scripts run only when the popup is shown. 2) `chrome.storage.local` is not `localStorage` so you're looking in the wrong place, see [Inspect extension's chrome.storage in devtools](https://stackoverflow.com/q/32435399) – wOxxOm Sep 14 '21 at 16:09
  • use chrome.storage.sync instead – Dean Van Greunen Sep 14 '21 at 16:26
  • I changed it to chrome.storage.sync and still doesn't work. my extension only collects data when the popup is open – Suji Sep 15 '21 at 01:14
  • So chrome.tabs.onActivated is running in the background.js ? chrome.storage.local.set in popup.js / html? Will console.log(result) say something? – Johan Hoeksma Sep 15 '21 at 14:46
  • @JohanHoeksma there is no popup.js. I’m running everything in background.js – Suji Sep 15 '21 at 15:10
  • chrome.storage.local.set({key: value}, function() { console.log('Value is set to ' + value); }); Did you try the callback at set – Johan Hoeksma Sep 15 '21 at 16:42

0 Answers0