From 5e3b7c0db7ee3b38b14d9815998439a73d4208e4 Mon Sep 17 00:00:00 2001 From: TC Date: Sat, 11 Apr 2020 12:30:15 +0200 Subject: [PATCH] Migrate adblock plugin to adblock-rs --- package.json | 3 +- plugins/adblocker/.gitignore | 3 +- plugins/adblocker/blocker.js | 3 +- plugins/adblocker/contains-ads.js | 46 +++++++++++-------- plugins/adblocker/downloader.js | 37 +++++++++++++++ plugins/adblocker/filter-lists/.keep | 0 plugins/adblocker/generator.js | 67 ---------------------------- 7 files changed, 69 insertions(+), 90 deletions(-) create mode 100644 plugins/adblocker/downloader.js create mode 100644 plugins/adblocker/filter-lists/.keep delete mode 100644 plugins/adblocker/generator.js diff --git a/package.json b/package.json index 1f795d40..9e8e6ddf 100644 --- a/package.json +++ b/package.json @@ -30,11 +30,12 @@ "start": "electron .", "icon": "rimraf assets/generated && electron-icon-maker --input=assets/youtube-music.png --output=assets/generated", "generate:package": "node utils/generate-package-json.js", - "postinstall": "yarn run generate:package YoutubeNonStop && yarn run icon && npm rebuild && node plugins/adblocker/generator.js && electron-rebuild", + "postinstall": "yarn run generate:package YoutubeNonStop && yarn run icon && yarn run plugin:adblock", "clean": "rimraf dist", "build": "yarn run clean && build --win --mac --linux", "build:mac": "yarn run clean && build --mac", "build:win": "yarn run clean && build --win", + "plugin:adblock": "node plugins/adblocker/downloader.js && electron-build-env neon build adblock-rs", "release:linux": "yarn run clean && build --linux -p always", "release:mac": "yarn run clean && build --mac -p always", "release:win": "yarn run clean && build --win -p always" diff --git a/plugins/adblocker/.gitignore b/plugins/adblocker/.gitignore index 2284fd44..c40f1081 100644 --- a/plugins/adblocker/.gitignore +++ b/plugins/adblocker/.gitignore @@ -1 +1,2 @@ -detector.buffer +/filter-lists/* +!/filter-lists/.keep diff --git a/plugins/adblocker/blocker.js b/plugins/adblocker/blocker.js index 48788dbe..063d516f 100644 --- a/plugins/adblocker/blocker.js +++ b/plugins/adblocker/blocker.js @@ -1,7 +1,6 @@ -const { initialize, containsAds } = require("./contains-ads"); +const { containsAds } = require("./contains-ads"); module.exports.blockWindowAds = webContents => { - initialize(); webContents.session.webRequest.onBeforeRequest( ["*://*./*"], (details, cb) => { diff --git a/plugins/adblocker/contains-ads.js b/plugins/adblocker/contains-ads.js index ebf91512..8fbc2ea8 100644 --- a/plugins/adblocker/contains-ads.js +++ b/plugins/adblocker/contains-ads.js @@ -1,24 +1,32 @@ const fs = require("fs"); -const path = require("path"); -const Blocker = require("ad-block"); +const path = require("path"); -const client = new Blocker.AdBlockClient(); -const file = path.resolve(__dirname, "detector.buffer"); +const AdBlockClient = require("adblock-rs"); +const is = require("electron-is"); -module.exports.client = client; -module.exports.initialize = () => - new Promise((resolve, reject) => { - fs.readFile(file, (err, buffer) => { - if (err) { - return reject(err); - } - client.deserialize(buffer); - return resolve(); - }); - }); +const sourcesFolder = path.resolve(__dirname, "filter-lists"); +const filterLists = fs + .readdirSync(sourcesFolder) + .filter(filename => filename.includes(".txt")) + .map(filename => + fs + .readFileSync(path.resolve(sourcesFolder, filename), { + encoding: "utf-8" + }) + .split("\n") + ); -const none = Blocker.FilterOptions.noFilterOption; -const isAd = (req, base) => client.matches(req, none, base); +const rules = [].concat(...filterLists); +const debug = is.dev(); +const client = new AdBlockClient.Engine(rules, debug); -module.exports.containsAds = (req, base) => isAd(req, base); -module.exports.isAd = isAd; +if (debug) { + const serializedArrayBuffer = client.serialize(); // Serialize the engine to an ArrayBuffer + console.log( + `AdBlock engine size: ${(serializedArrayBuffer.byteLength / 1024).toFixed( + 2 + )} KB` + ); +} + +module.exports.containsAds = (req, base) => client.check(req, base || "", ""); diff --git a/plugins/adblocker/downloader.js b/plugins/adblocker/downloader.js new file mode 100644 index 00000000..09d1c946 --- /dev/null +++ b/plugins/adblocker/downloader.js @@ -0,0 +1,37 @@ +// This file downloads the configured adblock lists +const fs = require("fs"); +const https = require("https"); +const path = require("path"); + +const SOURCES = [ + { + name: "youtube-ads", + url: + "https://raw.githubusercontent.com/kbinani/adblock-youtube-ads/master/signed.txt" + } +]; + +function downloadAdblockLists(sources = SOURCES) { + // fetch updated versions + sources.forEach(source => { + console.log(`Downloading list "${source.name}" (${source.url})`); + https + .get(source.url, response => { + const filepath = path.resolve( + __dirname, + "filter-lists", + `${source.name}.txt` + ); + const file = fs.createWriteStream(filepath); + response.pipe(file); + }) + .on("error", err => { + console.log("Error: " + err.message); + }); + }); +} + +module.exports = downloadAdblockLists; +if (require.main === module) { + downloadAdblockLists(); +} diff --git a/plugins/adblocker/filter-lists/.keep b/plugins/adblocker/filter-lists/.keep new file mode 100644 index 00000000..e69de29b diff --git a/plugins/adblocker/generator.js b/plugins/adblocker/generator.js deleted file mode 100644 index dce771da..00000000 --- a/plugins/adblocker/generator.js +++ /dev/null @@ -1,67 +0,0 @@ -// This file generates the detector buffer -const fs = require("fs"); -const path = require("path"); -const Blocker = require("ad-block"); -const https = require("https"); - -const SOURCES = [ - "https://raw.githubusercontent.com/kbinani/adblock-youtube-ads/master/signed.txt" -]; - -function parseAdblockList(client, adblockList) { - const urls = adblockList.split("\n"); - const totalSize = urls.length; - console.log( - "Parsing " + totalSize + " urls (this can take a couple minutes)." - ); - urls.map(line => client.parse(line)); - console.log("Created buffer."); -} - -function writeBuffer(client) { - const output = path.resolve(__dirname, "detector.buffer"); - fs.writeFile(output, client.serialize(64), err => { - if (err) { - console.error(err); - return; - } - console.log("Wrote buffer to detector.buffer!"); - }); -} - -function generateDetectorBuffer() { - const client = new Blocker.AdBlockClient(); - let nbSourcesFetched = 0; - - // fetch updated versions - SOURCES.forEach(source => { - console.log("Downloading " + source); - https - .get(source, resp => { - let data = ""; - - // A chunk of data has been recieved. - resp.on("data", chunk => { - data += chunk; - }); - - // The whole response has been received. Print out the result. - resp.on("end", () => { - parseAdblockList(client, data); - nbSourcesFetched++; - - if (nbSourcesFetched === SOURCES.length) { - writeBuffer(client); - } - }); - }) - .on("error", err => { - console.log("Error: " + err.message); - }); - }); -} - -module.exports = generateDetectorBuffer; -if (require.main === module) { - generateDetectorBuffer(); -}