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

Beginner: How to create "FiltersEngine" instance if I have several list URLs and also single additional local list file? #1901

Closed
martinrotter opened this issue May 3, 2021 · 3 comments

Comments

@martinrotter
Copy link

I have several remote list URLs like:

'https://easylist.to/easylist/easylist.txt',
'https://raw.githubusercontent.com/tomasko126/easylistczechandslovak/master/filters.txt'

so I use this to create engine:

adblock.FiltersEngine.fromLists(fetch, [
  'https://easylist.to/easylist/easylist.txt',
  'https://raw.githubusercontent.com/tomasko126/easylistczechandslovak/master/filters.txt',
]).then(function (res) { engine = res; });

How to add one more list which I have stored in local file? I know there is const engine = FiltersEngine.parse(fs.readFileSync('easylist.txt', 'utf-8')); but how to combine both aspects together?

Sorry, I am node/js beginner. I plan to integrate this great project withing RSS Guard - feed reader used by thousands of users.

@remusao
Copy link
Collaborator

remusao commented May 3, 2021

Hi @martinrotter,

Thanks for your interest. I think the easiest way to achieve this at the moment is to use the updateFromDiff method available on instances of FiltersEngine; it can add new filters (expressed as arrays of strings, and remove existing ones from the engine as well). Here is how it could look like with a self-contained example:

const adblock = require("@cliqz/adblocker");
const fetch = require("node-fetch");
const fs = require("fs");

(async () => {
  const engine = await adblock.FiltersEngine.fromLists(fetch, [
    "https://easylist.to/easylist/easylist.txt",
    "https://raw.githubusercontent.com/tomasko126/easylistczechandslovak/master/filters.txt",
  ]);

  // Before update
  console.log(engine.getFilters().networkFilters.length);

  engine.updateFromDiff({
    added: fs.readFileSync("./custom.txt", "utf-8").split("\n"),
  });

  // Afer update
  console.log(engine.getFilters().networkFilters.length);
})();

And content of custom.txt would contain one rule (a.k.a. filter) per line:

||example.com^
||foo.com^

I hope this helps,
Best,
Rémi

@martinrotter
Copy link
Author

@remusao Thanks for hints, will work best way for me out.

@remusao
Copy link
Collaborator

remusao commented May 30, 2021

Closing this as it seems you managed to integrate the adblocker into your project. Feel free to reopen if you have any question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants