mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-11 10:31:47 +00:00
49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
const { promises } = require("fs"); // used for caching
|
|
const path = require("path");
|
|
|
|
const { ElectronBlocker } = require("@cliqz/adblocker-electron");
|
|
const fetch = require("node-fetch");
|
|
|
|
const SOURCES = [
|
|
"https://raw.githubusercontent.com/kbinani/adblock-youtube-ads/master/signed.txt",
|
|
// uBlock Origin
|
|
"https://raw.githubusercontent.com/uBlockOrigin/uAssets/master/filters/filters.txt",
|
|
"https://raw.githubusercontent.com/uBlockOrigin/uAssets/master/filters/filters-2020.txt",
|
|
];
|
|
|
|
const loadAdBlockerEngine = (
|
|
session = undefined,
|
|
cache = true,
|
|
additionalBlockLists = [],
|
|
disableDefaultLists = false
|
|
) => {
|
|
// Only use cache if no additional blocklists are passed
|
|
const cachingOptions =
|
|
cache && additionalBlockLists.length === 0
|
|
? {
|
|
path: path.resolve(__dirname, "ad-blocker-engine.bin"),
|
|
read: promises.readFile,
|
|
write: promises.writeFile,
|
|
}
|
|
: undefined;
|
|
const lists = [
|
|
...(disableDefaultLists ? [] : SOURCES),
|
|
...additionalBlockLists,
|
|
];
|
|
|
|
ElectronBlocker.fromLists(fetch, lists, {}, cachingOptions)
|
|
.then((blocker) => {
|
|
if (session) {
|
|
blocker.enableBlockingInSession(session);
|
|
} else {
|
|
console.log("Successfully generated adBlocker engine.");
|
|
}
|
|
})
|
|
.catch((err) => console.log("Error loading adBlocker engine", err));
|
|
};
|
|
|
|
module.exports = { loadAdBlockerEngine };
|
|
if (require.main === module) {
|
|
loadAdBlockerEngine(); // Generate the engine without enabling it
|
|
}
|