@whotracksme/reporting 12.1.24 β†’ 12.1.25

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 CHANGED
@@ -1,3 +1,15 @@
1
+ # v12.1.25 (Tue Nov 24 2026)
2
+
3
+ #### πŸ› Bug Fix
4
+
5
+ - Fixes multiple bugs in the EventListenerQueue and its setup in Pages [#267](https://github.com/whotracksme/webextension-packages/pull/267) ([@philipp-classen](https://github.com/philipp-classen))
6
+
7
+ #### Authors: 1
8
+
9
+ - Philipp Claßen ([@philipp-classen](https://github.com/philipp-classen))
10
+
11
+ ---
12
+
1
13
  # v12.1.24 (Mon Nov 23 2026)
2
14
 
3
15
  #### πŸ› Bug Fix
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whotracksme/reporting",
3
- "version": "12.1.24",
3
+ "version": "12.1.25",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  "./sanitizer": "./reporting/src/sanitizer.js",
@@ -36,12 +36,8 @@ export default class EventListenerQueue {
36
36
  this._bufferedEvents = [];
37
37
  this._uninstallHandlers = [];
38
38
  listeners.forEach(({ method, api, type }) => {
39
- if (
40
- chrome &&
41
- chrome[api] &&
42
- chrome[api][type]?.addListener &&
43
- chrome[api][type]?.removeListener
44
- ) {
39
+ const event = globalThis.chrome?.[api]?.[type];
40
+ if (event?.addListener && event?.removeListener) {
45
41
  const handler = (...args) => {
46
42
  this._bufferedEvents.push({ method, args });
47
43
  if (
@@ -53,10 +49,27 @@ export default class EventListenerQueue {
53
49
  this._bufferedEvents.shift();
54
50
  }
55
51
  };
56
- chrome[api][type].addListener(handler);
57
- this._uninstallHandlers.push(() => {
58
- chrome[api][type].removeListener(handler);
59
- });
52
+
53
+ try {
54
+ if (api === 'webRequest') {
55
+ // webRequest API may refuse listeners without a filter.
56
+ // (Note: keep this part in sync with the setup in pages.js)
57
+ const filter = { urls: ['<all_urls>'] };
58
+ try {
59
+ event.addListener(handler, filter, ['responseHeaders']);
60
+ } catch {
61
+ event.addListener(handler, filter);
62
+ }
63
+ } else {
64
+ event.addListener(handler);
65
+ }
66
+ this._uninstallHandlers.push(() => {
67
+ event.removeListener(handler);
68
+ });
69
+ } catch (e) {
70
+ this.detectedErrors = true;
71
+ logger.warn('Cannot queue api', method, e);
72
+ }
60
73
  } else {
61
74
  logger.debug(
62
75
  'Cannot queue api',
@@ -433,7 +433,7 @@ export default class Pages {
433
433
  }
434
434
 
435
435
  if (this.isActive) {
436
- eventListenerQueue.replayEvents();
436
+ eventListenerQueue.replayEvents(this);
437
437
  this._setupListener();
438
438
  }
439
439
  this._updateSession();
@@ -1968,7 +1968,7 @@ export default class Pages {
1968
1968
  * ]
1969
1969
  */
1970
1970
  static describeListeners() {
1971
- const listeners = Object.getOwnPropertyNames(Pages);
1971
+ const listeners = Object.getOwnPropertyNames(Pages.prototype);
1972
1972
  return ['webRequest', 'webNavigation', 'tabs', 'windows'].flatMap((api) => {
1973
1973
  const prefix = `chrome_${api}_`;
1974
1974
  return listeners
@@ -0,0 +1,164 @@
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
+ import { expect } from 'chai';
13
+
14
+ import EventListenerQueue from '../src/event-listener-queue.js';
15
+ import Pages from '../src/pages.js';
16
+
17
+ class ListenerMock {
18
+ constructor() {
19
+ this._listeners = new Set();
20
+ }
21
+
22
+ addListener(handler) {
23
+ this._listeners.add(handler);
24
+ }
25
+
26
+ removeListener(handler) {
27
+ this._listeners.delete(handler);
28
+ }
29
+
30
+ hasListeners() {
31
+ return this._listeners.size > 0;
32
+ }
33
+
34
+ dispatch(...args) {
35
+ this._listeners.forEach((handler) => handler(...args));
36
+ }
37
+ }
38
+
39
+ // Like the real API: a listener without a filter is an error. Also,
40
+ // not all events accept 'responseHeaders' (e.g. onBeforeRequest).
41
+ class WebRequestListenerMock extends ListenerMock {
42
+ constructor({ supportsResponseHeaders }) {
43
+ super();
44
+ this.supportsResponseHeaders = supportsResponseHeaders;
45
+ }
46
+
47
+ addListener(handler, filter, extraInfoSpec = []) {
48
+ if (!filter?.urls) {
49
+ throw new TypeError('No matching signature: filter is missing');
50
+ }
51
+ if (
52
+ extraInfoSpec.includes('responseHeaders') &&
53
+ !this.supportsResponseHeaders
54
+ ) {
55
+ throw new TypeError('Invalid value for extraInfoSpec');
56
+ }
57
+ super.addListener(handler);
58
+ }
59
+ }
60
+
61
+ function newChromeApiMock() {
62
+ return {
63
+ tabs: {
64
+ query: async () => [],
65
+ onCreated: new ListenerMock(),
66
+ onUpdated: new ListenerMock(),
67
+ onRemoved: new ListenerMock(),
68
+ onActivated: new ListenerMock(),
69
+ },
70
+ webNavigation: {
71
+ onBeforeNavigate: new ListenerMock(),
72
+ onCommitted: new ListenerMock(),
73
+ },
74
+ webRequest: {
75
+ onBeforeRequest: new WebRequestListenerMock({
76
+ supportsResponseHeaders: false,
77
+ }),
78
+ onBeforeRedirect: new WebRequestListenerMock({
79
+ supportsResponseHeaders: true,
80
+ }),
81
+ },
82
+ };
83
+ }
84
+
85
+ function allListenerMocks(chrome) {
86
+ return Object.values(chrome).flatMap((api) =>
87
+ Object.values(api).filter((x) => x instanceof ListenerMock),
88
+ );
89
+ }
90
+
91
+ describe('#EventListenerQueue', function () {
92
+ let originalChrome;
93
+ let chrome;
94
+ let uut;
95
+
96
+ function newEventListenerQueue() {
97
+ return new EventListenerQueue({ listeners: Pages.describeListeners() });
98
+ }
99
+
100
+ beforeEach(function () {
101
+ originalChrome = globalThis.chrome;
102
+ chrome = newChromeApiMock();
103
+ globalThis.chrome = chrome;
104
+ });
105
+
106
+ afterEach(function () {
107
+ uut?.close();
108
+ uut = null;
109
+ globalThis.chrome = originalChrome;
110
+ });
111
+
112
+ it('should listen to all events that are available', function () {
113
+ uut = newEventListenerQueue();
114
+ for (const listener of allListenerMocks(chrome)) {
115
+ expect(listener.hasListeners()).to.be.true;
116
+ }
117
+ });
118
+
119
+ it('should keep the events, and replay them in the same order', function () {
120
+ uut = newEventListenerQueue();
121
+ const tab = { id: 1, url: 'https://example.test/' };
122
+ chrome.webRequest.onBeforeRequest.dispatch({ tabId: 1, type: 'image' });
123
+ chrome.tabs.onUpdated.dispatch(1, { status: 'loading' }, tab);
124
+ chrome.webNavigation.onCommitted.dispatch({ tabId: 1, frameId: 0 });
125
+
126
+ const calls = [];
127
+ const target = Object.fromEntries(
128
+ Pages.describeListeners().map(({ method }) => [
129
+ method,
130
+ (...args) => calls.push({ method, args }),
131
+ ]),
132
+ );
133
+ uut.replayEvents(target);
134
+
135
+ expect(calls).to.eql([
136
+ {
137
+ method: 'chrome_webRequest_onBeforeRequest',
138
+ args: [{ tabId: 1, type: 'image' }],
139
+ },
140
+ {
141
+ method: 'chrome_tabs_onUpdated',
142
+ args: [1, { status: 'loading' }, tab],
143
+ },
144
+ {
145
+ method: 'chrome_webNavigation_onCommitted',
146
+ args: [{ tabId: 1, frameId: 0 }],
147
+ },
148
+ ]);
149
+ });
150
+
151
+ it('should remove its listeners after the replay', function () {
152
+ uut = newEventListenerQueue();
153
+ uut.replayEvents({});
154
+ for (const listener of allListenerMocks(chrome)) {
155
+ expect(listener.hasListeners()).to.be.false;
156
+ }
157
+ });
158
+
159
+ it('should not fail if the chrome API is missing', function () {
160
+ delete globalThis.chrome;
161
+ uut = newEventListenerQueue();
162
+ uut.replayEvents({});
163
+ });
164
+ });
@@ -14,6 +14,7 @@ import './country-provider.spec.js';
14
14
  import './persisted-hashes.spec.js';
15
15
  import './doublefetch-unbreak.spec.js';
16
16
  import './url-analyzer.spec.js';
17
+ import './event-listener-queue.spec.js';
17
18
  import './alive-check.spec.js';
18
19
  import './alive-message-generator.spec.js';
19
20
  import './message-sender.spec.js';
@@ -11,7 +11,11 @@
11
11
 
12
12
  import { expect } from 'chai';
13
13
 
14
- import Pages, { stripLazyVars, CANCEL_LAZY_VAR } from '../src/pages.js';
14
+ import Pages, {
15
+ stripLazyVars,
16
+ CANCEL_LAZY_VAR,
17
+ eventListenerQueue,
18
+ } from '../src/pages.js';
15
19
  import UrlAnalyzer from '../src/url-analyzer.js';
16
20
  import SessionStorageWrapper from '../src/session-storage.js';
17
21
  import Patterns from '../src/patterns.js';
@@ -249,6 +253,35 @@ describe('#Pages', function () {
249
253
  });
250
254
  });
251
255
 
256
+ describe('#describeListeners', function () {
257
+ it('should find the listeners for the browser events', function () {
258
+ const listeners = Pages.describeListeners();
259
+ for (const expected of [
260
+ { method: 'chrome_tabs_onUpdated', api: 'tabs', type: 'onUpdated' },
261
+ {
262
+ method: 'chrome_webNavigation_onCommitted',
263
+ api: 'webNavigation',
264
+ type: 'onCommitted',
265
+ },
266
+ {
267
+ method: 'chrome_webRequest_onBeforeRedirect',
268
+ api: 'webRequest',
269
+ type: 'onBeforeRedirect',
270
+ },
271
+ {
272
+ method: 'chrome_windows_onFocusChanged',
273
+ api: 'windows',
274
+ type: 'onFocusChanged',
275
+ },
276
+ ]) {
277
+ expect(listeners).to.deep.include(expected);
278
+ }
279
+ for (const { method } of listeners) {
280
+ expect(Pages.prototype[method]).to.be.a('function');
281
+ }
282
+ });
283
+ });
284
+
252
285
  describe('with a complete chrome API stub', function () {
253
286
  let uut;
254
287
  let chromeApiMock;
@@ -592,6 +625,68 @@ describe('#Pages', function () {
592
625
  });
593
626
  });
594
627
 
628
+ describe('with events that came in during the startup', function () {
629
+ const TAB_ID = 7;
630
+ const PAGE_URL = 'https://www.example.com/article';
631
+
632
+ function tab(status) {
633
+ return {
634
+ id: TAB_ID,
635
+ windowId: 1,
636
+ incognito: false,
637
+ active: true,
638
+ status,
639
+ title: 'Some article',
640
+ url: PAGE_URL,
641
+ };
642
+ }
643
+
644
+ it('should process the events when it is ready', async function () {
645
+ const navigations = [];
646
+ uut.addObserver((event) => {
647
+ if (event.type === 'safe-page-navigation') {
648
+ navigations.push(event.url);
649
+ }
650
+ });
651
+
652
+ // The service worker starts, because the user opens a page.
653
+ // These events come in while "Pages" is still loading its state.
654
+ eventListenerQueue._bufferedEvents.push(
655
+ {
656
+ method: 'chrome_tabs_onUpdated',
657
+ args: [
658
+ TAB_ID,
659
+ { status: 'loading', url: PAGE_URL },
660
+ tab('loading'),
661
+ ],
662
+ },
663
+ {
664
+ method: 'chrome_webNavigation_onCommitted',
665
+ args: [
666
+ {
667
+ tabId: TAB_ID,
668
+ frameId: 0,
669
+ url: PAGE_URL,
670
+ transitionType: 'link',
671
+ },
672
+ ],
673
+ },
674
+ {
675
+ method: 'chrome_tabs_onUpdated',
676
+ args: [TAB_ID, { status: 'complete' }, tab('complete')],
677
+ },
678
+ );
679
+ await uut.init();
680
+ await uut.blockUntilFullInit();
681
+
682
+ expect(uut.describeTab(TAB_ID)).to.include({
683
+ url: PAGE_URL,
684
+ status: 'complete',
685
+ });
686
+ expect(navigations).to.eql([PAGE_URL]);
687
+ });
688
+ });
689
+
595
690
  describe('when fully initialized', function () {
596
691
  beforeEach(async () => {
597
692
  await uut.init();