forked from brave/adblock-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
59 lines (52 loc) · 1.66 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const adblockRust = require('adblock-rs');
const fs = require('fs');
const dataPath = '../data/'
const debugInfo = true;
const filterSet = new adblockRust.FilterSet(debugInfo);
const easylistFilters = fs.readFileSync(
dataPath + 'easylist.to/easylist/easylist.txt',
{ encoding: 'utf-8' },
).split('\n');
filterSet.addFilters(easylistFilters);
const uboUnbreakFilters = fs.readFileSync(
dataPath + 'uBlockOrigin/unbreak.txt',
{ encoding: 'utf-8' },
).split('\n');
filterSet.addFilters(uboUnbreakFilters);
const resources = adblockRust.uBlockResources(
dataPath + 'test/fake-uBO-files/web_accessible_resources',
dataPath + 'test/fake-uBO-files/redirect-resources.js',
dataPath + 'test/fake-uBO-files/scriptlets.js'
);
const engine = new adblockRust.Engine(filterSet, true);
engine.useResources(resources);
// Simple match
console.log(engine.check(
'http://example.com/-advertisement-icon.',
'http://example.com/helloworld',
'image',
));
// Match with full details
console.log(engine.check(
'http://example.com/-advertisement-icon.',
'http://example.com/helloworld',
'image',
true,
));
// No match, but still with full details
console.log(engine.check(
'https://github.githubassets.com/assets/frameworks-64831a3d.js',
'https://github.com/brave',
'script',
true,
));
// Example that includes a redirect resource
console.log(engine.check(
'https://bbci.co.uk/test/analytics.js',
'https://bbc.co.uk',
'script',
true
));
// Serialize the engine to an ArrayBuffer
const serializedArrayBuffer = engine.serializeRaw();
console.log(`Engine size: ${(serializedArrayBuffer.byteLength / 1024 / 1024).toFixed(2)} MB`);