How to score the functionality of the Chrome Extension API variables?

Hello.

I have the extension for Google Chrome. The challenge is to surpass this extension on browsers that support Chrome Extension API (such as Yandex.Browser, Opera, etc.). Just as we know, not all browsers fully support all of the features of the API. At least Opera doesn't support chrome.storage.sync and some settings in chrome.notifications.create. Not to do for each individual browser extension, it was decided to wrap functions with weak support in their own variable. For example, instead of chrome.storage.sync.get(keys, callback) to call the cbf.storage.sync.get(keys, callback). It is done like this:

var cbf = {};
cbf.storage = {};
cbf.storage.sync = {};
// browser_name set earlier
cbf.storage.sync.get = browser_name == "Chrome" ? chrome.storage.sync.get : chrome.storage.local.get;

Only problem is that it doesn't work. When you call the function in the console error is thrown:

cbf.storage.sync.get(function(sync_storage) { /* ... */ });

The error text:

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

It seems that I do not understand something. Prompt, please, as correctly.

TUCF8yt.png
July 9th 19 at 13:13
1 answer
July 9th 19 at 13:15
Solution
And what you put in there - chrome.storage.sync.get or chrome.storage.local.get - it is defined? What is it, if print it to the console?

console.log(chrome.storage.local.get);
Yes, chrome.storage.sync.get and chrome.storage.local.get are built-in methodsused in Chrome extensions.

Here is a good demonstration of the problem:

4h9t3fz.png
If you type console.log(chrome.storage.local.get);, it prints the following:

fgRzgpM.png
Image is clickable. - clifton83 commented on July 9th 19 at 13:18
: what if you try to wrap your function?

Something like:

cbf.storage.sync = {
get: function (/*string or array of string or object*/ keys, /*function*/ callback) {
// here - forwarding at any given store, depending on the browser https://developer.chrome.com/extensions/storage#ty...
}
}; - Leon59 commented on July 9th 19 at 13:21
in principle, we did so:

var cbf = {};
cbf.storage = {};
cbf.storage.sync = {};
cbf.notifications = {};

cbf.storage.sync.get = function() { 
 if (browser_info['name'] == "Chrome") {
 if (arguments.length === 1) { chrome.storage.sync.get(arguments[0]); }
 else if (arguments.length === 2) { chrome.storage.sync.get(arguments[0], arguments[1]); }
}
 else {
 if (arguments.length === 1) { chrome.storage.local.get(arguments[0]); }
 else if (arguments.length === 2) { chrome.storage.local.get(arguments[0], arguments[1]); }
}
}


Although the solution seems to me not very elegant, and indeed, it would be cool to know why the above code did not give the expected results. For self-development, so to speak. - clifton83 commented on July 9th 19 at 13:24
: maybe the context is not enough? stackoverflow.com/questions/8773579/if-javascript-... - Leon59 commented on July 9th 19 at 13:27

Find more questions by tags Extensions for browsersJavaScriptGoogle Chrome