A really simple way to call the original chrome javascript API and return a Promise.
npm i -S chrome-call
When you do:
var promise = new Promise(function(resolve,reject) {
chrome.storage.local.get('key',function(value) {
if(chrome.runtime.lastError){
reject(chrome.runtime.lastError);
}else{
resolve(value);
}
});
});
It's equal to:
var promise = chromeCall('storage.local.get','key');
That's really simple, right?
Most of chrome API only have one argument in callback, but someone not, such as chrome.hid.receive. In this situation, the value of promise will be an (real) Array:
chromeCall('hid.receive',connectionId)
.then(function(args){
Array.isArray(args); // true
var reportId = args[0];
var data = args[1];
});
The global chromeCall
search function on window.chrome
, but you can use different scope:
var local = chromeCall.scope('storage.local'); // or chromeCall.scope(chrome.storage.local)
var promise = local('get','key');
MIT