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

$replace modifier #1842

Merged
merged 11 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
7 changes: 3 additions & 4 deletions extension-manifest-v3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@
"webextension-polyfill": "^0.12.0"
},
"dependencies": {
"@cliqz/adblocker": "^1.31.2",
"@cliqz/adblocker-content": "^1.31.2",
"@cliqz/adblocker-webextension": "^1.31.2",
"@cliqz/adblocker-webextension-cosmetics": "^1.31.2",
"@cliqz/adblocker": "^1.33.2",
"@cliqz/adblocker-webextension": "^1.33.2",
"@cliqz/adblocker-webextension-cosmetics": "^1.33.2",
"@duckduckgo/autoconsent": "^10.15.0",
"@ghostery/trackers-preview": "^1.0.0",
"@ghostery/ui": "^1.0.0",
Expand Down
39 changes: 21 additions & 18 deletions extension-manifest-v3/src/background/adblocker.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,17 +466,7 @@ if (__PLATFORM__ === 'firefox') {
const engine = engines.get(name);
if (!engine) continue;

if (details.type === 'main_frame') {
const htmlFilters = engine.getHtmlFilters(request);
if (htmlFilters.length !== 0) {
filterRequestHTML(
chrome.webRequest.filterResponseData,
request,
htmlFilters,
);
return;
}
} else {
if (details.type !== 'main_frame') {
const { redirect, match } = engine.match(request);

if (redirect !== undefined) {
Expand Down Expand Up @@ -508,23 +498,36 @@ if (__PLATFORM__ === 'firefox') {
}

const request = Request.fromRequestDetails(details);
let policies;
const cspPolicies = [];
const htmlFilters = [];

for (const name of resolveEngines(request, details.type)) {
const engine = engines.get(name);
if (!engine) continue;

policies = engine.getCSPDirectives(request);
if (policies !== undefined) {
break;
htmlFilters.push(...engine.getHtmlFilters(request));

if (details.type === 'main_frame') {
const policies = engine.getCSPDirectives(request);
if (policies !== undefined) {
cspPolicies.push(...policies);
}
}
}

if (policies) {
return updateResponseHeadersWithCSP(details, policies);
if (htmlFilters.length !== 0) {
filterRequestHTML(
chrome.webRequest.filterResponseData,
request,
htmlFilters,
);
}

if (cspPolicies.length !== 0) {
return updateResponseHeadersWithCSP(details, cspPolicies);
}
},
{ urls: ['<all_urls>'], types: ['main_frame'] },
{ urls: ['http://*/*', 'https://*/*'] },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we add the same constraint on other even listeners? I found a case where we have a callback for our own background page in Firefox (Navigation API) when Dev Tools are opened.
Sometimes I have an error in Chrome, that shows that page from chrome:// can't be accessed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, and it is unclear if onBeforeRequest should process <all_urls> or only http* and ws*.

['blocking', 'responseHeaders'],
);
}
2 changes: 2 additions & 0 deletions extension-manifest-v3/src/utils/engines.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const engines = new Map();
const ENV = new Map([
['ext_ghostery', true],
['cap_html_filtering', checkUserAgent('Firefox')],
// can be removed in once $replace support is sufficiently distributed
['cap_replace_modifier', checkUserAgent('Firefox')],
['env_firefox', checkUserAgent('Firefox')],
['env_chromium', checkUserAgent('Chrome')],
['env_edge', checkUserAgent('Edg')],
Expand Down
51 changes: 25 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading