Chrome Extension V3 does not save storage?

366 views
Skip to first unread message

Petar Maletic

unread,
Feb 4, 2024, 7:57:49 PM2/4/24
to Chromium Extensions
service-worker.js:
chrome.storage.local.set({test: "yes"}, function () {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
} else {
console.log("SAVED");
}
});

Says saved but in developer tools -> Application->Local Storage, all empty?
In console:
chrome.storage.local.get(" test ").then((result) => {
    console.log("Value currently is " + result.key);
  });
and also in service-worker.js....

Says it's undefined?

manifest.json looks like:

{
"name": "SEO",
"description": "SEO",
"author": "Petar",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "js/service-worker.js"
},
"web_accessible_resources": [
{
"resources": ["js/*.js"],
"matches": ["<all_urls>"]
},
{
"resources": ["css/*.css"],
"matches": ["<all_urls>"]
},
{
"resources": ["*.html"],
"matches": ["<all_urls>"]
}
],
"externally_connectable": {
"matches": [
"*://*.google.com/*"
]
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"run_at": "document_start",
"all_frames": false,
"js": [ "js/google-scrape.js"]
}
],
"icons": {
"128": "images/icon-128.png",
"180": "images/icon-128.png"
},
"action": {},
"permissions": [
"activeTab",
"tabs",
"storage",
"gcm"
],
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'self'"
},
"options_page": "options.html",
"action": {
"default_icon": {
"128": "images/icon-128.png"
},
"default_title": "SEO"
}
}

Browser Extenstion

unread,
Feb 4, 2024, 10:08:30 PM2/4/24
to Chromium Extensions, Petar Maletic
Your syntax is incorrect.
Use:

chrome.storage.local.get("test", (result) => {
    console.log("Value currently is " + result.test);
});

wOxxOm

unread,
Feb 4, 2024, 10:41:19 PM2/4/24
to Chromium Extensions, Browser Extenstion, Petar Maletic
Also note that `Local Storage` shown by devtools is `window.localStorage` of the web site shown in the tab and not `chrome.storage.local` of the extension, which is a completely different storage. To inspect it you can use Storage Area Explorer extension (due to a bug in Chrome it can't work in devtools for a service worker, so you'll use it in a visible page like the popup) or run await chrome.storage.local.get() in devtools console or develop in Firefox which shows this storage in devtools for the background script.

Petar Maletic

unread,
Feb 5, 2024, 9:42:59 AM2/5/24
to Chromium Extensions, wOxxOm, Browser Extenstion, Petar Maletic
Thanks @ wOxxOm
I opened a pinned tab like this: chrome.tabs.create({url: "chrome-extension://" + chrome.runtime.id + "/js/google-scrape.js", active: false, pinned: true}, function (tab) {});
This is for faster checks after refresh extension :)

And than with Storage Area Explorer extension I can see all data :)
Reply all
Reply to author
Forward
0 new messages