summaryrefslogtreecommitdiffstats
path: root/platform/common/vapi-background.js
diff options
context:
space:
mode:
Diffstat (limited to 'platform/common/vapi-background.js')
-rw-r--r--platform/common/vapi-background.js133
1 files changed, 64 insertions, 69 deletions
diff --git a/platform/common/vapi-background.js b/platform/common/vapi-background.js
index 0d6fcdd..2ff99ac 100644
--- a/platform/common/vapi-background.js
+++ b/platform/common/vapi-background.js
@@ -87,6 +87,19 @@ vAPI.app = {
},
};
+/******************************************************************************/
+
+// Generate segments of random six alphanumeric characters, thus one segment
+// is a value out of 36^6 = over 2x10^9 values.
+
+vAPI.generateSecret = (size = 1) => {
+ let secret = '';
+ while ( size-- ) {
+ secret += (Math.floor(Math.random() * 2176782336) + 2176782336).toString(36).slice(1);
+ }
+ return secret;
+};
+
/*******************************************************************************
*
* https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/storage/session
@@ -96,9 +109,9 @@ vAPI.app = {
*
* */
-vAPI.sessionStorage = {
+vAPI.sessionStorage = browser.storage.session || {
get() {
- return Promise.resolve({});
+ return Promise.resolve();
},
set() {
return Promise.resolve();
@@ -109,7 +122,7 @@ vAPI.sessionStorage = {
clear() {
return Promise.resolve();
},
- implemented: false,
+ unavailable: true,
};
/*******************************************************************************
@@ -124,46 +137,21 @@ vAPI.sessionStorage = {
vAPI.storage = {
get(key, ...args) {
- if ( vAPI.sessionStorage.implemented !== true ) {
- return webext.storage.local.get(key, ...args).catch(reason => {
- console.log(reason);
- });
- }
- return vAPI.sessionStorage.get(key, ...args).then(bin => {
- const size = Object.keys(bin).length;
- if ( size === 1 && typeof key === 'string' && bin[key] === null ) {
- return {};
- }
- if ( size !== 0 ) { return bin; }
- return webext.storage.local.get(key, ...args).then(bin => {
- if ( bin instanceof Object === false ) { return bin; }
- // Mirror empty result as null value in order to prevent
- // from falling back to storage.local when there is no need.
- const tomirror = Object.assign({}, bin);
- if ( typeof key === 'string' && Object.keys(bin).length === 0 ) {
- Object.assign(tomirror, { [key]: null });
- }
- vAPI.sessionStorage.set(tomirror);
- return bin;
- }).catch(reason => {
- console.log(reason);
- });
+ return webext.storage.local.get(key, ...args).catch(reason => {
+ console.log(reason);
});
},
set(...args) {
- vAPI.sessionStorage.set(...args);
return webext.storage.local.set(...args).catch(reason => {
console.log(reason);
});
},
remove(...args) {
- vAPI.sessionStorage.remove(...args);
return webext.storage.local.remove(...args).catch(reason => {
console.log(reason);
});
},
clear(...args) {
- vAPI.sessionStorage.clear(...args);
return webext.storage.local.clear(...args).catch(reason => {
console.log(reason);
});
@@ -328,10 +316,10 @@ vAPI.Tabs = class {
});
}
- async executeScript() {
+ async executeScript(...args) {
let result;
try {
- result = await webext.tabs.executeScript(...arguments);
+ result = await webext.tabs.executeScript(...args);
}
catch(reason) {
}
@@ -445,22 +433,26 @@ vAPI.Tabs = class {
// For some reasons, some platforms do not honor the left,top
// position when specified. I found that further calling
// windows.update again with the same position _may_ help.
+ //
+ // https://github.com/uBlockOrigin/uBlock-issues/issues/2249
+ // Mind that the creation of the popup window might fail.
if ( details.popup !== undefined && vAPI.windows instanceof Object ) {
- const createDetails = {
+ const basicDetails = {
url: details.url,
type: details.popup,
};
- if ( details.box instanceof Object ) {
- Object.assign(createDetails, details.box);
+ const shouldRestorePosition = details.box instanceof Object;
+ const extraDetails = shouldRestorePosition
+ ? Object.assign({}, basicDetails, details.box)
+ : basicDetails;
+ const win = await vAPI.windows.create(extraDetails);
+ if ( win === null ) {
+ if ( shouldRestorePosition === false ) { return; }
+ return vAPI.windows.create(basicDetails);
}
- const win = await vAPI.windows.create(createDetails);
- if ( win === null ) { return; }
- if ( details.box instanceof Object === false ) { return; }
- if (
- win.left === details.box.left &&
- win.top === details.box.top
- ) {
- return;
+ if ( shouldRestorePosition === false ) { return; }
+ if ( win.left === details.box.left ) {
+ if ( win.top === details.box.top ) { return; }
}
vAPI.windows.update(win.id, {
left: details.box.left,
@@ -556,7 +548,7 @@ vAPI.Tabs = class {
targetURL = vAPI.getURL(targetURL);
}
- vAPI.tabs.update(tabId, { url: targetURL });
+ return vAPI.tabs.update(tabId, { url: targetURL });
}
async remove(tabId) {
@@ -1165,11 +1157,6 @@ vAPI.messaging = {
// Support using a new secret for every network request.
{
- // Generate a 6-character alphanumeric string, thus one random value out
- // of 36^6 = over 2x10^9 values.
- const generateSecret = ( ) =>
- (Math.floor(Math.random() * 2176782336) + 2176782336).toString(36).slice(1);
-
const root = vAPI.getURL('/');
const reSecret = /\?secret=(\w+)/;
const shortSecrets = [];
@@ -1207,7 +1194,7 @@ vAPI.messaging = {
}
}
lastShortSecretTime = Date.now();
- const secret = generateSecret();
+ const secret = vAPI.generateSecret();
shortSecrets.push(secret);
return secret;
},
@@ -1215,7 +1202,7 @@ vAPI.messaging = {
if ( previous !== undefined ) {
longSecrets.delete(previous);
}
- const secret = `${generateSecret()}${generateSecret()}${generateSecret()}`;
+ const secret = vAPI.generateSecret(3);
longSecrets.add(secret);
return secret;
},
@@ -1663,10 +1650,7 @@ vAPI.cloud = (( ) => {
const push = async function(details) {
const { datakey, data, encode } = details;
- if (
- data === undefined ||
- typeof data === 'string' && data === ''
- ) {
+ if ( data === undefined || typeof data === 'string' && data === '' ) {
return deleteChunks(datakey, 0);
}
const item = {
@@ -1674,10 +1658,9 @@ vAPI.cloud = (( ) => {
tstamp: Date.now(),
data,
};
- const json = JSON.stringify(item);
const encoded = encode instanceof Function
- ? await encode(json)
- : json;
+ ? await encode(item)
+ : JSON.stringify(item);
// Chunkify taking into account QUOTA_BYTES_PER_ITEM:
// https://developer.chrome.com/extensions/storage#property-sync
@@ -1742,13 +1725,16 @@ vAPI.cloud = (( ) => {
i += 1;
}
encoded = encoded.join('');
- const json = decode instanceof Function
- ? await decode(encoded)
- : encoded;
+
let entry = null;
try {
- entry = JSON.parse(json);
- } catch(ex) {
+ if ( decode instanceof Function ) {
+ entry = await decode(encoded) || null;
+ }
+ if ( typeof entry === 'string' ) {
+ entry = JSON.parse(entry);
+ }
+ } catch(_) {
}
return entry;
};
@@ -1797,15 +1783,24 @@ vAPI.cloud = (( ) => {
/******************************************************************************/
/******************************************************************************/
-vAPI.alarms = browser.alarms || {
- create() {
+vAPI.alarms = {
+ create(...args) {
+ webext.alarms.create(...args);
},
- clear() {
+ createIfNotPresent(name, ...args) {
+ webext.alarms.get(name).then(details => {
+ if ( details !== undefined ) { return; }
+ webext.alarms.create(name, ...args);
+ });
+ },
+ async clear(...args) {
+ return webext.alarms.clear(...args);
},
onAlarm: {
- addListener() {
- }
- }
+ addListener(...args) {
+ webext.alarms.onAlarm.addListener(...args);
+ },
+ },
};
/******************************************************************************/