From f25eb48f795cafd63045b05ace75de2f63dfa51e Mon Sep 17 00:00:00 2001 From: TC Date: Sun, 12 Apr 2020 14:17:38 +0200 Subject: [PATCH] Generate the adblocker engine only once (by using built-in caching) --- package.json | 3 ++- plugins/adblocker/.gitignore | 1 + plugins/adblocker/back.js | 14 ++------------ plugins/adblocker/blocker.js | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 plugins/adblocker/.gitignore create mode 100644 plugins/adblocker/blocker.js diff --git a/package.json b/package.json index 93e121a3b2..4492c9607f 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,8 @@ "build": "yarn run clean && build --win --mac --linux", "build:mac": "yarn run clean && build --mac", "build:win": "yarn run clean && build --win", - "plugins": "yarn run plugin:autoconfirm", + "plugins": "yarn run plugin:adblocker && yarn run plugin:autoconfirm", + "plugin:adblocker": "rimraf plugins/adblocker/ad-blocker-engine.bin && node plugins/adblocker/blocker.js", "plugin:autoconfirm": "yarn run generate:package YoutubeNonStop", "release:linux": "yarn run clean && build --linux -p always", "release:mac": "yarn run clean && build --mac -p always", diff --git a/plugins/adblocker/.gitignore b/plugins/adblocker/.gitignore new file mode 100644 index 0000000000..af053a2be1 --- /dev/null +++ b/plugins/adblocker/.gitignore @@ -0,0 +1 @@ +/ad-blocker-engine.bin diff --git a/plugins/adblocker/back.js b/plugins/adblocker/back.js index 99ee613ff5..caf0cc9758 100644 --- a/plugins/adblocker/back.js +++ b/plugins/adblocker/back.js @@ -1,12 +1,2 @@ -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" -]; - -module.exports = () => - ElectronBlocker.fromLists(fetch, SOURCES) - .then(blocker => blocker.enableBlockingInSession(session.defaultSession)) - .catch(err => console.log("Error loading adBlocker", err)); +const { loadAdBlockerEngine } = require("./blocker"); +module.exports = () => loadAdBlockerEngine(true); diff --git a/plugins/adblocker/blocker.js b/plugins/adblocker/blocker.js new file mode 100644 index 0000000000..452924f59c --- /dev/null +++ b/plugins/adblocker/blocker.js @@ -0,0 +1,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" +]; + +const loadAdBlockerEngine = (enableBlocking = false) => + ElectronBlocker.fromLists( + fetch, + SOURCES, + {}, + { + path: path.resolve(__dirname, "ad-blocker-engine.bin"), + read: promises.readFile, + write: promises.writeFile + } + ) + .then(blocker => { + if (enableBlocking) { + blocker.enableBlockingInSession(session.defaultSession); + } + }) + .catch(err => console.log("Error loading adBlocker engine", err)); + +module.exports = { loadAdBlockerEngine }; +if (require.main === module) { + loadAdBlockerEngine(false); // Generate the engine without enabling it +}