2019-09-16 15:45:17 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2023-12-04 18:10:34 +01:00
|
|
|
uBlock Origin - a comprehensive, efficient content blocker
|
2019-09-16 15:45:17 +02:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
// `webext` is a promisified api of `chrome`. Entries are added as
|
|
|
|
// the promisification of uBO progress.
|
|
|
|
|
2019-09-19 22:41:44 +02:00
|
|
|
const promisifyNoFail = function(thisArg, fnName, outFn = r => r) {
|
|
|
|
const fn = thisArg[fnName];
|
2024-05-10 03:23:40 +02:00
|
|
|
return function(...args) {
|
2019-09-19 22:41:44 +02:00
|
|
|
return new Promise(resolve => {
|
2024-05-10 03:23:40 +02:00
|
|
|
try {
|
|
|
|
fn.call(thisArg, ...args, function(...args) {
|
|
|
|
void chrome.runtime.lastError;
|
|
|
|
resolve(outFn(...args));
|
|
|
|
});
|
|
|
|
} catch(ex) {
|
|
|
|
console.error(ex);
|
|
|
|
resolve(outFn());
|
|
|
|
}
|
2019-09-19 22:41:44 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const promisify = function(thisArg, fnName) {
|
|
|
|
const fn = thisArg[fnName];
|
2024-05-10 03:23:40 +02:00
|
|
|
return function(...args) {
|
2019-09-19 22:41:44 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2024-05-10 03:23:40 +02:00
|
|
|
try {
|
|
|
|
fn.call(thisArg, ...args, function(...args) {
|
|
|
|
const lastError = chrome.runtime.lastError;
|
|
|
|
if ( lastError instanceof Object ) {
|
|
|
|
return reject(lastError.message);
|
|
|
|
}
|
|
|
|
resolve(...args);
|
|
|
|
});
|
|
|
|
} catch(ex) {
|
|
|
|
console.error(ex);
|
|
|
|
resolve();
|
|
|
|
}
|
2019-09-19 22:41:44 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const webext = {
|
2024-03-01 02:29:36 +01:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/alarms
|
|
|
|
alarms: {
|
|
|
|
clear: promisifyNoFail(chrome.alarms, 'clear'),
|
|
|
|
clearAll: promisifyNoFail(chrome.alarms, 'clearAll'),
|
|
|
|
create: promisifyNoFail(chrome.alarms, 'create'),
|
|
|
|
get: promisifyNoFail(chrome.alarms, 'get'),
|
|
|
|
getAll: promisifyNoFail(chrome.alarms, 'getAll'),
|
|
|
|
onAlarm: chrome.alarms.onAlarm,
|
|
|
|
},
|
2019-09-23 14:25:23 +02:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/browserAction
|
|
|
|
browserAction: {
|
|
|
|
setBadgeBackgroundColor: promisifyNoFail(chrome.browserAction, 'setBadgeBackgroundColor'),
|
|
|
|
setBadgeText: promisifyNoFail(chrome.browserAction, 'setBadgeText'),
|
|
|
|
setIcon: promisifyNoFail(chrome.browserAction, 'setIcon'),
|
|
|
|
setTitle: promisifyNoFail(chrome.browserAction, 'setTitle'),
|
|
|
|
},
|
2019-09-19 22:41:44 +02:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/menus
|
|
|
|
menus: {
|
|
|
|
create: function() {
|
|
|
|
return chrome.contextMenus.create(...arguments, ( ) => {
|
|
|
|
void chrome.runtime.lastError;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onClicked: chrome.contextMenus.onClicked,
|
|
|
|
remove: promisifyNoFail(chrome.contextMenus, 'remove'),
|
2021-07-15 19:14:37 +02:00
|
|
|
removeAll: promisifyNoFail(chrome.contextMenus, 'removeAll'),
|
2019-09-19 22:41:44 +02:00
|
|
|
},
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/privacy
|
|
|
|
privacy: {
|
|
|
|
},
|
2019-09-18 14:34:55 +02:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage
|
2019-09-16 15:45:17 +02:00
|
|
|
storage: {
|
2019-09-18 14:34:55 +02:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage/local
|
2019-09-16 15:45:17 +02:00
|
|
|
local: {
|
2019-09-19 22:41:44 +02:00
|
|
|
clear: promisify(chrome.storage.local, 'clear'),
|
|
|
|
get: promisify(chrome.storage.local, 'get'),
|
|
|
|
getBytesInUse: promisify(chrome.storage.local, 'getBytesInUse'),
|
|
|
|
remove: promisify(chrome.storage.local, 'remove'),
|
|
|
|
set: promisify(chrome.storage.local, 'set'),
|
2019-09-16 15:45:17 +02:00
|
|
|
},
|
|
|
|
},
|
2019-09-18 14:34:55 +02:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs
|
2019-09-16 15:45:17 +02:00
|
|
|
tabs: {
|
2019-09-19 22:41:44 +02:00
|
|
|
get: promisifyNoFail(chrome.tabs, 'get', tab => tab instanceof Object ? tab : null),
|
|
|
|
executeScript: promisifyNoFail(chrome.tabs, 'executeScript'),
|
|
|
|
insertCSS: promisifyNoFail(chrome.tabs, 'insertCSS'),
|
2020-10-02 18:16:47 +02:00
|
|
|
removeCSS: promisifyNoFail(chrome.tabs, 'removeCSS'),
|
2019-09-19 22:41:44 +02:00
|
|
|
query: promisifyNoFail(chrome.tabs, 'query', tabs => Array.isArray(tabs) ? tabs : []),
|
|
|
|
reload: promisifyNoFail(chrome.tabs, 'reload'),
|
|
|
|
remove: promisifyNoFail(chrome.tabs, 'remove'),
|
2024-03-17 14:34:05 +01:00
|
|
|
sendMessage: promisifyNoFail(chrome.tabs, 'sendMessage'),
|
2019-09-19 22:41:44 +02:00
|
|
|
update: promisifyNoFail(chrome.tabs, 'update', tab => tab instanceof Object ? tab : null),
|
2019-09-16 15:45:17 +02:00
|
|
|
},
|
2019-09-23 14:25:23 +02:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webNavigation
|
|
|
|
webNavigation: {
|
|
|
|
getFrame: promisify(chrome.webNavigation, 'getFrame'),
|
2020-07-18 13:44:26 +02:00
|
|
|
getAllFrames: promisify(chrome.webNavigation, 'getAllFrames'),
|
2019-09-23 14:25:23 +02:00
|
|
|
},
|
2019-09-18 14:34:55 +02:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/windows
|
2019-09-16 15:45:17 +02:00
|
|
|
windows: {
|
2019-09-19 22:41:44 +02:00
|
|
|
get: promisifyNoFail(chrome.windows, 'get', win => win instanceof Object ? win : null),
|
|
|
|
create: promisifyNoFail(chrome.windows, 'create', win => win instanceof Object ? win : null),
|
|
|
|
update: promisifyNoFail(chrome.windows, 'update', win => win instanceof Object ? win : null),
|
2019-09-16 15:45:17 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-09-19 22:41:44 +02:00
|
|
|
// browser.privacy entries
|
|
|
|
{
|
|
|
|
const settings = [
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/privacy/network
|
|
|
|
[ 'network', 'networkPredictionEnabled' ],
|
|
|
|
[ 'network', 'webRTCIPHandlingPolicy' ],
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/privacy/websites
|
|
|
|
[ 'websites', 'hyperlinkAuditingEnabled' ],
|
|
|
|
];
|
|
|
|
for ( const [ category, setting ] of settings ) {
|
|
|
|
let categoryEntry = webext.privacy[category];
|
|
|
|
if ( categoryEntry instanceof Object === false ) {
|
|
|
|
categoryEntry = webext.privacy[category] = {};
|
|
|
|
}
|
|
|
|
const settingEntry = categoryEntry[setting] = {};
|
|
|
|
const thisArg = chrome.privacy[category][setting];
|
|
|
|
settingEntry.clear = promisifyNoFail(thisArg, 'clear');
|
|
|
|
settingEntry.get = promisifyNoFail(thisArg, 'get');
|
|
|
|
settingEntry.set = promisifyNoFail(thisArg, 'set');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage/managed
|
|
|
|
if ( chrome.storage.managed instanceof Object ) {
|
|
|
|
webext.storage.managed = {
|
|
|
|
get: promisify(chrome.storage.managed, 'get'),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-18 14:34:55 +02:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage/sync
|
|
|
|
if ( chrome.storage.sync instanceof Object ) {
|
|
|
|
webext.storage.sync = {
|
|
|
|
QUOTA_BYTES: chrome.storage.sync.QUOTA_BYTES,
|
|
|
|
QUOTA_BYTES_PER_ITEM: chrome.storage.sync.QUOTA_BYTES_PER_ITEM,
|
|
|
|
MAX_ITEMS: chrome.storage.sync.MAX_ITEMS,
|
|
|
|
MAX_WRITE_OPERATIONS_PER_HOUR: chrome.storage.sync.MAX_WRITE_OPERATIONS_PER_HOUR,
|
|
|
|
MAX_WRITE_OPERATIONS_PER_MINUTE: chrome.storage.sync.MAX_WRITE_OPERATIONS_PER_MINUTE,
|
|
|
|
|
2019-09-19 22:41:44 +02:00
|
|
|
clear: promisify(chrome.storage.sync, 'clear'),
|
|
|
|
get: promisify(chrome.storage.sync, 'get'),
|
|
|
|
getBytesInUse: promisify(chrome.storage.sync, 'getBytesInUse'),
|
|
|
|
remove: promisify(chrome.storage.sync, 'remove'),
|
|
|
|
set: promisify(chrome.storage.sync, 'set'),
|
2019-09-18 14:34:55 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-16 22:17:48 +02:00
|
|
|
// https://bugs.chromium.org/p/chromium/issues/detail?id=608854
|
|
|
|
if ( chrome.tabs.removeCSS instanceof Function ) {
|
2019-09-19 22:41:44 +02:00
|
|
|
webext.tabs.removeCSS = promisifyNoFail(chrome.tabs, 'removeCSS');
|
2019-09-16 22:17:48 +02:00
|
|
|
}
|
|
|
|
|
2021-10-07 20:41:29 +02:00
|
|
|
export default webext;
|