Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update webextension-packages (location changes no longer need to be e… #2150

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading