Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON adblock resources support #74

Merged
merged 2 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "submodules/uBlock"]
path = submodules/uBlock
url = https://github.com/brave/uBlock
38 changes: 23 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Packages component and theme extensions used in the Brave browser",
"dependencies": {
"ad-block": "brave/ad-block",
"adblock-rs": "^0.1.28",
"adblock-rs": "^0.1.36",
"ajv": "^6.10.0",
"autoplay-whitelist": "github:brave/autoplay-whitelist",
"aws-sdk": "^2.449.0",
Expand Down
62 changes: 47 additions & 15 deletions scripts/generateAdBlockRustDataFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,34 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

const { Engine, lists } = require('adblock-rs')
const { Engine, lists, uBlockResources } = require('adblock-rs')
const path = require('path')
const fs = require('fs')
const request = require('request')

const uBlockRevision = '978f04219a1f41f3fab7ad5c38ec2a1a021920fe'
const uBlockGitArchiveURL = `https://github.com/gorhill/uBlock/archive/${uBlockRevision}.zip`
antonok-edm marked this conversation as resolved.
Show resolved Hide resolved

const uBlockLocalRoot = 'submodules/uBlock'
const uBlockWebAccessibleResources = path.join(uBlockLocalRoot, 'src/web_accessible_resources')
const uBlockRedirectEngine = path.join(uBlockLocalRoot, 'src/js/redirect-engine.js')
const uBlockScriptlets = path.join(uBlockLocalRoot, 'assets/resources/scriptlets.js')

/**
* Returns a promise that generates a resources file from the uBlock Origin
* repo hosted on GitHub
*/
const generateResourcesFile = (uBlockArchiveZip) => {
return new Promise((resolve, reject) => {
const jsonData = JSON.stringify(uBlockResources(
uBlockWebAccessibleResources,
uBlockRedirectEngine,
uBlockScriptlets
))
fs.writeFileSync(getOutPath('resources.json', 'default'), jsonData, 'utf8')
})
}

/**
* Returns a promise that which resolves with the list data
*
Expand Down Expand Up @@ -47,6 +70,25 @@ const getListFilterFunction = (uuid) => {
return undefined
}

/**
* Obtains the output path to store a file given the specied name and subdir
*/
const getOutPath = (outputFilename, outSubdir) => {
let outPath = path.join('build')
if (!fs.existsSync(outPath)) {
fs.mkdirSync(outPath)
}
outPath = path.join(outPath, 'ad-block-updater')
if (!fs.existsSync(outPath)) {
fs.mkdirSync(outPath)
}
outPath = path.join(outPath, outSubdir)
if (!fs.existsSync(outPath)) {
fs.mkdirSync(outPath)
}
return path.join(outPath, outputFilename)
}

/**
* Parses the passed in filter rule data and serializes a data file to disk.
*
Expand All @@ -62,19 +104,8 @@ const generateDataFileFromString = (filterRuleData, outputDATFilename, outSubdir
}
const client = new Engine(rules.split('\n'))
const arrayBuffer = client.serialize()
let outPath = path.join('build')
if (!fs.existsSync(outPath)) {
fs.mkdirSync(outPath)
}
outPath = path.join(outPath, 'ad-block-updater')
if (!fs.existsSync(outPath)) {
fs.mkdirSync(outPath)
}
outPath = path.join(outPath, outSubdir)
if (!fs.existsSync(outPath)) {
fs.mkdirSync(outPath)
}
fs.writeFileSync(path.join(outPath, outputDATFilename), Buffer.from(arrayBuffer))
const outPath = getOutPath(outputDATFilename, outSubdir)
fs.writeFileSync(outPath, Buffer.from(arrayBuffer))
}

/**
Expand Down Expand Up @@ -121,7 +152,7 @@ const generateDataFilesForAllRegions = () => {
}

/**
* Convenience function that generates a DAT file for the default list
* Convenience function that generates a DAT file and resources file for the default list
*/
const generateDataFilesForList = (lists, filename) => {
let promises = []
Expand All @@ -134,6 +165,7 @@ const generateDataFilesForList = (lists, filename) => {
p = p.then((listBuffers) => {
generateDataFileFromString(listBuffers, filename, 'default')
})
p = p.then(generateResourcesFile)
return p
}

Expand Down
1 change: 1 addition & 0 deletions submodules/uBlock
Submodule uBlock added at fcfa83