brave-experiments / ad-block

Ad block engine used in the Brave browser for ABP filter syntax based lists like EasyList.
https://www.brave.com
Mozilla Public License 2.0
239 stars 95 forks source link

Brave ad-block does not work when Electron is upgraded to v3.x #155

Open mailtokartik1 opened 5 years ago

mailtokartik1 commented 5 years ago

We have been using Brave Ad-Block for our web-browser project for a long time. Recently, we upgraded the Electron version in our web browser application to v3.0.0 and observed that ad-block did not work for Electron v3.0.0. I figured there may be some flaw in our implementation for ad-block in our application and proceeded to check the implementation. This post is for all those who may encounter such issues in the future. The way we have implemented ad-block is follows: 1) We get the easylist.txt file from the server. 2) We serialize it and create a buffer for the easylist.txt during the build.

clientPromise.then((client) => {
  const buffer = client.serialize(64);
  return new Promise((resolve, reject) => {
    fs.writeFile(outputPath, buffer, (err) => {
      if (err) {
        reject(err);
      }
      return resolve();
    });
  });
})
.then(() => {
  done();
})
.catch((error) => {
  throw new gulpUtil.PluginError({
    plugin: 'compile-block-ads-list',
    message: error.message + ' ' + error.stack
  });
});

3) We deserialize the buffer in the start process.

fs.readFile(bufferPath, (err, buffer) => {
  if ( err ) {
    fail(err);
  }
  const adBlockClient = new AdBlockClient();
  adBlockClient.deserialize(buffer);
  done(adBlockClient);
});

4) We create an AdblockClient and match the url, the resource type and domain name to the deserialized buffer.

(client as any).matches(url, mapFilterType[details.resourceType], firstPartyUrl.host)

We had some problems in using ad-block in the past with Electron v2.x hence, we forked ad-block and were currently using v3.1.11. The problem being https://github.com/brave/ad-block/issues/117. After the update to Electron v3.x, adblock stopped working. After some careful research, found out that the buffer creation and the serialization worked as usual but the deserialize function didn't. The way I solved it is by adding the code of deserialize from v3.1.11 into the forked v4.1.0 and it worked like a charm. If you have any better solutions, feel free to tell us. Thank you.