Adblocker - advanced options (caching or not, additional lists)

This commit is contained in:
TC
2020-12-03 19:02:43 +01:00
parent 7b20b9339d
commit b94d0d4e8b
2 changed files with 27 additions and 9 deletions

View File

@ -1,2 +1,7 @@
const { loadAdBlockerEngine } = require("./blocker");
module.exports = (win) => loadAdBlockerEngine(win.webContents.session);
module.exports = (win, options) =>
loadAdBlockerEngine(
win.webContents.session,
options.cache,
options.additionalBlockLists
);

View File

@ -1,4 +1,4 @@
const { promises } = require("fs"); // used for caching
const { existsSync, promises, unlinkSync } = require("fs"); // used for caching
const path = require("path");
const { ElectronBlocker } = require("@cliqz/adblocker-electron");
@ -8,16 +8,28 @@ const SOURCES = [
"https://raw.githubusercontent.com/kbinani/adblock-youtube-ads/master/signed.txt",
];
const loadAdBlockerEngine = (session = undefined) =>
const loadAdBlockerEngine = (
session = undefined,
cache = true,
additionalBlockLists = []
) => {
const adBlockerCache = path.resolve(__dirname, "ad-blocker-engine.bin");
if (!cache && existsSync(adBlockerCache)) {
unlinkSync(adBlockerCache);
}
const cachingOptions = cache
? {
path: adBlockerCache,
read: promises.readFile,
write: promises.writeFile,
}
: undefined;
ElectronBlocker.fromLists(
fetch,
SOURCES,
[...SOURCES, ...additionalBlockLists],
{},
{
path: path.resolve(__dirname, "ad-blocker-engine.bin"),
read: promises.readFile,
write: promises.writeFile,
}
cachingOptions
)
.then((blocker) => {
if (session) {
@ -27,6 +39,7 @@ const loadAdBlockerEngine = (session = undefined) =>
}
})
.catch((err) => console.log("Error loading adBlocker engine", err));
};
module.exports = { loadAdBlockerEngine };
if (require.main === module) {