P
P
Petr Flaks2016-05-18 16:56:20
JavaScript
Petr Flaks, 2016-05-18 16:56:20

How to cast Chrome Extension API functions into variables?

Hello.
I have an extension for Google Chrome. There is a task to overtake this extension to browsers that support the Chrome Extension API (such as Yandex.Browser, Opera, etc.). Only as you know, not all browsers fully support all the features of the API. At the very least, Opera doesn't support chrome.storage.sync and some options in chrome.notifications.create . In order not to make an individual extension for each browser, it was decided to wrap functions with weak support in their own variable. chrome.storage.sync.get(keys, callback)For example, to call instead of cbf.storage.sync.get(keys, callback). It is implemented like this:

var cbf = {};
cbf.storage = {};
cbf.storage.sync = {};
// browser_name задается чуть раньше
cbf.storage.sync.get = browser_name == "Chrome" ? chrome.storage.sync.get : chrome.storage.local.get;

The only problem is that for some reason it doesn't work. When calling the function to the console, an error is thrown:
cbf.storage.sync.get(function(sync_storage) { /* ... */ });

Error text:
extensions::StorageArea:26 Uncaught TypeError: Cannot read property 'get' of undefined

It looks like I don't understand something. Please tell me the correct way.
TUCF8yt.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Pavlov, 2016-05-18
@neluzhin

And what you add there - chrome.storage.sync.get or chrome.storage.local.get - is it defined? What is there if output to the console?
console.log(chrome.storage.local.get);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question