YankoFelipe
YankoFelipe

Reputation: 13

Is there a way to use the `chrome` namespace in a webpage?

I'm working on a WebExtension that runs in both firefox and chrome (also, I need the extension to work as a webpage when I visit the html that the build leaves). In this context, I want to use the chrome.storage.local storage. However, when I try to use it in the webpage in Chrome, storage isn't defined and I get:

Uncaught (in promise) TypeError: Cannot read property 'local' of undefined

As a workaround, I'm considering to use window.localStorage if location.protocol is different than 'chrome-extension:' or 'moz-extension:', but I'd like to know if there is a way to make chrome.storage work in the webpage.

Upvotes: 1

Views: 980

Answers (1)

Petr Srníček
Petr Srníček

Reputation: 2386

Some chrome APIs (eg. chrome.runtime.sendMessage) are available to regular pages, but chrome.storage is not one of them. It is only available to extensions that request the storage permission in their manifest.json. This is stated in the documentation for both Chrome and Mozilla Firefox.

Upvotes: 2

Related Questions