chromiumoxide/handler/blockers/
mod.rs

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