Skip to content

Commit

Permalink
Add advanced setting requestStatsDisabled
Browse files Browse the repository at this point in the history
To disable collating global blocked/allowed counts.

Boolean, default to `false`.

Setting to `true` will prevent uBO from loading/saving global
blocked/allowed counts, and in such case the "Blocked since
install" count instead reflects the count since uBO launched.

Setting back to `false` will cause the counts to resume from
last time they were saved.

Related issue:
uBlockOrigin/uBlock-issues#3100
  • Loading branch information
gorhill committed Mar 1, 2024
1 parent e0971fe commit e02ea69
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const hiddenSettingsDefault = {
popupPanelLockedSections: 0,
popupPanelHeightMode: 0,
requestJournalProcessPeriod: 1000,
requestStatsDisabled: false,
selfieAfter: 2,
strictBlockingBypassDuration: 120,
toolbarWarningTimeout: 60,
Expand Down
76 changes: 52 additions & 24 deletions src/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,52 +97,80 @@ import {
/******************************************************************************/

{
µb.loadLocalSettings = ( ) => Promise.all([
vAPI.sessionStorage.get('requestStats'),
vAPI.storage.get('requestStats'),
vAPI.storage.get([ 'blockedRequestCount', 'allowedRequestCount' ]),
]).then(([ a, b, c ]) => {
if ( a instanceof Object && a.requestStats ) { return a.requestStats; }
if ( b instanceof Object && b.requestStats ) { return b.requestStats; }
if ( c instanceof Object && Object.keys(c).length === 2 ) {
return {
blockedCount: c.blockedRequestCount,
allowedCount: c.allowedRequestCount,
};
}
return { blockedCount: 0, allowedCount: 0 };
}).then(({ blockedCount, allowedCount }) => {
µb.requestStats.blockedCount += blockedCount;
µb.requestStats.allowedCount += allowedCount;
});
const requestStats = µb.requestStats;
let requestStatsDisabled = false;

µb.loadLocalSettings = async ( ) => {
requestStatsDisabled = µb.hiddenSettings.requestStatsDisabled;
if ( requestStatsDisabled ) { return; }
return Promise.all([
vAPI.sessionStorage.get('requestStats'),
vAPI.storage.get('requestStats'),
vAPI.storage.get([ 'blockedRequestCount', 'allowedRequestCount' ]),
]).then(([ a, b, c ]) => {
if ( a instanceof Object && a.requestStats ) { return a.requestStats; }
if ( b instanceof Object && b.requestStats ) { return b.requestStats; }
if ( c instanceof Object && Object.keys(c).length === 2 ) {
return {
blockedCount: c.blockedRequestCount,
allowedCount: c.allowedRequestCount,
};
}
return { blockedCount: 0, allowedCount: 0 };
}).then(({ blockedCount, allowedCount }) => {
requestStats.blockedCount += blockedCount;
requestStats.allowedCount += allowedCount;
});
};

const SAVE_DELAY_IN_MINUTES = 3.6;
const QUICK_SAVE_DELAY_IN_SECONDS = 23;

const stopTimers = ( ) => {
vAPI.alarms.clear('saveLocalSettings');
quickSaveTimer.off();
saveTimer.off();
};

const saveTimer = vAPI.defer.create(( ) => {
µb.saveLocalSettings();
});

const quickSaveTimer = vAPI.defer.create(( ) => {
if ( vAPI.sessionStorage.unavailable !== true ) {
vAPI.sessionStorage.set({ requestStats: requestStats });
}
if ( requestStatsDisabled ) { return; }
saveTimer.on({ min: SAVE_DELAY_IN_MINUTES });
if ( vAPI.sessionStorage.unavailable ) { return; }
vAPI.sessionStorage.set({ requestStats: µb.requestStats });
vAPI.alarms.createIfNotPresent('saveLocalSettings', {
delayInMinutes: SAVE_DELAY_IN_MINUTES + 0.5
});
});

µb.incrementRequestStats = (blocked, allowed) => {
µb.requestStats.blockedCount += blocked;
µb.requestStats.allowedCount += allowed;
requestStats.blockedCount += blocked;
requestStats.allowedCount += allowed;
quickSaveTimer.on({ sec: QUICK_SAVE_DELAY_IN_SECONDS });
};

µb.saveLocalSettings = ( ) => {
vAPI.alarms.clear('saveLocalSettings');
quickSaveTimer.off(); saveTimer.off();
stopTimers();
if ( requestStatsDisabled ) { return; }
return vAPI.storage.set({ requestStats: µb.requestStats });
};

onBroadcast(msg => {
if ( msg.what !== 'hiddenSettingsChanged' ) { return; }
const newState = µb.hiddenSettings.requestStatsDisabled;
if ( requestStatsDisabled === newState ) { return; }
requestStatsDisabled = newState;
if ( newState ) {
stopTimers();
µb.requestStats.blockedCount = µb.requestStats.allowedCount = 0;
} else {
µb.loadLocalSettings();
}
});
}

/******************************************************************************/
Expand Down

0 comments on commit e02ea69

Please sign in to comment.