mirror of
https://github.com/gorhill/uBlock.git
synced 2024-10-06 09:37:12 +02:00
code review of efcab14597
: fix collected errors; replace Promises with callbacks
This commit is contained in:
parent
525584de1f
commit
e40a66e294
@ -37,10 +37,22 @@ vAPI.cantWebsocket =
|
|||||||
chrome.webRequest.ResourceType instanceof Object === false ||
|
chrome.webRequest.ResourceType instanceof Object === false ||
|
||||||
chrome.webRequest.ResourceType.WEBSOCKET !== 'websocket';
|
chrome.webRequest.ResourceType.WEBSOCKET !== 'websocket';
|
||||||
|
|
||||||
// https://issues.adblockplus.org/ticket/5695
|
vAPI.lastError = function() {
|
||||||
// - Good idea, adopted: cleaner way to detect user-stylesheet support.
|
return chrome.runtime.lastError;
|
||||||
|
};
|
||||||
|
|
||||||
|
// https://github.com/gorhill/uBlock/issues/875
|
||||||
|
// https://code.google.com/p/chromium/issues/detail?id=410868#c8
|
||||||
|
// Must not leave `lastError` unchecked.
|
||||||
|
vAPI.resetLastError = function() {
|
||||||
|
void chrome.runtime.lastError;
|
||||||
|
};
|
||||||
|
|
||||||
vAPI.supportsUserStylesheets = vAPI.webextFlavor.soup.has('user_stylesheet');
|
vAPI.supportsUserStylesheets = vAPI.webextFlavor.soup.has('user_stylesheet');
|
||||||
vAPI.insertCSS = chrome.tabs.insertCSS;
|
|
||||||
|
vAPI.insertCSS = function(tabId, details) {
|
||||||
|
return chrome.tabs.insertCSS(tabId, details, vAPI.resetLastError);
|
||||||
|
};
|
||||||
|
|
||||||
var noopFunc = function(){};
|
var noopFunc = function(){};
|
||||||
|
|
||||||
@ -103,12 +115,6 @@ vAPI.browserSettings = (function() {
|
|||||||
}
|
}
|
||||||
})(),
|
})(),
|
||||||
|
|
||||||
// https://github.com/gorhill/uBlock/issues/875
|
|
||||||
// Must not leave `lastError` unchecked.
|
|
||||||
noopCallback: function() {
|
|
||||||
void chrome.runtime.lastError;
|
|
||||||
},
|
|
||||||
|
|
||||||
// Calling with `true` means IP address leak is not prevented.
|
// Calling with `true` means IP address leak is not prevented.
|
||||||
// https://github.com/gorhill/uBlock/issues/533
|
// https://github.com/gorhill/uBlock/issues/533
|
||||||
// We must first check wether this Chromium-based browser was compiled
|
// We must first check wether this Chromium-based browser was compiled
|
||||||
@ -170,12 +176,12 @@ vAPI.browserSettings = (function() {
|
|||||||
if ( setting ) {
|
if ( setting ) {
|
||||||
cpn.webRTCMultipleRoutesEnabled.clear({
|
cpn.webRTCMultipleRoutesEnabled.clear({
|
||||||
scope: 'regular'
|
scope: 'regular'
|
||||||
}, this.noopCallback);
|
}, vAPI.resetLastError);
|
||||||
} else {
|
} else {
|
||||||
cpn.webRTCMultipleRoutesEnabled.set({
|
cpn.webRTCMultipleRoutesEnabled.set({
|
||||||
value: false,
|
value: false,
|
||||||
scope: 'regular'
|
scope: 'regular'
|
||||||
}, this.noopCallback);
|
}, vAPI.resetLastError);
|
||||||
}
|
}
|
||||||
} catch(ex) {
|
} catch(ex) {
|
||||||
console.error(ex);
|
console.error(ex);
|
||||||
@ -188,14 +194,14 @@ vAPI.browserSettings = (function() {
|
|||||||
if ( setting ) {
|
if ( setting ) {
|
||||||
cpn.webRTCIPHandlingPolicy.clear({
|
cpn.webRTCIPHandlingPolicy.clear({
|
||||||
scope: 'regular'
|
scope: 'regular'
|
||||||
}, this.noopCallback);
|
}, vAPI.resetLastError);
|
||||||
} else {
|
} else {
|
||||||
// https://github.com/uBlockOrigin/uAssets/issues/333#issuecomment-289426678
|
// https://github.com/uBlockOrigin/uAssets/issues/333#issuecomment-289426678
|
||||||
// - Leverage virtuous side-effect of strictest setting.
|
// - Leverage virtuous side-effect of strictest setting.
|
||||||
cpn.webRTCIPHandlingPolicy.set({
|
cpn.webRTCIPHandlingPolicy.set({
|
||||||
value: 'disable_non_proxied_udp',
|
value: 'disable_non_proxied_udp',
|
||||||
scope: 'regular'
|
scope: 'regular'
|
||||||
}, this.noopCallback);
|
}, vAPI.resetLastError);
|
||||||
}
|
}
|
||||||
} catch(ex) {
|
} catch(ex) {
|
||||||
console.error(ex);
|
console.error(ex);
|
||||||
@ -214,12 +220,12 @@ vAPI.browserSettings = (function() {
|
|||||||
if ( !!details[setting] ) {
|
if ( !!details[setting] ) {
|
||||||
chrome.privacy.network.networkPredictionEnabled.clear({
|
chrome.privacy.network.networkPredictionEnabled.clear({
|
||||||
scope: 'regular'
|
scope: 'regular'
|
||||||
}, this.noopCallback);
|
}, vAPI.resetLastError);
|
||||||
} else {
|
} else {
|
||||||
chrome.privacy.network.networkPredictionEnabled.set({
|
chrome.privacy.network.networkPredictionEnabled.set({
|
||||||
value: false,
|
value: false,
|
||||||
scope: 'regular'
|
scope: 'regular'
|
||||||
}, this.noopCallback);
|
}, vAPI.resetLastError);
|
||||||
}
|
}
|
||||||
} catch(ex) {
|
} catch(ex) {
|
||||||
console.error(ex);
|
console.error(ex);
|
||||||
@ -231,12 +237,12 @@ vAPI.browserSettings = (function() {
|
|||||||
if ( !!details[setting] ) {
|
if ( !!details[setting] ) {
|
||||||
chrome.privacy.websites.hyperlinkAuditingEnabled.clear({
|
chrome.privacy.websites.hyperlinkAuditingEnabled.clear({
|
||||||
scope: 'regular'
|
scope: 'regular'
|
||||||
}, this.noopCallback);
|
}, vAPI.resetLastError);
|
||||||
} else {
|
} else {
|
||||||
chrome.privacy.websites.hyperlinkAuditingEnabled.set({
|
chrome.privacy.websites.hyperlinkAuditingEnabled.set({
|
||||||
value: false,
|
value: false,
|
||||||
scope: 'regular'
|
scope: 'regular'
|
||||||
}, this.noopCallback);
|
}, vAPI.resetLastError);
|
||||||
}
|
}
|
||||||
} catch(ex) {
|
} catch(ex) {
|
||||||
console.error(ex);
|
console.error(ex);
|
||||||
@ -271,7 +277,6 @@ vAPI.tabs = {};
|
|||||||
// network requests.
|
// network requests.
|
||||||
|
|
||||||
vAPI.isBehindTheSceneTabId = function(tabId) {
|
vAPI.isBehindTheSceneTabId = function(tabId) {
|
||||||
if ( typeof tabId === 'string' ) { debugger; }
|
|
||||||
return tabId < 0;
|
return tabId < 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -284,11 +289,9 @@ vAPI.anyTabId = -2; // one of the existing tab
|
|||||||
// To remove when tabId-as-integer has been tested enough.
|
// To remove when tabId-as-integer has been tested enough.
|
||||||
|
|
||||||
var toChromiumTabId = function(tabId) {
|
var toChromiumTabId = function(tabId) {
|
||||||
if ( typeof tabId === 'string' ) { debugger; }
|
return typeof tabId === 'number' && !isNaN(tabId) && tabId > 0 ?
|
||||||
if ( typeof tabId !== 'number' || isNaN(tabId) || tabId === -1 ) {
|
tabId :
|
||||||
return 0;
|
0;
|
||||||
}
|
|
||||||
return tabId;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
@ -401,7 +404,7 @@ vAPI.tabs.get = function(tabId, callback) {
|
|||||||
chrome.tabs.query(
|
chrome.tabs.query(
|
||||||
{ active: true, currentWindow: true },
|
{ active: true, currentWindow: true },
|
||||||
function(tabs) {
|
function(tabs) {
|
||||||
if ( chrome.runtime.lastError ) { /* noop */ }
|
void chrome.runtime.lastError;
|
||||||
callback(
|
callback(
|
||||||
Array.isArray(tabs) && tabs.length !== 0 ? tabs[0] : null
|
Array.isArray(tabs) && tabs.length !== 0 ? tabs[0] : null
|
||||||
);
|
);
|
||||||
@ -417,7 +420,7 @@ vAPI.tabs.get = function(tabId, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
chrome.tabs.get(tabId, function(tab) {
|
chrome.tabs.get(tabId, function(tab) {
|
||||||
if ( chrome.runtime.lastError ) { /* noop */ }
|
void chrome.runtime.lastError;
|
||||||
callback(tab);
|
callback(tab);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -528,7 +531,7 @@ vAPI.tabs.open = function(details) {
|
|||||||
targetURLWithoutHash = pos === -1 ? targetURL : targetURL.slice(0, pos);
|
targetURLWithoutHash = pos === -1 ? targetURL : targetURL.slice(0, pos);
|
||||||
|
|
||||||
chrome.tabs.query({ url: targetURLWithoutHash }, function(tabs) {
|
chrome.tabs.query({ url: targetURLWithoutHash }, function(tabs) {
|
||||||
if ( chrome.runtime.lastError ) { /* noop */ }
|
void chrome.runtime.lastError;
|
||||||
var tab = Array.isArray(tabs) && tabs[0];
|
var tab = Array.isArray(tabs) && tabs[0];
|
||||||
if ( !tab ) {
|
if ( !tab ) {
|
||||||
wrapper();
|
wrapper();
|
||||||
@ -562,12 +565,7 @@ vAPI.tabs.replace = function(tabId, url) {
|
|||||||
targetURL = vAPI.getURL(targetURL);
|
targetURL = vAPI.getURL(targetURL);
|
||||||
}
|
}
|
||||||
|
|
||||||
chrome.tabs.update(tabId, { url: targetURL }, function() {
|
chrome.tabs.update(tabId, { url: targetURL }, vAPI.resetLastError);
|
||||||
// https://code.google.com/p/chromium/issues/detail?id=410868#c8
|
|
||||||
if ( chrome.runtime.lastError ) {
|
|
||||||
/* noop */
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
@ -576,14 +574,7 @@ vAPI.tabs.remove = function(tabId) {
|
|||||||
tabId = toChromiumTabId(tabId);
|
tabId = toChromiumTabId(tabId);
|
||||||
if ( tabId === 0 ) { return; }
|
if ( tabId === 0 ) { return; }
|
||||||
|
|
||||||
var onTabRemoved = function() {
|
chrome.tabs.remove(tabId, vAPI.resetLastError);
|
||||||
// https://code.google.com/p/chromium/issues/detail?id=410868#c8
|
|
||||||
if ( chrome.runtime.lastError ) {
|
|
||||||
/* noop */
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
chrome.tabs.remove(tabId, onTabRemoved);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
@ -592,17 +583,10 @@ vAPI.tabs.reload = function(tabId, bypassCache) {
|
|||||||
tabId = toChromiumTabId(tabId);
|
tabId = toChromiumTabId(tabId);
|
||||||
if ( tabId === 0 ) { return; }
|
if ( tabId === 0 ) { return; }
|
||||||
|
|
||||||
var onReloaded = function() {
|
|
||||||
// https://code.google.com/p/chromium/issues/detail?id=410868#c8
|
|
||||||
if ( chrome.runtime.lastError ) {
|
|
||||||
/* noop */
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
chrome.tabs.reload(
|
chrome.tabs.reload(
|
||||||
tabId,
|
tabId,
|
||||||
{ bypassCache: bypassCache === true },
|
{ bypassCache: bypassCache === true },
|
||||||
onReloaded
|
vAPI.resetLastError
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -615,12 +599,8 @@ vAPI.tabs.select = function(tabId) {
|
|||||||
if ( tabId === 0 ) { return; }
|
if ( tabId === 0 ) { return; }
|
||||||
|
|
||||||
chrome.tabs.update(tabId, { active: true }, function(tab) {
|
chrome.tabs.update(tabId, { active: true }, function(tab) {
|
||||||
if ( chrome.runtime.lastError ) {
|
void chrome.runtime.lastError;
|
||||||
/* noop */
|
if ( !tab ) { return; }
|
||||||
}
|
|
||||||
if ( !tab ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
chrome.windows.update(tab.windowId, { focused: true });
|
chrome.windows.update(tab.windowId, { focused: true });
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -630,9 +610,7 @@ vAPI.tabs.select = function(tabId) {
|
|||||||
vAPI.tabs.injectScript = function(tabId, details, callback) {
|
vAPI.tabs.injectScript = function(tabId, details, callback) {
|
||||||
var onScriptExecuted = function() {
|
var onScriptExecuted = function() {
|
||||||
// https://code.google.com/p/chromium/issues/detail?id=410868#c8
|
// https://code.google.com/p/chromium/issues/detail?id=410868#c8
|
||||||
if ( chrome.runtime.lastError ) {
|
void chrome.runtime.lastError;
|
||||||
/* noop */
|
|
||||||
}
|
|
||||||
if ( typeof callback === 'function' ) {
|
if ( typeof callback === 'function' ) {
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
@ -835,21 +813,28 @@ vAPI.messaging.onPortMessage = (function() {
|
|||||||
details.runAt = 'document_start';
|
details.runAt = 'document_start';
|
||||||
}
|
}
|
||||||
var cssText;
|
var cssText;
|
||||||
const cssPromises = [];
|
var countdown = 0;
|
||||||
|
var countdownHandler = function() {
|
||||||
|
void chrome.runtime.lastError;
|
||||||
|
countdown -= 1;
|
||||||
|
if ( countdown === 0 && typeof callback === 'function' ) {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
};
|
||||||
for ( cssText of msg.add ) {
|
for ( cssText of msg.add ) {
|
||||||
|
countdown += 1;
|
||||||
details.code = cssText;
|
details.code = cssText;
|
||||||
cssPromises.push(chrome.tabs.insertCSS(tabId, details));
|
chrome.tabs.insertCSS(tabId, details, countdownHandler);
|
||||||
}
|
}
|
||||||
if ( typeof chrome.tabs.removeCSS === 'function' ) {
|
if ( typeof chrome.tabs.removeCSS === 'function' ) {
|
||||||
for ( cssText of msg.remove ) {
|
for ( cssText of msg.remove ) {
|
||||||
|
countdown += 1;
|
||||||
details.code = cssText;
|
details.code = cssText;
|
||||||
cssPromises.push(chrome.tabs.removeCSS(tabId, details));
|
chrome.tabs.removeCSS(tabId, details, countdownHandler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( typeof callback === 'function' ) {
|
if ( countdown === 0 && typeof callback === 'function' ) {
|
||||||
Promise.all(cssPromises).then(() => {
|
callback();
|
||||||
callback();
|
|
||||||
}, null);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -990,9 +975,10 @@ vAPI.contextMenu = chrome.contextMenus && {
|
|||||||
_callback: null,
|
_callback: null,
|
||||||
_entries: [],
|
_entries: [],
|
||||||
_createEntry: function(entry) {
|
_createEntry: function(entry) {
|
||||||
chrome.contextMenus.create(JSON.parse(JSON.stringify(entry)), function() {
|
chrome.contextMenus.create(
|
||||||
void chrome.runtime.lastError;
|
JSON.parse(JSON.stringify(entry)),
|
||||||
});
|
vAPI.resetLastError
|
||||||
|
);
|
||||||
},
|
},
|
||||||
onMustUpdate: function() {},
|
onMustUpdate: function() {},
|
||||||
setEntries: function(entries, callback) {
|
setEntries: function(entries, callback) {
|
||||||
@ -1038,13 +1024,6 @@ vAPI.commands = chrome.commands;
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
vAPI.lastError = function() {
|
|
||||||
return chrome.runtime.lastError;
|
|
||||||
};
|
|
||||||
|
|
||||||
/******************************************************************************/
|
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
// This is called only once, when everything has been loaded in memory after
|
// This is called only once, when everything has been loaded in memory after
|
||||||
// the extension was launched. It can be used to inject content scripts
|
// the extension was launched. It can be used to inject content scripts
|
||||||
// in already opened web pages, to remove whatever nuisance could make it to
|
// in already opened web pages, to remove whatever nuisance could make it to
|
||||||
|
Loading…
Reference in New Issue
Block a user