Created
August 29, 2019 19:19
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This Source Code Form is subject to the terms of the Mozilla Public | |
* 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/. */ | |
// Excerpt from ad-block/scripts/generateDataFiles and inspired from ad-block/scripts/check.js | |
const url = require('url'); | |
const path = require('path') | |
const fs = require('fs') | |
const {AdBlockClient, FilterOptions} = require('ad-block'); | |
const { makeAdBlockClientFromDATFile} = require('ad-block/lib/util'); | |
function generateDataFileFromFile(filterRuleFile, outputDATFilename) { | |
const filterRuleData = fs.readFileSync(filterRuleFile, 'utf8'); | |
const client = new AdBlockClient() | |
if (filterRuleData.constructor === Array) { | |
filterRuleData.forEach(filterRuleDataItem => client.parse(filterRuleDataItem)) | |
} else { | |
client.parse(filterRuleData) | |
} | |
console.log('Parsing stats:', client.getParsingStats()) | |
const serializedData = client.serialize() | |
if (!fs.existsSync('data')) { | |
fs.mkdirSync('./data') | |
} | |
fs.writeFileSync(path.join('data', outputDATFilename), serializedData) | |
} | |
function processURLs(urls, location, dataFile) { | |
return makeAdBlockClientFromDATFile(dataFile) | |
.then((adBlockClient) => { | |
const parsedUrl = url.parse(location); | |
let match = 0; | |
let nomatch = 0; | |
urls.forEach(async(href) => { | |
if (adBlockClient.matches(href, FilterOptions.noFilterOption, parsedUrl.host)) { | |
match++; | |
} else { | |
nomatch++; | |
} | |
}); | |
return { | |
match, | |
nomatch, | |
total: match + nomatch | |
}; | |
}) | |
.catch((err) => { | |
return err; | |
}); | |
} | |
function urlProcessor() { | |
const clients = { | |
}; | |
return async (href, location, dataFile) => { | |
if (typeof clients[dataFile] === 'undefined') { | |
clients[dataFile] = await makeAdBlockClientFromDATFile(dataFile); | |
} | |
const parsedUrl = url.parse(location); | |
return clients[dataFile].matches(href, FilterOptions.noFilterOption, parsedUrl.host); | |
} | |
} | |
module.exports = { | |
generateDataFileFromFile, | |
urlProcessor, | |
processURLs | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment