1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-08 12:57:57 +02:00
uBlock/platform/chromium/webext.js
Raymond Hill eec53c0154
Work toward modernizing code base: promisification
Swathes of code have been converted to use
Promises/async/await. More left to do.

Related commits:
- 915687fddb
- 55cc0c6997
- e27328f931
2019-09-16 09:45:17 -04:00

158 lines
5.6 KiB
JavaScript

/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2019-present Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/uBlock
*/
'use strict';
// `webext` is a promisified api of `chrome`. Entries are added as
// the promisification of uBO progress.
const webext = { // jshint ignore:line
storage: {
local: {
clear: function() {
return new Promise((resolve, reject) => {
chrome.storage.local.clear(( ) => {
const lastError = chrome.runtime.lastError;
if ( lastError instanceof Object ) {
return reject(lastError);
}
resolve();
});
});
},
get: function() {
return new Promise((resolve, reject) => {
chrome.storage.local.get(...arguments, result => {
const lastError = chrome.runtime.lastError;
if ( lastError instanceof Object ) {
return reject(lastError);
}
resolve(result);
});
});
},
getBytesInUse: function() {
return new Promise((resolve, reject) => {
chrome.storage.local.getBytesInUse(...arguments, result => {
const lastError = chrome.runtime.lastError;
if ( lastError instanceof Object ) {
return reject(lastError);
}
resolve(result);
});
});
},
remove: function() {
return new Promise((resolve, reject) => {
chrome.storage.local.remove(...arguments, ( ) => {
const lastError = chrome.runtime.lastError;
if ( lastError instanceof Object ) {
return reject(lastError);
}
resolve();
});
});
},
set: function() {
return new Promise((resolve, reject) => {
chrome.storage.local.set(...arguments, ( ) => {
const lastError = chrome.runtime.lastError;
if ( lastError instanceof Object ) {
return reject(lastError);
}
resolve();
});
});
},
},
},
tabs: {
get: function() {
return new Promise(resolve => {
chrome.tabs.get(...arguments, tab => {
void chrome.runtime.lastError;
resolve(tab instanceof Object ? tab : null);
});
});
},
query: function() {
return new Promise(resolve => {
chrome.tabs.query(...arguments, tabs => {
void chrome.runtime.lastError;
resolve(Array.isArray(tabs) ? tabs : []);
});
});
},
update: function() {
return new Promise(resolve => {
chrome.tabs.update(...arguments, tab => {
void chrome.runtime.lastError;
resolve(tab instanceof Object ? tab : null);
});
});
},
},
windows: {
get: async function() {
return new Promise(resolve => {
chrome.windows.get(...arguments, win => {
void chrome.runtime.lastError;
resolve(win instanceof Object ? win : null);
});
});
},
create: async function() {
return new Promise(resolve => {
chrome.windows.create(...arguments, win => {
void chrome.runtime.lastError;
resolve(win instanceof Object ? win : null);
});
});
},
update: async function() {
return new Promise(resolve => {
chrome.windows.update(...arguments, win => {
void chrome.runtime.lastError;
resolve(win instanceof Object ? win : null);
});
});
},
},
};
if ( chrome.storage.managed instanceof Object ) {
webext.storage.managed = {
get: function() {
return new Promise((resolve, reject) => {
chrome.storage.local.get(...arguments, result => {
const lastError = chrome.runtime.lastError;
if ( lastError instanceof Object ) {
return reject(lastError);
}
resolve(result);
});
});
},
};
}