1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-05 11:37:01 +02:00

Avoid using ! toolbar icon badge when inconsequential

uBO will now verify that at least one unprocessed network requests
at launch should have been blocked in order to warn users of
unprocessed network requests through the `!` toolbar icon badge.

For example, with default filter lists, there is nothing to block
on `wikipedia.org`, and hence in this case it's not useful to
present the user with the `!` badge.

Therefore uBO will not show the badge *only* when at least one
unprocessed network requests should have been blocked had uBO been
ready when it was fired by the browser.

Related commit:
- https://github.com/gorhill/uBlock/commit/769b8da664be
This commit is contained in:
Raymond Hill 2023-03-30 12:30:44 -04:00
parent 3d1c696e20
commit 1835e90125
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 38 additions and 19 deletions

View File

@ -1191,7 +1191,7 @@ vAPI.Net = class {
this.deferredSuspendableListener = undefined;
this.listenerMap = new WeakMap();
this.suspendDepth = 0;
this.unprocessedTabs = new Set();
this.unprocessedTabs = new Map();
browser.webRequest.onBeforeRequest.addListener(
details => {
@ -1252,6 +1252,17 @@ vAPI.Net = class {
this.onUnprocessedRequest(details);
}
setSuspendableListener(listener) {
for ( const [ tabId, requests ] of this.unprocessedTabs ) {
let i = requests.length;
while ( i-- ) {
const r = listener(requests[i]);
if ( r === undefined || r.cancel === false ) {
requests.splice(i, 1);
}
}
if ( requests.length !== 0 ) { continue; }
this.unprocessedTabs.delete(tabId);
}
if ( this.unprocessedTabs.size !== 0 ) {
this.deferredSuspendableListener = listener;
listener = details => {
@ -1285,11 +1296,16 @@ vAPI.Net = class {
return actualListener;
}
onUnprocessedRequest(details) {
if ( details.tabId === -1 ) { return; }
const { tabId } = details;
if ( tabId === -1 ) { return; }
if ( this.unprocessedTabs.size === 0 ) {
vAPI.setDefaultIcon('-loading', '!');
}
this.unprocessedTabs.add(details.tabId);
let requests = this.unprocessedTabs.get(tabId);
if ( requests === undefined ) {
this.unprocessedTabs.set(tabId, (requests = []));
}
requests.push(Object.assign({}, details));
}
hasUnprocessedRequest(tabId) {
return this.unprocessedTabs.size !== 0 &&

View File

@ -103,6 +103,7 @@ const initializeTabs = async ( ) => {
const tabs = await vAPI.tabs.query({ url: '<all_urls>' });
for ( const tab of tabs ) {
if ( tab.discarded === true ) { continue; }
if ( tab.status === 'unloaded' ) { continue; }
const { id, url } = tab;
µb.tabContextManager.commit(id, url);
µb.bindTabToPageStore(id, 'tabCommitted', tab);
@ -118,21 +119,23 @@ const initializeTabs = async ( ) => {
tabIds.push(id);
}
}
const results = await Promise.all(toCheck);
for ( let i = 0; i < results.length; i++ ) {
const result = results[i];
if ( result.length === 0 || result[0] !== true ) { continue; }
// Inject declarative content scripts programmatically.
for ( const contentScript of manifest.content_scripts ) {
for ( const file of contentScript.js ) {
vAPI.tabs.executeScript(tabIds[i], {
file: file,
allFrames: contentScript.all_frames,
runAt: contentScript.run_at
});
// We do not want to block on content scripts injection
Promise.all(toCheck).then(results => {
for ( let i = 0; i < results.length; i++ ) {
const result = results[i];
if ( result.length === 0 || result[0] !== true ) { continue; }
// Inject declarative content scripts programmatically.
for ( const contentScript of manifest.content_scripts ) {
for ( const file of contentScript.js ) {
vAPI.tabs.executeScript(tabIds[i], {
file: file,
allFrames: contentScript.all_frames,
runAt: contentScript.run_at
});
}
}
}
}
});
};
/******************************************************************************/
@ -462,6 +465,9 @@ if ( selfieIsValid !== true ) {
// This can be used to defer filtering decision-making.
µb.readyToFilter = true;
// Initialize internal state with maybe already existing tabs.
await initializeTabs();
// Start network observers.
webRequest.start();
@ -472,9 +478,6 @@ webRequest.start();
// as possible ensure minimal memory usage baseline.
lz4Codec.relinquish();
// Initialize internal state with maybe already existing tabs.
initializeTabs();
// https://github.com/chrisaljoudi/uBlock/issues/184
// Check for updates not too far in the future.
io.addObserver(µb.assetObserver.bind(µb));