chromiumoxide/handler/blockers/
mod.rs

1/// adblock patterns
2pub mod adblock_patterns;
3/// interception manager
4pub mod intercept_manager;
5/// script blockers
6pub mod scripts;
7/// xhr blockers
8pub mod xhr;
9
10// Trie node for ignore.
11#[derive(Default, Debug)]
12pub struct TrieNode {
13    /// Children for trie.
14    pub children: hashbrown::HashMap<char, TrieNode>,
15    /// End of word match.
16    pub is_end_of_word: bool,
17}
18
19/// Basic Ignore trie.
20#[derive(Debug)]
21pub struct Trie {
22    /// The trie node.
23    pub root: TrieNode,
24}
25
26impl Trie {
27    /// Setup a new trie.
28    pub fn new() -> Self {
29        Trie {
30            root: TrieNode::default(),
31        }
32    }
33    // Insert a word into the Trie.
34    pub fn insert(&mut self, word: &str) {
35        let mut node = &mut self.root;
36        for ch in word.chars() {
37            node = node.children.entry(ch).or_insert_with(TrieNode::default);
38        }
39        node.is_end_of_word = true;
40    }
41
42    // Check if the Trie contains any prefix of the given string.
43    #[inline]
44    pub fn contains_prefix(&self, text: &str) -> bool {
45        let mut node = &self.root;
46
47        for ch in text.chars() {
48            if let Some(next_node) = node.children.get(&ch) {
49                node = next_node;
50                if node.is_end_of_word {
51                    return true;
52                }
53            } else {
54                break;
55            }
56        }
57
58        false
59    }
60}
61
62/// Url matches analytics that we want to ignore or trackers.
63pub(crate) fn ignore_script_embedded(url: &str) -> bool {
64    crate::handler::blockers::scripts::URL_IGNORE_EMBEDED_TRIE.contains_prefix(url)
65}
66
67/// Url matches analytics that we want to ignore or trackers.
68pub(crate) fn ignore_script_xhr(url: &str) -> bool {
69    crate::handler::blockers::xhr::URL_IGNORE_XHR_TRIE.contains_prefix(url)
70}
71
72/// Url matches media that we want to ignore.
73pub(crate) fn ignore_script_xhr_media(url: &str) -> bool {
74    crate::handler::blockers::xhr::URL_IGNORE_XHR_MEDIA_TRIE.contains_prefix(url)
75}