Skip to content

Commit

Permalink
Update webextension-packages (location changes no longer need to be e…
Browse files Browse the repository at this point in the history
…xternally provided)
  • Loading branch information
philipp-classen committed Dec 16, 2024
1 parent c6fc2eb commit 0c41eec
Showing 1 changed file with 23 additions and 78 deletions.
101 changes: 23 additions & 78 deletions src/background/reporting/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import { setLogLevel, describeLoggers } from '@whotracksme/reporting/reporting';

import asyncSetup from '/utils/setup.js';
import debug from '/utils/debug.js';
import * as OptionsObserver from '/utils/options-observer.js';

Expand All @@ -36,89 +35,35 @@ import webRequestReporter from './webrequest-reporter.js';
}
})();

const setup = asyncSetup('reporting', [
OptionsObserver.addListener('terms', async function reporting(terms) {
if (terms) {
if (webRequestReporter) {
webRequestReporter.init().catch((e) => {
console.warn(
'Failed to initialize request reporting. Leaving the module disabled and continue.',
e,
);
});
}
await urlReporter.init().catch((e) => {
OptionsObserver.addListener('terms', function reporting(terms) {
if (terms) {
if (webRequestReporter) {
webRequestReporter.init().catch((e) => {
console.warn(
'Failed to initialize reporting. Leaving the module disabled and continue.',
'Failed to initialize request reporting. Leaving the module disabled and continue.',
e,
);
});
} else {
try {
urlReporter.unload();
} catch (e) {
console.error(e);
}
try {
webRequestReporter?.unload();
} catch (e) {
console.error(e);
}
}
}),
]);

async function onLocationChange(details) {
try {
setup.pending && (await setup.pending);
} catch (e) {
console.warn('Reporting is unavailable:', e);
return;
}

if (!urlReporter.isActive) return;

const { url, frameId, tabId } = details;
if (frameId !== 0 || url === 'about:blank' || url.startsWith('chrome://')) {
return;
}

// Be aware that the documentation of webNavigation.onCommitted is incomplete
// (https://developer.chrome.com/docs/extensions/reference/webNavigation/#event-onCommitted):
//
// > Fired when a navigation is committed. The document (and the resources
// > it refers to, such as images and subframes) might still be downloading,
// > but at least part of the document has been received from the server and
// > the browser has decided to switch to the new document.
//
// In practice, the event may also trigger for prefetch requests for which
// no tab exists. For instance, it can be reproduced in Chrome by starting
// a Google search from the address bar. Under certain conditions, the first
// search result triggers an extra onCommitted event (even if the user didn't
// click on the link yet).
const tab = await chrome.tabs.get(tabId).catch(() => null);
if (!tab) {
return;
}

// Don't leak information in private tabs (neither by storing on disk nor
// by initiating HTTP requests).
if (tab.incognito) {
return;
}

try {
await urlReporter.analyzeUrl(url);
} catch (e) {
console.warn('Unexpected error in reporting module:', e);
urlReporter.init().catch((e) => {
console.warn(
'Failed to initialize urlReporting. Leaving the module disabled and continue.',
e,
);
});
} else {
try {
urlReporter.unload();
} catch (e) {
console.error(e);
}
try {
webRequestReporter?.unload();
} catch (e) {
console.error(e);
}
}
}

chrome.webNavigation.onCommitted.addListener(onLocationChange);

if (__PLATFORM__ === 'chromium' || __PLATFORM__ === 'firefox') {
chrome.webNavigation.onHistoryStateUpdated.addListener(onLocationChange);
}
});

debug.WTM = {
communication,
Expand Down

0 comments on commit 0c41eec

Please sign in to comment.