Skip to content

Commit 627b5e4

Browse files
Setting of document for tracker analysis (#1613)
* Setting of document for tracker analysis * Add comments to explain global exposing code
1 parent f996039 commit 627b5e4

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

injected/src/trackers.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
import { getGlobal } from './utils.js';
2+
13
/**
24
* Check if the current document origin is on the tracker list, using the provided lookup trie.
35
* @param {object} trackerLookup Trie lookup of tracker domains
46
* @returns {boolean} True iff the origin is a tracker.
7+
*
8+
* Note: getGlobal() is used in testing to get the global object,
9+
* it's a work around for ESM modules are essentially singletons preventing overriding of global variables.
510
*/
6-
export function isTrackerOrigin(trackerLookup, originHostname = document.location.hostname) {
11+
export function isTrackerOrigin(trackerLookup, originHostname = getGlobal().document.location.hostname) {
712
const parts = originHostname.split('.').reverse();
813
let node = trackerLookup;
914
for (const sub of parts) {

injected/src/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ export function setGlobal(globalObjIn) {
4949
Error = globalObj.Error;
5050
}
5151

52+
/**
53+
* Used for testing to allow other files to override the globals used within this file.
54+
* @returns {globalThis} the global object
55+
*/
56+
export function getGlobal() {
57+
return globalObj;
58+
}
59+
5260
// linear feedback shift register to find a random approximation
5361
export function nextRandom(v) {
5462
return Math.abs((v >> 1) | (((v << 62) ^ (v << 61)) & (~(~0 << 63) << 62)));

0 commit comments

Comments
 (0)