Skip to content

Commit

Permalink
Format blocker file + use window session in adblocker
Browse files Browse the repository at this point in the history
  • Loading branch information
th-ch committed Apr 12, 2020
1 parent 3bbab52 commit 26dd7c5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion plugins/adblocker/back.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const { loadAdBlockerEngine } = require("./blocker");
module.exports = () => loadAdBlockerEngine(true);
module.exports = (win) => loadAdBlockerEngine(win.webContents.session);
19 changes: 10 additions & 9 deletions plugins/adblocker/blocker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,33 @@ const { promises } = require("fs"); // used for caching
const path = require("path");

const { ElectronBlocker } = require("@cliqz/adblocker-electron");
const { session } = require("electron");
const fetch = require("node-fetch");

const SOURCES = [
"https://raw.githubusercontent.com/kbinani/adblock-youtube-ads/master/signed.txt"
"https://raw.githubusercontent.com/kbinani/adblock-youtube-ads/master/signed.txt",
];

const loadAdBlockerEngine = (enableBlocking = false) =>
const loadAdBlockerEngine = (session = undefined) =>
ElectronBlocker.fromLists(
fetch,
SOURCES,
{},
{
path: path.resolve(__dirname, "ad-blocker-engine.bin"),
read: promises.readFile,
write: promises.writeFile
write: promises.writeFile,
}
)
.then(blocker => {
if (enableBlocking) {
blocker.enableBlockingInSession(session.defaultSession);
.then((blocker) => {
if (session) {
blocker.enableBlockingInSession(session);
} else {
console.log("Successfully generated adBlocker engine.");
}
})
.catch(err => console.log("Error loading adBlocker engine", err));
.catch((err) => console.log("Error loading adBlocker engine", err));

module.exports = { loadAdBlockerEngine };
if (require.main === module) {
loadAdBlockerEngine(false); // Generate the engine without enabling it
loadAdBlockerEngine(); // Generate the engine without enabling it
}

0 comments on commit 26dd7c5

Please sign in to comment.