Skip to content

Commit

Permalink
Adblocker - advanced options (caching or not, additional lists)
Browse files Browse the repository at this point in the history
  • Loading branch information
th-ch committed Dec 3, 2020
1 parent 7b20b93 commit b94d0d4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
7 changes: 6 additions & 1 deletion plugins/adblocker/back.js
Original file line number Diff line number Diff line change
@@ -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
);
29 changes: 21 additions & 8 deletions plugins/adblocker/blocker.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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) {
Expand All @@ -27,6 +39,7 @@ const loadAdBlockerEngine = (session = undefined) =>
}
})
.catch((err) => console.log("Error loading adBlocker engine", err));
};

module.exports = { loadAdBlockerEngine };
if (require.main === module) {
Expand Down

0 comments on commit b94d0d4

Please sign in to comment.