brave/ad-block

Name: ad-block

Owner: Brave Software

Description: Ad block engine used in the Brave browser for ABP filter syntax based lists like EasyList.

Created: 2015-10-13 23:12:15.0

Updated: 2018-05-24 15:26:23.0

Pushed: 2018-05-24 15:26:21.0

Homepage: https://www.brave.com

Size: 2439

Language: C

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Build Status

Brave Ad Block

Native node module, and C++ library for Adblock Plus filter parsing for lists like EasyList.

It uses a bloom filter and Rabin-Karp algorithm to be super fast.

To include brave/ad-block in your project:
install --save ad-block
JS Sample
t {AdBlockClient, FilterOptions} = require('ad-block')
t client = new AdBlockClient()
nt.parse('/public/ad/*$domain=slashdot.org')
nt.parse('/public/ad3/*$script')
b1 = client.matches('http://www.brianbondy.com/public/ad/some-ad', FilterOptions.script, 'slashdot.org')
b2 = client.matches('http://www.brianbondy.com/public/ad/some-ad', FilterOptions.script, 'digg.com')
ole.log('public/ad/* should match b1.  Actual: ', b1)
ole.log('public/ad/* should not match b2.  Actual: ', b2)
C++ Sample
lude "ad_block_client.h"
lude <algorithm>
lude <iostream>
lude <fstream>
lude <sstream>
lude <iostream>
lude <string>

g namespace std;

ng getFileContents(const char *filename)

stream in(filename, ios::in);
 (in) {
ostringstream contents;
contents << in.rdbuf();
in.close();
return(contents.str());

row(errno);


 writeFile(const char *filename, const char *buffer, int length)

stream outFile(filename, ios::out | ios::binary);
 (outFile) {
outFile.write(buffer, length);
outFile.close();
return;

row(errno);



main(int argc, char**argv) {
d::string &&easyListTxt = getFileContents("./test/data/easylist.txt");
nst char *urlsToCheck[] = {
// ||pagead2.googlesyndication.com^$~object-subrequest
"http://pagead2.googlesyndication.com/pagead/show_ads.js",
// Should be blocked by: ||googlesyndication.com/safeframe/$third-party
"http://tpc.googlesyndication.com/safeframe/1-0-2/html/container.html",
// Should be blocked by: ||googletagservices.com/tag/js/gpt_$third-party
"http://www.googletagservices.com/tag/js/gpt_mobile.js",
// Shouldn't be blocked
"http://www.brianbondy.com"


 This is the site who's URLs are being checked, not the domain of the URL being checked.
nst char *currentPageDomain = "slashdot.org";

 Parse easylist
BlockClient client;
ient.parse(easyListTxt.c_str());

 Do the checks
d::for_each(urlsToCheck, urlsToCheck + sizeof(urlsToCheck) / sizeof(urlsToCheck[0]), [&client, currentPageDomain](std::string const &urlToCheck) {
if (client.matches(urlToCheck.c_str(), FONoFilterOption, currentPageDomain)) {
  cout << urlToCheck << ": You should block this URL!" << endl;
} else {
  cout << urlToCheck << ": You should NOT block this URL!" << endl;
}
;

t size;
 This buffer is allocate on the heap, you must call delete[] when you're done using it.
ar *buffer = client.serialize(size);
iteFile("./ABPFilterParserData.dat", buffer, size);

BlockClient client2;
 Deserialize uses the buffer directly for subsequent matches, do not free until all matches are done.
ient2.deserialize(buffer);
 Prints the same as client.matches would
d::for_each(urlsToCheck, urlsToCheck + sizeof(urlsToCheck) / sizeof(urlsToCheck[0]), [&client2, currentPageDomain](std::string const &urlToCheck) {
if (client2.matches(urlToCheck.c_str(), FONoFilterOption, currentPageDomain)) {
  cout << urlToCheck << ": You should block this URL!" << endl;
} else {
  cout << urlToCheck << ": You should NOT block this URL!" << endl;
}
;
lete[] buffer;
turn 0;

Util for checking URLs
Developing brave/ad-block
  1. Clone the git repository from GitHub:

    git clone --recursive https://github.com/brave/ad-block

  2. Open the working directory:

    cd ad-block

  3. Install the Node (v5+) dependencies:

    npm install

Make the node module

Running sample (which also generates a .dat file for deserializing)
 sample
Running tests
 test
Clearing build files
 clean

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.