Convert Figma logo to code with AI

brave logoadblock-rust

Brave's Rust-based adblock engine

1,386
119
1,386
68

Top Related Projects

45,801

uBlock Origin - An efficient blocker for Chromium and Firefox. Fast and lean.

AdGuard browser extension

Data set of top third party web domains with rich metadata about them

EasyList filter subscription (EasyList, EasyPrivacy, EasyList Cookie, Fanboy's Social/Annoyances/Notifications Blocking List)

Quick Overview

Brave/adblock-rust is a Rust implementation of ad-blocking functionality, primarily used in the Brave browser. It provides efficient and customizable ad-blocking capabilities, allowing for the creation and application of filter lists to block unwanted content on web pages.

Pros

  • High performance due to Rust's efficiency and low-level control
  • Cross-platform compatibility, enabling use in various environments
  • Actively maintained and supported by the Brave browser team
  • Customizable and extensible for different ad-blocking needs

Cons

  • Steep learning curve for developers not familiar with Rust
  • Limited documentation compared to some other ad-blocking libraries
  • Primarily focused on Brave browser integration, which may limit its adoption in other projects
  • Requires regular updates to keep up with evolving ad techniques

Code Examples

  1. Creating an engine and adding rules:
use adblock::engine::Engine;

let mut engine = Engine::new(true);
engine.add_filter_list("||example.com^", false);
  1. Checking if a URL should be blocked:
let url = "https://ads.example.com/banner.jpg";
let source_url = "https://example.com";
let request_type = "image";

let should_block = engine.check_network_urls(url, source_url, request_type);
  1. Applying element hiding rules:
let mut engine = Engine::new(true);
engine.add_filter_list("#ad-banner", false);

let domain = "example.com";
let hiding_rules = engine.hidden_class_id_selectors(domain, false);

Getting Started

To use adblock-rust in your Rust project:

  1. Add the dependency to your Cargo.toml:

    [dependencies]
    adblock = "0.5"
    
  2. In your Rust code:

    use adblock::engine::Engine;
    
    fn main() {
        let mut engine = Engine::new(true);
        engine.add_filter_list("||ads.example.com^", false);
    
        let url = "https://ads.example.com/banner.jpg";
        let source_url = "https://example.com";
        let request_type = "image";
    
        let should_block = engine.check_network_urls(url, source_url, request_type);
        println!("Should block: {}", should_block);
    }
    

This example creates an ad-blocking engine, adds a filter rule, and checks if a specific URL should be blocked.

Competitor Comparisons

45,801

uBlock Origin - An efficient blocker for Chromium and Firefox. Fast and lean.

Pros of uBlock

  • More mature and widely adopted project with a larger community
  • Extensive customization options and advanced features for power users
  • Cross-browser compatibility (Chrome, Firefox, Safari, and more)

Cons of uBlock

  • Written primarily in JavaScript, which may have performance implications
  • Higher memory usage compared to more lightweight alternatives
  • Steeper learning curve for configuring advanced features

Code Comparison

uBlock (JavaScript):

µBlock.staticNetFilteringEngine.matchString(
    fctxt,
    requestType,
    requestURL
);

adblock-rust (Rust):

engine.check_network_urls(
    &url,
    &domain,
    &csp_type,
    &request_type
)

Key Differences

  • adblock-rust is written in Rust, potentially offering better performance and memory efficiency
  • uBlock provides a more comprehensive set of features and customization options
  • adblock-rust is specifically designed for integration into the Brave browser, while uBlock is a standalone extension

Use Cases

  • uBlock: Ideal for users who want granular control over their ad-blocking experience across multiple browsers
  • adblock-rust: Better suited for integration into Brave browser or projects requiring a lightweight, performant ad-blocking solution

AdGuard browser extension

Pros of AdguardBrowserExtension

  • More comprehensive content blocking features, including custom rules and advanced settings
  • Cross-browser compatibility, supporting multiple browsers like Chrome, Firefox, and Edge
  • Active community support and frequent updates

Cons of AdguardBrowserExtension

  • Larger codebase and potentially higher resource usage
  • More complex setup and configuration process for users
  • May have a steeper learning curve for contributors due to its extensive feature set

Code Comparison

AdguardBrowserExtension (JavaScript):

export const isAddressMatch = (address, rule) => {
    const domain = getDomain(address);
    return domain && domain.endsWith(rule.domain);
};

adblock-rust (Rust):

pub fn is_domain_in(domain: &str, filter: &str) -> bool {
    domain.ends_with(filter) && (domain.len() == filter.len() || domain.as_bytes()[domain.len() - filter.len() - 1] == b'.')
}

Both repositories implement domain matching functionality, but adblock-rust uses Rust for potentially better performance and memory safety. AdguardBrowserExtension's JavaScript implementation may be more accessible for web developers but could be less efficient in some scenarios.

Data set of top third party web domains with rich metadata about them

Pros of Tracker Radar

  • Broader focus on tracking technologies beyond just ad-blocking
  • Extensive dataset of trackers and their behaviors
  • Community-driven approach with contributions from researchers and users

Cons of Tracker Radar

  • Less integrated with a specific browser or ad-blocking solution
  • May require more manual implementation for practical use
  • Potentially slower updates compared to a dedicated ad-blocking solution

Code Comparison

Tracker Radar (JavaScript):

const trackers = require('@duckduckgo/tracker-radar');
const domain = 'example.com';
const matchedTrackers = trackers.getTrackerData(domain);
console.log(matchedTrackers);

Adblock Rust (Rust):

use adblock::engine::Engine;
let mut engine = Engine::new(true);
engine.load_filters("easylist.txt");
let blocked = engine.check_network_urls("https://example.com/ad.js", "https://example.com", "image");
println!("Blocked: {}", blocked);

Key Differences

  • Tracker Radar focuses on identifying and categorizing trackers, while Adblock Rust is specifically designed for ad-blocking
  • Adblock Rust is implemented in Rust, offering potential performance benefits, while Tracker Radar is primarily JavaScript-based
  • Tracker Radar provides a more comprehensive dataset, while Adblock Rust offers more direct integration with ad-blocking functionality

Both projects contribute valuable tools to the privacy and ad-blocking ecosystem, with Tracker Radar offering broader tracking insights and Adblock Rust providing efficient ad-blocking capabilities.

EasyList filter subscription (EasyList, EasyPrivacy, EasyList Cookie, Fanboy's Social/Annoyances/Notifications Blocking List)

Pros of EasyList

  • Widely adopted and community-maintained filter lists
  • Extensive coverage of ad domains and patterns
  • Regular updates to keep up with new ad sources

Cons of EasyList

  • Requires integration with a separate ad-blocking engine
  • Can be resource-intensive due to large filter lists
  • May cause false positives, blocking legitimate content

Code Comparison

EasyList (filter rules):

||example.com/ads/*
##.ad-banner
/banner\d+\.gif/

adblock-rust (Rust implementation):

let engine = Engine::new(true);
engine.add_filter_list(&filters, FilterSet::default());
let matched = engine.check_network_urls(&url, &frame_url, ResourceType::Image);

Key Differences

  • EasyList focuses on filter rules, while adblock-rust provides a complete ad-blocking engine
  • adblock-rust offers more granular control and customization options
  • EasyList requires less development effort for basic ad-blocking implementation
  • adblock-rust provides better performance and resource management for large-scale applications

Use Cases

  • EasyList: Ideal for quick integration into existing ad-blocking solutions
  • adblock-rust: Suitable for building custom ad-blocking applications with specific requirements

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

adblock-rust

crates.io npmjs.com docs.rs Build Status License

Putting you back in control of your browsing experience.

adblock-rust is the engine powering Brave's native adblocker, available as a library for anyone to use. It features:

  • Network blocking
  • Cosmetic filtering
  • Resource replacements
  • Hosts syntax
  • uBlock Origin syntax extensions
  • iOS content-blocking syntax conversion
  • Compiling to native code or WASM
  • Rust bindings (crates)
  • JS bindings (npm)
  • Community-maintained Python bindings (pypi)
  • High performance!

Getting started

adblock-rust is used in several projects, including browsers, research tools, and proxies. It may be a good fit for yours, too!

See docs.rs for detailed API documentation.

Also check the Rust example or the NodeJS example.

Optional features

The following cargo features can be used to tweak adblock-rust to best fit your use-case.

CSS validation during rule parsing (css-validation)

When parsing cosmetic filter rules, it's possible to include a built-in implementation of CSS validation (through the selectors and cssparser crates) by enabling the css-validation feature. This will cause adblock-rust to reject cosmetic filter rules with invalid CSS syntax.

Content blocking format translation (content-blocking)

Enabling the content-blocking feature gives adblock-rust support for conversion of standard ABP-style rules into Apple's content-blocking format, which can be exported for use on iOS and macOS platforms.

External domain resolution (embedded-domain-resolver)

By default, adblock-rust ships with a built-in domain resolution implementation (through the addr crate) that will generally suffice for standalone use-cases. For more advanced use-cases, disabling the embedded-domain-resolver feature will allow adblock-rust to use an external domain resolution implementation instead. This is extremely useful to reduce binary bloat and improve consistency when embedding adblock-rust within a browser.

Parsing resources from uBlock Origin's formats (resource-assembler)

adblock-rust uses uBlock Origin-compatible resources for scriptlet injection and redirect rules. The resource-assembler feature allows adblock-rust to parse these resources directly from the file formats used by the uBlock Origin repository.

Thread safety (object-pooling, unsync-regex-caching)

The object-pooling and unsync-regex-caching features enable optimizations for rule matching speed and the amount of memory used by the engine. These features can be disabled to make the engine Send + Sync, although it is recommended to only access the engine on a single thread to maintain optimal performance.