Skip to content

Commit 28ea00f

Browse files
committed
[mv3] Share console error in troubleshooting information
This shoould help investigating issues, especially with mobile devices, where the browser dev tools console is not available.
1 parent a1a5f36 commit 28ea00f

File tree

10 files changed

+126
-60
lines changed

10 files changed

+126
-60
lines changed

platform/mv3/extension/js/background.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import {
7979
getMatchedRules,
8080
isSideloaded,
8181
toggleDeveloperMode,
82+
ubolErr,
8283
ubolLog,
8384
} from './debug.js';
8485

@@ -90,6 +91,7 @@ import {
9091
} from './config.js';
9192

9293
import { dnr } from './ext-compat.js';
94+
import { getTroubleshootingInfo } from './troubleshooting.js';
9395
import { registerInjectables } from './scripting-manager.js';
9496
import { toggleToolbarIcon } from './action.js';
9597

@@ -189,7 +191,7 @@ function onMessage(request, sender, callback) {
189191
origin: 'USER',
190192
target: { tabId, frameIds: [ frameId ] },
191193
}).catch(reason => {
192-
console.log(reason);
194+
ubolErr(reason);
193195
});
194196
return false;
195197
}
@@ -201,7 +203,7 @@ function onMessage(request, sender, callback) {
201203
origin: 'USER',
202204
target: { tabId, frameIds: [ frameId ] },
203205
}).catch(reason => {
204-
console.log(reason);
206+
ubolErr(reason);
205207
});
206208
return false;
207209
}
@@ -240,7 +242,7 @@ function onMessage(request, sender, callback) {
240242
target: { tabId, frameIds: [ frameId ] },
241243
injectImmediately: true,
242244
}).catch(reason => {
243-
console.log(reason);
245+
ubolErr(reason);
244246
}).then(( ) => {
245247
callback();
246248
});
@@ -271,8 +273,9 @@ function onMessage(request, sender, callback) {
271273
return registerInjectables();
272274
}).then(( ) => {
273275
callback(result);
274-
broadcastMessage({ enabledRulesets: result.enabledRulesets });
275276
});
277+
}).finally(( ) => {
278+
broadcastMessage({ enabledRulesets: rulesetConfig.enabledRulesets });
276279
});
277280
return true;
278281
}
@@ -506,6 +509,12 @@ function onMessage(request, sender, callback) {
506509
});
507510
return true;
508511

