Skip to content

Commit f25eb48

Browse files
committed
Generate the adblocker engine only once (by using built-in caching)
1 parent 6e45e7b commit f25eb48

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"build": "yarn run clean && build --win --mac --linux",
3636
"build:mac": "yarn run clean && build --mac",
3737
"build:win": "yarn run clean && build --win",
38-
"plugins": "yarn run plugin:autoconfirm",
38+
"plugins": "yarn run plugin:adblocker && yarn run plugin:autoconfirm",
39+
"plugin:adblocker": "rimraf plugins/adblocker/ad-blocker-engine.bin && node plugins/adblocker/blocker.js",
3940
"plugin:autoconfirm": "yarn run generate:package YoutubeNonStop",
4041
"release:linux": "yarn run clean && build --linux -p always",
4142
"release:mac": "yarn run clean && build --mac -p always",

plugins/adblocker/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/ad-blocker-engine.bin

plugins/adblocker/back.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,2 @@
1-
const { ElectronBlocker } = require("@cliqz/adblocker-electron");
2-
const { session } = require("electron");
3-
const fetch = require("node-fetch");
4-
5-
const SOURCES = [
6-
"https://raw.githubusercontent.com/kbinani/adblock-youtube-ads/master/signed.txt"
7-
];
8-
9-
module.exports = () =>
10-
ElectronBlocker.fromLists(fetch, SOURCES)
11-
.then(blocker => blocker.enableBlockingInSession(session.defaultSession))
12-
.catch(err => console.log("Error loading adBlocker", err));
1+
const { loadAdBlockerEngine } = require("./blocker");
2+
module.exports = () => loadAdBlockerEngine(true);

plugins/adblocker/blocker.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { promises } = require("fs"); // used for caching
2+
const path = require("path");
3+
4+
const { ElectronBlocker } = require("@cliqz/adblocker-electron");
5+
const { session } = require("electron");
6+
const fetch = require("node-fetch");
7+
8+
const SOURCES = [
9+
"https://raw.githubusercontent.com/kbinani/adblock-youtube-ads/master/signed.txt"
10+
];
11+
12+
const loadAdBlockerEngine = (enableBlocking = false) =>
13+
ElectronBlocker.fromLists(
14+
fetch,
15+
SOURCES,
16+
{},
17+
{
18+
path: path.resolve(__dirname, "ad-blocker-engine.bin"),
19+
read: promises.readFile,
20+
write: promises.writeFile
21+
}
22+
)
23+
.then(blocker => {
24+
if (enableBlocking) {
25+
blocker.enableBlockingInSession(session.defaultSession);
26+
}
27+
})
28+
.catch(err => console.log("Error loading adBlocker engine", err));
29+
30+
module.exports = { loadAdBlockerEngine };
31+
if (require.main === module) {
32+
loadAdBlockerEngine(false); // Generate the engine without enabling it
33+
}

0 commit comments

Comments
 (0)