@whotracksme/reporting 11.1.1 → 12.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +39 -0
- package/package.json +1 -1
- package/reporting/README.md +13 -0
- package/reporting/example/README.md +4 -0
- package/reporting/example/e2e/README.md +265 -0
- package/reporting/example/e2e/console-capture.js +67 -0
- package/reporting/example/e2e/control.js +449 -0
- package/reporting/example/e2e/hub.mjs +380 -0
- package/reporting/example/e2e/launch.mjs +92 -0
- package/reporting/example/e2e/mock-extension.mjs +106 -0
- package/reporting/example/e2e/pages/private-fixture.html +16 -0
- package/reporting/example/e2e/run.mjs +226 -0
- package/reporting/example/e2e/scenarios/privacy-guards.json +26 -0
- package/reporting/example/e2e/scenarios/smoke.json +15 -0
- package/reporting/example/e2e/verify.mjs +159 -0
- package/reporting/example/index.js +55 -46
- package/reporting/example/manifests/chromium.json +1 -0
- package/reporting/example/storage.js +65 -0
- package/reporting/package.json +5 -1
- package/reporting/rollup.config.cjs +1 -1
- package/reporting/snapshots/0001/snapshot.json +13 -13
- package/reporting/snapshots/0003/snapshot.json +5 -5
- package/reporting/snapshots/0005/snapshot.json +13 -13
- package/reporting/src/doublefetch-page-handler.js +30 -20
- package/reporting/src/doublefetch-unbreak.js +186 -0
- package/reporting/src/http.js +197 -19
- package/reporting/src/index.js +1 -0
- package/reporting/src/offscreen/doublefetch/capture.js +90 -0
- package/reporting/src/patterns.js +142 -2
- package/reporting/src/request/index.js +6 -0
- package/reporting/src/request/page-store.js +1 -0
- package/reporting/src/request/support.js +29 -0
- package/reporting/src/search-extractor.js +8 -2
- package/reporting/src/utils.js +19 -0
- package/reporting/test/doublefetch-unbreak.spec.js +133 -0
- package/reporting/test/http.spec.js +113 -0
- package/reporting/test/index.js +1 -0
- package/reporting/test/patterns.spec.js +239 -0
- package/reporting/test/search-extractor.spec.js +19 -1
- package/reporting/test/setup.unit.js +9 -0
package/reporting/src/http.js
CHANGED
|
@@ -316,8 +316,96 @@ async function tryCloseOffscreenDocument() {
|
|
|
316
316
|
|
|
317
317
|
const OFFSCREEN_DOCUMENT_PREFIX = 'offscreen/doublefetch';
|
|
318
318
|
const OFFSCREEN_DOCUMENT_INDEX_HTML = `${OFFSCREEN_DOCUMENT_PREFIX}/index.html`;
|
|
319
|
+
const OFFSCREEN_DOCUMENT_CAPTURE_JS = `${OFFSCREEN_DOCUMENT_PREFIX}/capture.js`;
|
|
320
|
+
const CAPTURE_CONTENT_SCRIPT_ID = 'wtm-doublefetch-capture';
|
|
321
|
+
const CAPTURE_MESSAGE_TARGET = 'doublefetch:capture';
|
|
319
322
|
|
|
320
|
-
|
|
323
|
+
/**
|
|
324
|
+
* The page cannot be read from here (cross-origin), so a content script in the
|
|
325
|
+
* iframe sends the DOM back once the page looks ready. Content scripts cannot
|
|
326
|
+
* be aimed at a single frame of an offscreen document, so this one is
|
|
327
|
+
* registered for the whole domain; pages that the user opens meanwhile are
|
|
328
|
+
* covered by the guard in capture.js and by ignoring messages from a tab.
|
|
329
|
+
*/
|
|
330
|
+
async function installCaptureHandler(url, { waitFor = [], delay = 0 }) {
|
|
331
|
+
const { protocol, hostname } = new URL(url);
|
|
332
|
+
let resolveHtml;
|
|
333
|
+
let rejectHtml;
|
|
334
|
+
const pendingHtml = new Promise((resolve, reject) => {
|
|
335
|
+
resolveHtml = resolve;
|
|
336
|
+
rejectHtml = reject;
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
const onMessage = (message, sender, sendResponse) => {
|
|
340
|
+
if (message?.target !== CAPTURE_MESSAGE_TARGET) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
if (sender.tab) {
|
|
344
|
+
logger.warn(
|
|
345
|
+
'Ignoring a double-fetch capture message from a tab:',
|
|
346
|
+
sender.tab.id,
|
|
347
|
+
);
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
if (message.type === 'config') {
|
|
351
|
+
logger.debug('Capture script is ready in:', message.url);
|
|
352
|
+
sendResponse({ waitFor, delay });
|
|
353
|
+
} else if (message.type === 'content' && typeof message.html === 'string') {
|
|
354
|
+
logger.debug('Captured', message.html.length, 'characters');
|
|
355
|
+
resolveHtml(message.html);
|
|
356
|
+
} else if (message.type === 'permanent-error') {
|
|
357
|
+
logger.warn('Unable to capture:', url, message.code, message.details);
|
|
358
|
+
rejectHtml(
|
|
359
|
+
new PermanentlyUnableToFetchUrlError(
|
|
360
|
+
`Unable to capture ${url} (${message.code})`,
|
|
361
|
+
),
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
chrome.runtime.onMessage.addListener(onMessage);
|
|
366
|
+
|
|
367
|
+
try {
|
|
368
|
+
// Leftovers are not expected (the script is registered for the duration of
|
|
369
|
+
// one step only), but the service worker could have been killed in between.
|
|
370
|
+
await chrome.scripting
|
|
371
|
+
.unregisterContentScripts({ ids: [CAPTURE_CONTENT_SCRIPT_ID] })
|
|
372
|
+
.catch(() => {});
|
|
373
|
+
await chrome.scripting.registerContentScripts([
|
|
374
|
+
{
|
|
375
|
+
id: CAPTURE_CONTENT_SCRIPT_ID,
|
|
376
|
+
// Note: match patterns have no port, so this covers any port on the host.
|
|
377
|
+
matches: [`${protocol}//${hostname}/*`],
|
|
378
|
+
js: [OFFSCREEN_DOCUMENT_CAPTURE_JS],
|
|
379
|
+
runAt: 'document_start',
|
|
380
|
+
allFrames: true,
|
|
381
|
+
world: 'ISOLATED',
|
|
382
|
+
persistAcrossSessions: false,
|
|
383
|
+
},
|
|
384
|
+
]);
|
|
385
|
+
} catch (e) {
|
|
386
|
+
chrome.runtime.onMessage.removeListener(onMessage);
|
|
387
|
+
throw e;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
return {
|
|
391
|
+
pendingHtml,
|
|
392
|
+
cleanup: async () => {
|
|
393
|
+
chrome.runtime.onMessage.removeListener(onMessage);
|
|
394
|
+
await chrome.scripting
|
|
395
|
+
.unregisterContentScripts({ ids: [CAPTURE_CONTENT_SCRIPT_ID] })
|
|
396
|
+
.catch((e) => {
|
|
397
|
+
logger.error('cleanup failed: unable to remove capture script:', e);
|
|
398
|
+
});
|
|
399
|
+
},
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
async function withOffscreenDocumentReady(
|
|
404
|
+
url,
|
|
405
|
+
headers,
|
|
406
|
+
captureConfig,
|
|
407
|
+
asyncCallback,
|
|
408
|
+
) {
|
|
321
409
|
const cleanups = [];
|
|
322
410
|
try {
|
|
323
411
|
const domain = new URL(url).hostname;
|
|
@@ -347,16 +435,26 @@ async function withOffscreenDocumentReady(url, headers, asyncCallback) {
|
|
|
347
435
|
});
|
|
348
436
|
cleanups.push(undoHeaderOverride);
|
|
349
437
|
|
|
438
|
+
let pendingHtml = null;
|
|
439
|
+
if (captureConfig) {
|
|
440
|
+
const handler = await installCaptureHandler(url, captureConfig);
|
|
441
|
+
cleanups.push(handler.cleanup);
|
|
442
|
+
pendingHtml = handler.pendingHtml;
|
|
443
|
+
}
|
|
444
|
+
|
|
350
445
|
const offscreenUrl = chrome.runtime.getURL(OFFSCREEN_DOCUMENT_INDEX_HTML);
|
|
351
446
|
const existingContexts = await chrome.runtime.getContexts({
|
|
352
447
|
contextTypes: ['OFFSCREEN_DOCUMENT'],
|
|
353
448
|
documentUrls: [offscreenUrl],
|
|
354
449
|
});
|
|
355
450
|
if (existingContexts.length > 0) {
|
|
356
|
-
//
|
|
357
|
-
//
|
|
358
|
-
|
|
359
|
-
|
|
451
|
+
// A leftover from an earlier run: the service worker must have died
|
|
452
|
+
// before its cleanup could run. Since the lookup is by URL, it can only
|
|
453
|
+
// be our own document, so it is safe to close it and start over.
|
|
454
|
+
// (Otherwise, it would break all further dynamic double-fetches: the
|
|
455
|
+
// browser allows only one offscreen document per extension.)
|
|
456
|
+
logger.warn('Closing a stale offscreen document:', existingContexts);
|
|
457
|
+
await tryCloseOffscreenDocument();
|
|
360
458
|
}
|
|
361
459
|
|
|
362
460
|
await chrome.offscreen.createDocument({
|
|
@@ -366,7 +464,7 @@ async function withOffscreenDocumentReady(url, headers, asyncCallback) {
|
|
|
366
464
|
});
|
|
367
465
|
cleanups.push(() => tryCloseOffscreenDocument());
|
|
368
466
|
|
|
369
|
-
return await asyncCallback();
|
|
467
|
+
return await asyncCallback(pendingHtml);
|
|
370
468
|
} finally {
|
|
371
469
|
await Promise.all(
|
|
372
470
|
cleanups.map(async (x) => {
|
|
@@ -567,6 +665,19 @@ const LOCK = new SeqExecutor();
|
|
|
567
665
|
* can be used in later requests. Still, the chain of requests always
|
|
568
666
|
* starts from a clean state. In other words, the temporary context is
|
|
569
667
|
* fully isolated and will be discarded once all steps have completed.
|
|
668
|
+
*
|
|
669
|
+
* A step with "dynamic" set renders the URL in an iframe instead of
|
|
670
|
+
* fetching it; if it is the last step, the rendered page is returned.
|
|
671
|
+
* Two optional keys say when it counts as rendered:
|
|
672
|
+
* * waitFor: CSS selectors that all have to match ('a, b' for either)
|
|
673
|
+
* * delay: milliseconds to wait after that (default: 0), for pages
|
|
674
|
+
* that render progressively
|
|
675
|
+
* Without selectors, the delay alone decides; without either, the load
|
|
676
|
+
* event. If the page never gets there, the step ends in a timeout.
|
|
677
|
+
* - safecookie (optional):
|
|
678
|
+
* Allows to predefine cookies (see "findSafeCookies"). Must be an ES6 Map.
|
|
679
|
+
* Example: if a website uses AWS WAF, steps can set
|
|
680
|
+
* "{{safecookie:aws-waf-token}}".
|
|
570
681
|
* - onError (optional):
|
|
571
682
|
* A fallback configuration for error recovery.
|
|
572
683
|
* - emptyHtml (default: false):
|
|
@@ -580,7 +691,13 @@ export async function anonymousHttpGet(originalUrl, params = {}) {
|
|
|
580
691
|
}
|
|
581
692
|
|
|
582
693
|
async function anonymousHttpGet_(originalUrl, params = {}) {
|
|
583
|
-
const {
|
|
694
|
+
const {
|
|
695
|
+
steps = [],
|
|
696
|
+
emptyHtml = false,
|
|
697
|
+
onError,
|
|
698
|
+
safecookie = new Map(),
|
|
699
|
+
...sharedParams
|
|
700
|
+
} = params;
|
|
584
701
|
if (emptyHtml) {
|
|
585
702
|
logger.debug('Returning empty HTML for URL:', originalUrl);
|
|
586
703
|
return EMPTY_RESCUE_HTML;
|
|
@@ -588,7 +705,10 @@ async function anonymousHttpGet_(originalUrl, params = {}) {
|
|
|
588
705
|
|
|
589
706
|
try {
|
|
590
707
|
if (steps.length === 0) {
|
|
591
|
-
|
|
708
|
+
const headers =
|
|
709
|
+
sharedParams.headers &&
|
|
710
|
+
replacePlaceholders(sharedParams.headers, { safecookie });
|
|
711
|
+
return await singleHttpGetStep(originalUrl, { ...sharedParams, headers });
|
|
592
712
|
}
|
|
593
713
|
if (!chrome?.webRequest?.onHeadersReceived) {
|
|
594
714
|
throw new MultiStepDoublefetchNotSupportedError();
|
|
@@ -597,6 +717,13 @@ async function anonymousHttpGet_(originalUrl, params = {}) {
|
|
|
597
717
|
if (hasDynamicSteps && !chrome?.offscreen?.createDocument) {
|
|
598
718
|
throw new DynamicDoublefetchNotSupportedError();
|
|
599
719
|
}
|
|
720
|
+
// Reading the rendered page needs a content script in the iframe.
|
|
721
|
+
if (
|
|
722
|
+
steps[steps.length - 1].dynamic &&
|
|
723
|
+
!chrome?.scripting?.registerContentScripts
|
|
724
|
+
) {
|
|
725
|
+
throw new DynamicDoublefetchNotSupportedError();
|
|
726
|
+
}
|
|
600
727
|
|
|
601
728
|
// The context that can be queried with the placeholder syntax (e.g.
|
|
602
729
|
// "foo={{cookie:bar}}"). The context is built from the requests from
|
|
@@ -612,6 +739,7 @@ async function anonymousHttpGet_(originalUrl, params = {}) {
|
|
|
612
739
|
cookie: new Map(), // default ('set-cookies' in response)
|
|
613
740
|
cookie0: new Map(), // fallback ('cookies' in request)
|
|
614
741
|
param: new Map(), // URL params
|
|
742
|
+
safecookie,
|
|
615
743
|
},
|
|
616
744
|
observer,
|
|
617
745
|
);
|
|
@@ -627,6 +755,16 @@ async function anonymousHttpGet_(originalUrl, params = {}) {
|
|
|
627
755
|
const nextStep = steps[stepIdx + 1];
|
|
628
756
|
const localParams = { ...sharedParams, ...currentStep };
|
|
629
757
|
|
|
758
|
+
// check "requires": they resolve like normal placeholders, but if they
|
|
759
|
+
// would result in an empty string, it will fail the current chain-of-steps.
|
|
760
|
+
const isMet = (entry) => !!replacePlaceholders({ v: entry }, ctx).v;
|
|
761
|
+
const missing = currentStep.requires?.find((entry) => !isMet(entry));
|
|
762
|
+
if (missing) {
|
|
763
|
+
throw new PermanentlyUnableToFetchUrlError(
|
|
764
|
+
`Step ${stepIdx + 1} requires <<${missing}>>, but it did not resolve`,
|
|
765
|
+
);
|
|
766
|
+
}
|
|
767
|
+
|
|
630
768
|
const readyForNextStep = new Promise((resolve, reject) => {
|
|
631
769
|
observer.onChange = null;
|
|
632
770
|
if (!currentStep.dynamic) {
|
|
@@ -635,7 +773,7 @@ async function anonymousHttpGet_(originalUrl, params = {}) {
|
|
|
635
773
|
}
|
|
636
774
|
|
|
637
775
|
try {
|
|
638
|
-
const graph = buildDependencyGraph(nextStep);
|
|
776
|
+
const graph = buildDependencyGraph(nextStep, ctx);
|
|
639
777
|
if (graph.allReady) {
|
|
640
778
|
resolve();
|
|
641
779
|
return;
|
|
@@ -749,14 +887,19 @@ async function anonymousHttpGet_(originalUrl, params = {}) {
|
|
|
749
887
|
}
|
|
750
888
|
try {
|
|
751
889
|
if (currentStep.dynamic) {
|
|
752
|
-
//
|
|
753
|
-
|
|
754
|
-
|
|
890
|
+
// The content comes from the last step, so only it needs a capture.
|
|
891
|
+
const captureConfig = nextStep
|
|
892
|
+
? null
|
|
893
|
+
: {
|
|
894
|
+
waitFor: localParams.waitFor,
|
|
895
|
+
delay: localParams.delay,
|
|
896
|
+
};
|
|
755
897
|
|
|
756
898
|
await withOffscreenDocumentReady(
|
|
757
899
|
localUrl,
|
|
758
900
|
localParams.headers,
|
|
759
|
-
|
|
901
|
+
captureConfig,
|
|
902
|
+
async (pendingHtml) => {
|
|
760
903
|
const { ok, error } = await chrome.runtime.sendMessage({
|
|
761
904
|
target: 'offscreen:urlReporting',
|
|
762
905
|
type: 'request',
|
|
@@ -770,7 +913,7 @@ async function anonymousHttpGet_(originalUrl, params = {}) {
|
|
|
770
913
|
|
|
771
914
|
let timeout = null;
|
|
772
915
|
const timedOut = new Promise((resolve, reject) => {
|
|
773
|
-
const maxTime =
|
|
916
|
+
const maxTime = localParams.timeout || 15 * SECOND;
|
|
774
917
|
timeout = setTimeout(() => {
|
|
775
918
|
timeout = null;
|
|
776
919
|
logger.warn(
|
|
@@ -790,7 +933,13 @@ async function anonymousHttpGet_(originalUrl, params = {}) {
|
|
|
790
933
|
}, maxTime);
|
|
791
934
|
});
|
|
792
935
|
try {
|
|
793
|
-
|
|
936
|
+
// The last step waits for the rendered page, the others for
|
|
937
|
+
// the values that the step after them needs.
|
|
938
|
+
if (pendingHtml) {
|
|
939
|
+
content = await Promise.race([pendingHtml, timedOut]);
|
|
940
|
+
} else {
|
|
941
|
+
await Promise.race([readyForNextStep, timedOut]);
|
|
942
|
+
}
|
|
794
943
|
} finally {
|
|
795
944
|
clearTimeout(timeout);
|
|
796
945
|
}
|
|
@@ -811,6 +960,7 @@ async function anonymousHttpGet_(originalUrl, params = {}) {
|
|
|
811
960
|
if (onError) {
|
|
812
961
|
const rescueParams = {
|
|
813
962
|
...sharedParams,
|
|
963
|
+
safecookie,
|
|
814
964
|
...onError,
|
|
815
965
|
};
|
|
816
966
|
logger.info(
|
|
@@ -859,6 +1009,9 @@ export function replacePlaceholders(headers, ctx) {
|
|
|
859
1009
|
} else if (expr.startsWith('param:')) {
|
|
860
1010
|
const key = expr.slice('param:'.length);
|
|
861
1011
|
resolved = ctx.param.get(key);
|
|
1012
|
+
} else if (expr.startsWith('safecookie:')) {
|
|
1013
|
+
const key = expr.slice('safecookie:'.length);
|
|
1014
|
+
resolved = ctx.safecookie.get(key);
|
|
862
1015
|
} else {
|
|
863
1016
|
throw new Error(
|
|
864
1017
|
`Unsupported expression: stopped at <<${expr}>> (full expression: ${fullExpression})`,
|
|
@@ -894,17 +1047,28 @@ export function findPlaceholders(text) {
|
|
|
894
1047
|
}
|
|
895
1048
|
|
|
896
1049
|
// Note: exported for tests only
|
|
897
|
-
export function buildDependencyGraph(nextStep) {
|
|
1050
|
+
export function buildDependencyGraph(nextStep, ctx = {}) {
|
|
898
1051
|
const graph = {
|
|
899
1052
|
allReady: true,
|
|
900
1053
|
onChange: null,
|
|
901
1054
|
onReady: () => {}, // can be overwritten by the caller
|
|
902
1055
|
};
|
|
903
1056
|
|
|
1057
|
+
// Unlike cookies and params, safecookie values are provided by the caller
|
|
1058
|
+
// instead of being observed during the run: an absent one never arrives.
|
|
1059
|
+
const isUnresolvable = (x) =>
|
|
1060
|
+
x.startsWith('safecookie:') &&
|
|
1061
|
+
!ctx.safecookie.get(x.slice('safecookie:'.length));
|
|
1062
|
+
|
|
904
1063
|
// 1) find all dependencies
|
|
905
1064
|
// (if there are none, we can exit early without entering "observe-mode")
|
|
906
|
-
if (nextStep
|
|
907
|
-
|
|
1065
|
+
if (nextStep) {
|
|
1066
|
+
// "requires" entries are templates as well, so they are dependencies of
|
|
1067
|
+
// the step in the same way that its headers are.
|
|
1068
|
+
const allTemplates = [
|
|
1069
|
+
...Object.values(nextStep.headers || {}),
|
|
1070
|
+
...(nextStep.requires || []),
|
|
1071
|
+
];
|
|
908
1072
|
if (allTemplates.length > 0) {
|
|
909
1073
|
// Keep track of which placeholders expression exist and by what
|
|
910
1074
|
// (atomic) placeholders they get resolved. For instance, in the
|
|
@@ -922,9 +1086,14 @@ export function buildDependencyGraph(nextStep) {
|
|
|
922
1086
|
|
|
923
1087
|
for (const template of allTemplates) {
|
|
924
1088
|
for (const expression of findPlaceholders(template)) {
|
|
1089
|
+
const placeholders = expression.split('||');
|
|
1090
|
+
if (placeholders.every(isUnresolvable)) {
|
|
1091
|
+
logger.debug('[observe-mode] not waiting for:', expression);
|
|
1092
|
+
continue;
|
|
1093
|
+
}
|
|
925
1094
|
pendingExpressions.add(expression);
|
|
926
1095
|
|
|
927
|
-
for (const placeholder of
|
|
1096
|
+
for (const placeholder of placeholders) {
|
|
928
1097
|
const unlocks =
|
|
929
1098
|
expressionResolvedByPlaceholder.get(placeholder) || [];
|
|
930
1099
|
if (!unlocks.includes(expression)) {
|
|
@@ -969,6 +1138,15 @@ export function buildDependencyGraph(nextStep) {
|
|
|
969
1138
|
}
|
|
970
1139
|
}
|
|
971
1140
|
}
|
|
1141
|
+
|
|
1142
|
+
// A value that the context already holds (a borrowed cookie, or one that an
|
|
1143
|
+
// earlier step captured) will not be set again, so feed it through the same
|
|
1144
|
+
// path that a later value would take.
|
|
1145
|
+
for (const [type, values] of Object.entries(ctx)) {
|
|
1146
|
+
for (const [key, value] of values) {
|
|
1147
|
+
graph.onChange?.(type, key, value);
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
972
1150
|
return graph;
|
|
973
1151
|
}
|
|
974
1152
|
|
package/reporting/src/index.js
CHANGED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// Content script for a dynamic double-fetch step: sends back the rendered DOM.
|
|
2
|
+
(() => {
|
|
3
|
+
const TARGET = 'doublefetch:capture';
|
|
4
|
+
|
|
5
|
+
// Avoid performance bugs if pages are extremely large (>10MB).
|
|
6
|
+
const MAX_HTML_LENGTH = 10 * 1024 * 1024;
|
|
7
|
+
|
|
8
|
+
// This script is registered for the whole domain while a step runs, so it can
|
|
9
|
+
// also land in a page that the user opened. Only the credentialless iframe
|
|
10
|
+
// may be read: "credentialless" is false in the user's tabs, and since the
|
|
11
|
+
// flag is inherited by nested iframes, also check that this is the document
|
|
12
|
+
// the offscreen document embedded (a direct child of the top document).
|
|
13
|
+
if (
|
|
14
|
+
window.credentialless !== true ||
|
|
15
|
+
window === window.top ||
|
|
16
|
+
window.parent !== window.top
|
|
17
|
+
) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let done = false;
|
|
22
|
+
const send = (message) =>
|
|
23
|
+
chrome.runtime.sendMessage({ target: TARGET, ...message }).catch(() => {
|
|
24
|
+
// nobody is listening anymore
|
|
25
|
+
done = true;
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// When to capture the HTML? Once all selectors match. With no selectors,
|
|
29
|
+
// right away, because then the delay is the whole wait. With no delay
|
|
30
|
+
// either, at the load event, because before that the document is empty.
|
|
31
|
+
const whenReady = (waitFor, delay, callback) => {
|
|
32
|
+
if (waitFor.length === 0) {
|
|
33
|
+
if (delay > 0 || document.readyState === 'complete') {
|
|
34
|
+
callback();
|
|
35
|
+
} else {
|
|
36
|
+
window.addEventListener('load', callback, { once: true });
|
|
37
|
+
}
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const isReady = () => waitFor.every((x) => document.querySelector(x));
|
|
42
|
+
if (isReady()) {
|
|
43
|
+
callback();
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const observer = new MutationObserver(() => {
|
|
47
|
+
if (isReady()) {
|
|
48
|
+
observer.disconnect();
|
|
49
|
+
callback();
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
observer.observe(document, { childList: true, subtree: true });
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
send({ type: 'config', url: location.href })
|
|
56
|
+
.then((config) => {
|
|
57
|
+
if (!config || done) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const { waitFor = [], delay = 0 } = config;
|
|
61
|
+
whenReady(waitFor, delay, () => {
|
|
62
|
+
// A selector can match before the element is finished filling in.
|
|
63
|
+
setTimeout(() => {
|
|
64
|
+
if (!done) {
|
|
65
|
+
done = true;
|
|
66
|
+
const html = document.documentElement.outerHTML;
|
|
67
|
+
if (html.length <= MAX_HTML_LENGTH) {
|
|
68
|
+
send({ type: 'content', html });
|
|
69
|
+
} else {
|
|
70
|
+
send({
|
|
71
|
+
type: 'permanent-error',
|
|
72
|
+
code: 'too-large',
|
|
73
|
+
details: { length: html.length },
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}, delay);
|
|
78
|
+
});
|
|
79
|
+
})
|
|
80
|
+
.catch((e) => {
|
|
81
|
+
// The configuration cannot be used (for instance, it could contain a
|
|
82
|
+
// selector that is not valid CSS). A retry will not fix that.
|
|
83
|
+
done = true;
|
|
84
|
+
send({
|
|
85
|
+
type: 'permanent-error',
|
|
86
|
+
code: 'bad-config',
|
|
87
|
+
details: { message: `${e}` },
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
})();
|
|
@@ -320,6 +320,143 @@ const TRANSFORMS = new Map(
|
|
|
320
320
|
requireString(text);
|
|
321
321
|
return text.trim();
|
|
322
322
|
},
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Removes the first match of a regular expression. The length cannot
|
|
326
|
+
* increase, because characters can only be removed, never added.
|
|
327
|
+
*
|
|
328
|
+
* Example ["removeFirstMatch", "AB+"]:
|
|
329
|
+
* - "xABBBy ABz" -> "xy ABz"
|
|
330
|
+
*
|
|
331
|
+
* @since: 9
|
|
332
|
+
*/
|
|
333
|
+
removeFirstMatch: (text, pattern) => {
|
|
334
|
+
requireString(text);
|
|
335
|
+
requireString(pattern);
|
|
336
|
+
if (text.length === 0) {
|
|
337
|
+
return '';
|
|
338
|
+
}
|
|
339
|
+
try {
|
|
340
|
+
return text.replace(new RegExp(pattern), '');
|
|
341
|
+
} catch (e) {
|
|
342
|
+
return '';
|
|
343
|
+
}
|
|
344
|
+
},
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Removes every match of a regular expression. The length cannot
|
|
348
|
+
* increase, because characters can only be removed, never added.
|
|
349
|
+
*
|
|
350
|
+
* Example ["removeAllMatches", "AB+"]:
|
|
351
|
+
* - "xABBBy ABz" -> "xy z"
|
|
352
|
+
*
|
|
353
|
+
* @since: 9
|
|
354
|
+
*/
|
|
355
|
+
removeAllMatches: (text, pattern) => {
|
|
356
|
+
requireString(text);
|
|
357
|
+
requireString(pattern);
|
|
358
|
+
if (text.length === 0) {
|
|
359
|
+
return '';
|
|
360
|
+
}
|
|
361
|
+
try {
|
|
362
|
+
return text.replaceAll(new RegExp(pattern, 'g'), '');
|
|
363
|
+
} catch (e) {
|
|
364
|
+
return '';
|
|
365
|
+
}
|
|
366
|
+
},
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Keeps only the first match of a regular expression (or an empty
|
|
370
|
+
* string if there is no match). The length cannot increase, because
|
|
371
|
+
* characters can only be removed, never added.
|
|
372
|
+
*
|
|
373
|
+
* Example ["selectFirstMatch", "AB+"]:
|
|
374
|
+
* - "xABBBy ABz" -> "ABBB"
|
|
375
|
+
*
|
|
376
|
+
* @since: 9
|
|
377
|
+
*/
|
|
378
|
+
selectFirstMatch: (text, pattern) => {
|
|
379
|
+
requireString(text);
|
|
380
|
+
requireString(pattern);
|
|
381
|
+
if (text.length === 0) {
|
|
382
|
+
return '';
|
|
383
|
+
}
|
|
384
|
+
try {
|
|
385
|
+
const m = new RegExp(pattern).exec(text);
|
|
386
|
+
return m ? m[0] : '';
|
|
387
|
+
} catch (e) {
|
|
388
|
+
return '';
|
|
389
|
+
}
|
|
390
|
+
},
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Keeps only the matches of a regular expression, concatenated in
|
|
394
|
+
* order (everything else is removed). The length cannot increase,
|
|
395
|
+
* because characters can only be removed, never added.
|
|
396
|
+
*
|
|
397
|
+
* Example ["selectAllMatches", "AB+"]:
|
|
398
|
+
* - "xABBBy ABz" -> "ABBBAB"
|
|
399
|
+
*
|
|
400
|
+
* @since: 9
|
|
401
|
+
*/
|
|
402
|
+
selectAllMatches: (text, pattern) => {
|
|
403
|
+
requireString(text);
|
|
404
|
+
requireString(pattern);
|
|
405
|
+
if (text.length === 0) {
|
|
406
|
+
return '';
|
|
407
|
+
}
|
|
408
|
+
try {
|
|
409
|
+
const regexp = new RegExp(pattern, 'g');
|
|
410
|
+
|
|
411
|
+
// Pathological patterns will always be slow, but we can defend against
|
|
412
|
+
// one class: if the pattern can match empty and we can prove it,
|
|
413
|
+
// we can switch to a more memory-efficient implementation, avoiding
|
|
414
|
+
// massive arrays of empty strings.
|
|
415
|
+
//
|
|
416
|
+
// The test('') heuristic is incomplete, but it catches common cases
|
|
417
|
+
// that could be introduced by accident (e.g. "[^a-zA-Z0-9]*").
|
|
418
|
+
const canMatchEmpty = regexp.test('');
|
|
419
|
+
if (canMatchEmpty) {
|
|
420
|
+
// "exec" is safer for empty-matching patterns. It avoids the worst case
|
|
421
|
+
// of creating massive arrays of empty strings.
|
|
422
|
+
let matches = [];
|
|
423
|
+
let m;
|
|
424
|
+
regexp.lastIndex = 0;
|
|
425
|
+
while ((m = regexp.exec(text)) !== null) {
|
|
426
|
+
if (m[0]) {
|
|
427
|
+
matches.push(m[0]);
|
|
428
|
+
} else {
|
|
429
|
+
regexp.lastIndex += 1;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return matches.join('');
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// "match" is slightly faster for non-empty-matching patterns
|
|
436
|
+
const matches = text.match(regexp);
|
|
437
|
+
return matches ? matches.join('') : '';
|
|
438
|
+
} catch (e) {
|
|
439
|
+
return '';
|
|
440
|
+
}
|
|
441
|
+
},
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* @since: 9
|
|
445
|
+
*/
|
|
446
|
+
maxLength: (text, size) => {
|
|
447
|
+
requireString(text);
|
|
448
|
+
requireInt(size);
|
|
449
|
+
return text.length <= size ? text : null;
|
|
450
|
+
},
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* @since: 9
|
|
454
|
+
*/
|
|
455
|
+
minLength: (text, size) => {
|
|
456
|
+
requireString(text);
|
|
457
|
+
requireInt(size);
|
|
458
|
+
return text.length >= size ? text : null;
|
|
459
|
+
},
|
|
323
460
|
}),
|
|
324
461
|
);
|
|
325
462
|
|
|
@@ -340,7 +477,7 @@ export function lookupBuiltinTransform(name) {
|
|
|
340
477
|
* to disable clients that do not meet the minimum requirements of the
|
|
341
478
|
* current patterns.
|
|
342
479
|
*/
|
|
343
|
-
const PATTERN_DSL_VERSION =
|
|
480
|
+
const PATTERN_DSL_VERSION = 9;
|
|
344
481
|
|
|
345
482
|
/**
|
|
346
483
|
* "Magic" empty rule set, which exists only if patterns were loaded, but
|
|
@@ -418,7 +555,7 @@ export default class Patterns {
|
|
|
418
555
|
return null;
|
|
419
556
|
}
|
|
420
557
|
const convert = (
|
|
421
|
-
{ followRedirects, headers, steps, emptyHtml, onError },
|
|
558
|
+
{ followRedirects, headers, steps, timeout, emptyHtml, onError },
|
|
422
559
|
target = {},
|
|
423
560
|
) => {
|
|
424
561
|
if (followRedirects) {
|
|
@@ -430,6 +567,9 @@ export default class Patterns {
|
|
|
430
567
|
if (steps) {
|
|
431
568
|
target.steps = steps;
|
|
432
569
|
}
|
|
570
|
+
if (timeout) {
|
|
571
|
+
target.timeout = timeout;
|
|
572
|
+
}
|
|
433
573
|
if (typeof emptyHtml === 'boolean') {
|
|
434
574
|
target.emptyHtml = emptyHtml;
|
|
435
575
|
}
|
|
@@ -40,6 +40,7 @@ import {
|
|
|
40
40
|
} from './steps/check-context.js';
|
|
41
41
|
import PageStore from './page-store.js';
|
|
42
42
|
import ChromeStorageMap from './utils/chrome-storage-map.js';
|
|
43
|
+
import { isRequestReportingSupported } from './support.js';
|
|
43
44
|
|
|
44
45
|
const DAY_CHANGE_INTERVAL = 20 * 1000;
|
|
45
46
|
const RECENTLY_MODIFIED_TTL = 30 * 1000;
|
|
@@ -68,6 +69,11 @@ export default class RequestReporter {
|
|
|
68
69
|
pauseState, // see Reporting#constructor (../reporting.js)
|
|
69
70
|
},
|
|
70
71
|
) {
|
|
72
|
+
if (!isRequestReportingSupported()) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
'RequestReporter requires documentId support in webRequest and webNavigation (Chromium >= 106, Safari >= 18.4, or Firefox >= 153)',
|
|
75
|
+
);
|
|
76
|
+
}
|
|
71
77
|
this.settings = settings;
|
|
72
78
|
this.onMessageReady = onMessageReady;
|
|
73
79
|
this.trustedClock = trustedClock;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WhoTracks.Me
|
|
3
|
+
* https://whotracks.me/
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2017-present Ghostery GmbH. All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
8
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
9
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// Chromium forks (Edge, Opera, Yandex) keep the Chrome/<major> token.
|
|
13
|
+
const DOCUMENT_ID_MIN_VERSIONS = [
|
|
14
|
+
[/Chrom(?:e|ium)\/(\d+)/, 106],
|
|
15
|
+
[/Firefox\/(\d+)/, 153],
|
|
16
|
+
[/Version\/(\d+(?:\.\d+)?).*Safari/, 18.4],
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
export function isRequestReportingSupported(
|
|
20
|
+
userAgent = globalThis.navigator?.userAgent,
|
|
21
|
+
) {
|
|
22
|
+
for (const [pattern, minVersion] of DOCUMENT_ID_MIN_VERSIONS) {
|
|
23
|
+
const match = userAgent?.match(pattern);
|
|
24
|
+
if (match) {
|
|
25
|
+
return parseFloat(match[1]) >= minVersion;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
}
|