512+
case 'getTroubleshootingInfo':
513+
getTroubleshootingInfo(request.siteMode).then(info => {
514+
callback(info);
515+
});
516+
return true;
517+
509518
default:
510519
break;
511520
}
@@ -639,7 +648,7 @@ const isFullyInitialized = start().then(( ) => {
639648
localRemove('goodStart');
640649
return false;
641650
}).catch(reason => {
642-
console.trace(reason);
651+
ubolErr(reason);
643652
if ( process.wakeupRun ) { return; }
644653
return localRead('goodStart').then(goodStart => {
645654
if ( goodStart === false ) {

platform/mv3/extension/js/dashboard.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ import {
2424
localRead,
2525
localRemove,
2626
localWrite,
27+
runtime,
28+
sendMessage,
2729
} from './ext.js';
2830

29-
import { getTroubleshootingInfo } from './troubleshooting.js';
30-
import { runtime } from './ext.js';
31-
3231
/******************************************************************************/
3332

3433
{
@@ -53,9 +52,14 @@ localRead('dashboard.activePane').then(pane => {
5352
dom.body.dataset.pane = pane;
5453
});
5554

56-
getTroubleshootingInfo().then(config => {
57-
qs$('[data-i18n="supportS5H"] + pre').textContent = config;
55+
// Update troubleshooting on-demand
56+
const tsinfoObserver = new IntersectionObserver(entries => {
57+
if ( entries.every(a => a.isIntersecting === false) ) { return; }
58+
sendMessage({ what: 'getTroubleshootingInfo' }).then(config => {
59+
qs$('[data-i18n="supportS5H"] + pre').textContent = config;
60+
});
5861
});
62+
tsinfoObserver.observe(qs$('[data-i18n="supportS5H"] + pre'));
5963

6064
/******************************************************************************/
6165

platform/mv3/extension/js/debug.js

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,89 @@
1919
Home: https://github.com/gorhill/uBlock
2020
*/
2121

22-
import { dnr, normalizeDNRRules } from './ext-compat.js';
23-
import { browser } from './ext.js';
22+
import {
23+
dnr,
24+
normalizeDNRRules,
25+
webext,
26+
} from './ext-compat.js';
27+
28+
import {
29+
sessionRead,
30+
sessionWrite,
31+
} from './ext.js';
2432

2533
/******************************************************************************/
2634

2735
const isModern = dnr.onRuleMatchedDebug instanceof Object;
2836

2937
export const isSideloaded = (( ) => {
30-
const { permissions } = browser.runtime.getManifest();
38+
const { permissions } = webext.runtime.getManifest();
3139
return permissions?.includes('declarativeNetRequestFeedback') ?? false;
3240
})();
3341

3442
/******************************************************************************/
3543

44+
const CONSOLE_MAX_LINES = 32;
45+
const consoleOutput = [];
46+
let consoleWritePtr = 0;
47+
48+
sessionRead('console').then(before => {
49+
if ( Array.isArray(before) === false ) { return; }
50+
const current = getConsoleOutput();
51+
const merged = [ ...before, ...current ].slice(-CONSOLE_MAX_LINES);
52+
for ( let i = 0; i < merged.length; i++ ) {
53+
consoleOutput[i] = merged[i];
54+
}
55+
consoleWritePtr = merged.length % CONSOLE_MAX_LINES;
56+
});
57+
58+
const consoleAdd = (...args) => {
59+
if ( args.length === 0 ) { return; }
60+
const now = new Date();
61+
const time = [
62+
`${now.getUTCMonth()+1}`.padStart(2, '0'),
63+
`${now.getUTCDate()}`.padStart(2, '0'),
64+
'.',
65+
`${now.getUTCHours()}`.padStart(2, '0'),
66+
`${now.getUTCMinutes()}`.padStart(2, '0'),
67+
].join('');
68+
for ( let i = 0; i < args.length; i++ ) {
69+
const s = `[${time}]${args[i]}`;
70+
if ( Boolean(s) === false ) { continue; }
71+
consoleOutput[consoleWritePtr++] = s;
72+
consoleWritePtr %= CONSOLE_MAX_LINES;
73+
}
74+
sessionWrite('console', getConsoleOutput());
75+
}
76+
3677
export const ubolLog = (...args) => {
3778
// Do not pollute dev console in stable releases.
3879
if ( isSideloaded !== true ) { return; }
3980
console.info('[uBOL]', ...args);
4081
};
4182

83+
export const ubolErr = (...args) => {
84+
if ( Array.isArray(args) === false ) { return; }
85+
if ( globalThis.ServiceWorkerGlobalScope ) {
86+
consoleAdd(...args);
87+
}
88+
// Do not pollute dev console in stable releases.
89+
if ( isSideloaded !== true ) { return; }
90+
console.error('[uBOL]', ...args);
91+
};
92+
93+
export const getConsoleOutput = ( ) => {
94+
return [
95+
...consoleOutput.slice(consoleWritePtr),
96+
...consoleOutput.slice(0, consoleWritePtr),
97+
].reduce((acc, val) => {
98+
if ( val !== acc.at(-1) ) {
99+
acc.push(val);
100+
}
101+
return acc;
102+
}, []);
103+
};
104+
42105
/******************************************************************************/
43106

44107
const rulesets = new Map();

platform/mv3/extension/js/fetch.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
Home: https://github.com/gorhill/uBlock
2020
*/
2121

22+
import { ubolErr } from './debug.js';
23+
2224
/******************************************************************************/
2325

2426
function fetchJSON(path) {
2527
return fetch(`${path}.json`).then(response =>
2628
response.json()
2729
).catch(reason => {
28-
console.info(reason);
30+
ubolErr(reason);
2931
});
3032
}
3133

platform/mv3/extension/js/filter-manager.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import {
3434
subtractHostnameIters,
3535
} from './utils.js';
3636

37+
import { ubolErr } from './debug.js';
38+
3739
/******************************************************************************/
3840

3941
export async function selectorsFromCustomFilters(hostname) {
@@ -79,7 +81,7 @@ export function startCustomFilters(tabId, frameId) {
7981
target: { tabId, frameIds: [ frameId ] },
8082
injectImmediately: true,
8183
}).catch(reason => {
82-
console.log(reason);
84+
ubolErr(reason);
8385
})
8486
}
8587

@@ -89,7 +91,7 @@ export function terminateCustomFilters(tabId, frameId) {
8991
target: { tabId, frameIds: [ frameId ] },
9092
injectImmediately: true,
9193
}).catch(reason => {
92-
console.log(reason);
94+
ubolErr(reason);
9395
})
9496
}
9597

@@ -107,7 +109,7 @@ export async function injectCustomFilters(tabId, frameId, hostname) {
107109
origin: 'USER',
108110
target: { tabId, frameIds: [ frameId ] },
109111
}).catch(reason => {
110-
console.log(reason);
112+
ubolErr(reason);
111113
})
112114
);
113115
}
@@ -119,7 +121,7 @@ export async function injectCustomFilters(tabId, frameId, hostname) {
119121
target: { tabId, frameIds: [ frameId ] },
120122
injectImmediately: true,
121123
}).catch(reason => {
122-
console.log(reason);
124+
ubolErr(reason);
123125
})
124126
);
125127
}

platform/mv3/extension/js/report.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
*/
2121

2222
import { dom, qs$ } from './dom.js';
23-
import { getTroubleshootingInfo } from './troubleshooting.js';
2423
import { sendMessage } from './ext.js';
2524

