Skip to content

Instantly share code, notes, and snippets.

@chrmod
Created June 11, 2024 11:14
Show Gist options
  • Save chrmod/f26267a9fcd3f39d9adfd401f5c5b168 to your computer and use it in GitHub Desktop.
Save chrmod/f26267a9fcd3f39d9adfd401f5c5b168 to your computer and use it in GitHub Desktop.
Filter list preprocessor

Filter list preprocessor

Use to remove unsupported filters from a list

Usage

npx -- node ./preprocessor.js <path-to-list>
import { readFileSync, existsSync } from "node:fs";
import path from "node:path";
import { FiltersEngine } from "@cliqz/adblocker";
function preprocess(list, env) {
const engine = FiltersEngine.parse(list, {
loadPreprocessors: true,
debug: true,
});
engine.updateEnv(env);
const { networkFilters, cosmeticFilters } = engine.getFilters();
return [...networkFilters, ...cosmeticFilters]
.filter((filter) => !engine.preprocessors.isFilterExcluded(filter))
.map((filter) => filter.rawLine)
.sort();
}
const args = process.argv.slice(2);
// list of common envs https://github.com/ghostery/adblocker/blob/ca11ec5269d5657890c7195d11d5c06df0651c82/packages/adblocker/src/preprocessor.ts#L3
// by default all filters are accepted
// please specify ones that are meant to be expluded
const env = new Map([
['cap_html_filtering', false],
['env_firefox', false],
]);
const listPath = path.resolve(args[0]);
if (!existsSync(listPath)) {
throw new Error("Cannot find a filter list at location:", listPath);
}
const list = readFileSync(listPath, { encoding: 'utf-8' });
const filters = preprocess(list, env);
process.stdout.write(filters.join('\n'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment