Use to remove unsupported filters from a list
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')); |