2625
/******************************************************************************/
@@ -93,7 +92,10 @@ async function reportSpecificFilterIssue() {
9392

9493
/******************************************************************************/
9594

96-
getTroubleshootingInfo(reportedPage.mode).then(config => {
95+
sendMessage({
96+
what: 'getTroubleshootingInfo',
97+
siteMode: reportedPage.mode,
98+
}).then(config => {
9799
qs$('[data-i18n="supportS5H"] + pre').textContent = config;
98100

99101
dom.on('[data-url]', 'click', ev => {

platform/mv3/extension/js/ruleset-manager.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import { fetchJSON } from './fetch.js';
3636
import { getAdminRulesets } from './admin.js';
3737
import { hasBroadHostPermissions } from './utils.js';
3838
import { rulesFromText } from './dnr-parser.js';
39-
import { ubolLog } from './debug.js';
39+
import { ubolErr, ubolLog } from './debug.js';
4040

4141
/******************************************************************************/
4242

@@ -318,7 +318,7 @@ async function updateDynamicRules() {
318318
ubolLog(`Add ${addRules.length} dynamic DNR rules`);
319319
}
320320
} catch(reason) {
321-
console.error(`updateDynamicRules() / ${reason}`);
321+
ubolErr(`updateDynamicRules() / ${reason}`);
322322
response.error = `${reason}`;
323323
}
324324

@@ -472,7 +472,7 @@ async function updateSessionRules() {
472472
ubolLog(`Add ${addRules.length} session DNR rules`);
473473
}
474474
} catch(reason) {
475-
console.error(`updateSessionRules() / ${reason}`);
475+
ubolErr(`updateSessionRules() / ${reason}`);
476476
response.error = `${reason}`;
477477
}
478478
return response;
@@ -667,7 +667,7 @@ async function enableRulesets(ids) {
667667
enableRulesetIds,
668668
disableRulesetIds,
669669
}).catch(reason => {
670-
ubolLog(reason);
670+
ubolErr(reason);
671671
response.error = `${reason}`;
672672
});
673673

@@ -684,7 +684,7 @@ async function enableRulesets(ids) {
684684
ubolLog(`Available static rule count: ${count}`);
685685
response.staticRuleCount = count;
686686
}).catch(reason => {
687-
ubolLog(reason);
687+
ubolErr(reason);
688688
});
689689

690690
return response;
@@ -781,7 +781,7 @@ async function updateUserRules() {
781781
out.added = addRules.length;
782782
out.removed = removeRuleIds.length;
783783
} catch(reason) {
784-
console.info(`updateUserRules() / ${reason}`);
784+
ubolErr(`updateUserRules() / ${reason}`);
785785
out.errors.push(`${reason}`);
786786
} finally {
787787
const userRules = await getEffectiveUserRules();

platform/mv3/extension/js/scripting-manager.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,14 @@
2121

2222
import * as ut from './utils.js';
2323

24-
import {
25-
browser,
26-
localRemove,
27-
localWrite,
28-
} from './ext.js';
24+
import { browser, localRemove } from './ext.js';
25+
import { ubolErr, ubolLog } from './debug.js';
2926

3027
import { fetchJSON } from './fetch.js';
3128
import { getEnabledRulesetsDetails } from './ruleset-manager.js';
3229
import { getFilteringModeDetails } from './mode-manager.js';
3330
import { registerCustomFilters } from './filter-manager.js';
3431
import { registerToolbarIconToggler } from './action.js';
35-
import { ubolLog } from './debug.js';
3632

3733
/******************************************************************************/
3834

@@ -555,8 +551,7 @@ async function registerInjectables() {
555551
await browser.scripting.unregisterContentScripts({ ids: toRemove });
556552
localRemove('$scripting.unregisterContentScripts');
557553
} catch(reason) {
558-
localWrite('$scripting.unregisterContentScripts', `${reason}`);
559-
console.info(reason);
554+
ubolErr(reason);
560555
}
561556
}
562557

@@ -566,8 +561,7 @@ async function registerInjectables() {
566561
await browser.scripting.registerContentScripts(toAdd);
567562
localRemove('$scripting.registerContentScripts');
568563
} catch(reason) {
569-
localWrite('$scripting.registerContentScripts', `${reason}`);
570-
console.info(reason);
564+
ubolErr(reason);
571565
}
572566
}
573567

platform/mv3/extension/js/settings.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,8 @@ listen.onmessage = ev => {
222222
}
223223

224224
if ( message.enabledRulesets !== undefined ) {
225-
if ( hashFromIterable(message.enabledRulesets) !== hashFromIterable(local.enabledRulesets) ) {
226-
local.enabledRulesets = message.enabledRulesets;
227-
render = true;
228-
}
225+
local.enabledRulesets = message.enabledRulesets;
226+
render = true;
229227
}
230228

231229
if ( render === false ) { return; }
@@ -250,7 +248,7 @@ sendMessage({
250248
}
251249
listen();
252250
}).catch(reason => {
253-
console.trace(reason);
251+
console.error(reason);
254252
});
255253

256254
/******************************************************************************/

0 commit comments

Comments
 (0)