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

PoC: integration with ghostery/adblocker#4499 and uBo origin #2120

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
nodejs 22.4.1
deno 2.0.3
1,047 changes: 674 additions & 373 deletions LICENSE

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ Ghostery relies on [contributions](https://github.com/ghostery/ghostery-extensio

## License

[MPL-2.0](https://www.mozilla.org/en-US/MPL/2.0/) Copyright 2017-present Ghostery GmbH. All rights reserved.
[GPL-3.0](https://www.gnu.org/licenses/gpl-3.0.en.html/) Copyright 2017-present Ghostery GmbH. All rights reserved.

See [LICENSE](LICENSE)
55 changes: 55 additions & 0 deletions deno.lock

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"locales": "npm run locales:setup && npm run locales.src",
"xcode-export-locales": "rm -rf ./xcode/en.xcloc && xcodebuild -exportLocalizations -project ./xcode/Ghostery.xcodeproj -localizationPath ./xcode",
"release": "./scripts/release.sh",
"update-scriptlets": "deno scripts/update-scriptlets.js --tagName 1.61.3b4 > ./src/rule_resources/scriptlets.js",
"package": "./scripts/package.sh"
},
"devDependencies": {
Expand Down Expand Up @@ -66,5 +67,5 @@
"email": "info@ghostery.com",
"url": "https://www.ghostery.com"
},
"license": "MPL-2.0"
"license": "GPL-3.0"
}
7 changes: 7 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@
resolve(options.outDir, 'rule_resources', 'engine-trackerdb.dat'),
);

// copy scriptlets
cpSync(
resolve(options.srcDir, 'rule_resources', 'scriptlets.js'),
resolve(options.outDir, 'rule_resources', 'scriptlets.js'),
);


Check failure on line 220 in scripts/build.js

View workflow job for this annotation

GitHub Actions / test

Delete `⏎`
// copy managed storage configuration
if (manifest.storage?.managed_schema) {
const path = resolve(options.srcDir, manifest.storage.managed_schema);
Expand Down
70 changes: 70 additions & 0 deletions scripts/update-scriptlets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// To be run in Deno

const tagName = process.argv.includes('--tagName')
? process.argv[process.argv.findIndex((o) => o === '--tagName') + 1]
: false;

if (!tagName) {
throw new Error('pass argument --tagName <TAG_NAME>');
}

const { builtinScriptlets: scriptlets } = await import(
`https://raw.githubusercontent.com/gorhill/uBlock/${tagName}/src/js/resources/scriptlets.js`
);

console.log(`/*******************************************************************************

uBlock Origin - a comprehensive, efficient content blocker
Copyright (C) 2019-present Raymond Hill

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.

Home: https://github.com/gorhill/uBlock

*/
const scriptlets = {};
${scriptlets
.map((scriptlet) => {
// Make full function
const fnLiteral = scriptlet.fn.toString();
if (fnLiteral.startsWith('class ')) {
return fnLiteral;
}

// Extract function call name
const callNameEndsAt = fnLiteral.indexOf('(');
if (callNameEndsAt === -1) {
return '';
}
const callName = fnLiteral.slice('function '.length, callNameEndsAt);

// Handle aliases
const names = [scriptlet.name];
if (scriptlet.aliases !== undefined) {
for (const alias of scriptlet.aliases) {
names.push(alias);
}
}

return `${fnLiteral}
const __${callName} = {
callName: "${callName}",
dependencies: ${JSON.stringify(scriptlet.dependencies || [])},
fn: ${callName},
};
${names.map((name) => `scriptlets["${name}"] = __${callName};`).join('\n')}`;
})
.join('\n\n')}

export default scriptlets;`);
71 changes: 66 additions & 5 deletions src/background/adblocker.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { debugMode } from '/utils/debug.js';

import { tabStats, updateTabStats } from './stats.js';
import { getException } from './exceptions.js';
import { default as scriptlets } from '../rule_resources/scriptlets.js';

let options = Options;

Expand Down Expand Up @@ -260,7 +261,7 @@ async function injectCosmetics(details, config) {
const isBootstrap = config.bootstrap;

{
const cosmetics = engine.getCosmeticsFilters({
const { matches, allowGenericHides } = engine.matchCosmeticFilters({
domain,
hostname,
url,
Expand All @@ -281,12 +282,72 @@ async function injectCosmetics(details, config) {
getRulesFromDOM: !isBootstrap,
});

if (isBootstrap && cosmetics.scripts.length > 0) {
injectScriptlets(cosmetics.scripts, tabId, frameId);
// Scriptlet sources including all dependencies
const includes = new Set();
// A list of call signatures extracted from filters
const signatures = [];
for (const match of matches) {
if (
match.exception !== undefined ||
match.filter.isScriptInject() === false
) {
continue;
}

const parsed = match.filter.parseScript();
if (parsed === undefined) {
continue;
}

const scriptlet = scriptlets[parsed.name];
if (scriptlet === undefined) {
continue;
}

includes.add(scriptlet.fn.toString());

const resolves = [...scriptlet.dependencies];
while (resolves.length > 0) {
const dependency = scriptlets[resolves.shift()];
includes.add(dependency.fn.toString());

// Flatten dependencies of dependencies to resovles queue.
if (dependency !== undefined) {
resolves.push(...dependency.dependencies);
}
}

signatures.push(
`${scriptlet.callName}(${JSON.stringify(scriptlet.args)});`,
);
}

if (signatures.length !== 0) {
let script = '';
for (const include of includes) {
script += include + '\n';
}
for (const signature of signatures) {
script += signature + '\n';
}

injectScriptlets(script, tabId, frameId);
}

if (cosmetics.styles) {
injectStyles(cosmetics.styles, tabId, frameId);
const { styles } = engine.injectCosmeticFilters({
url,

injectStyles: true,
injectScriptlets: false,
injectExtended: isBootstrap,
injectPureHasSafely: isBootstrap,

allowGenericHides,
getBaseRules: false,
});

if (styles) {
injectStyles(styles, tabId, frameId);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ import './reporting/index.js';
import './telemetry/index.js';

import './devtools.js';
import scriptlets from '../rule_resources/scriptlets.js';
globalThis.scriptlets = scriptlets;
Loading