Category: Adblock

  • [AdBlock Anatomy] Run and Debug AdBlock Locally

    This article is starter of an anatomy series for dissecting AdBlock/AdBlockPlus/uBlockOrigin source code and learn how they work. First let’s start from running and debugging AdBlock locally.

    Note that this article is for eyeo owned browser extension AdBlock and AdBlockPlus.

    Source code

    Luckily, AdBlock main source entry is using NX monorepo, where we can easily see the dependency graph within the repo below.

    However, there are many core features are not within the monorepo, here is a list of important ones:

    • @eyeo/webext-ad-filtering-solution: most important core adblocking features shared across extensions.
    • @eyeo/snippets: eyeo developed special content scripts for element hiding and behavioral intercepting. This repo reflects everything from eyeo official developer document.
    • Filter list: eyeo developed filter list mostly leveraging @eyeo/snippets for enhanced blocking.
    • @adblockinc/rules: an npm package for retrieving, storing and providing filter lists from various sources for use in AdBlock and Adblock Plus.

    Installation

    Now let’s download the main source code and install packages:

    git clone https://gitlab.com/eyeo/extensions/extensions.git
    cd extensions
    npm install

    In often case if you are using Apple silicon chips, you will often see node-gyp errors like below:

    > npm ERR! gyp ERR! stack Error: `gyp` failed with exit code: 1

    The extensions repo provides some potential fixes, but below commends worked for me:

    python3 -m pip install setuptools
    brew install pkg-config cairo pango libpng jpeg giflib librsvg

    Build extension

    Simply run below to build both AdBlock and AdBlockPlus extension:

    npm run build

    Add extensions to Chrome

    • Go to chrome://extensions/.
    • At the top right, turn on Developer mode.
    • Click Load unpacked.
    • Find and select the app or extension folder.
      • e.g. for Chrome AdBlockPlus with Manifest V2, it is located inside /extensions/host/adblockplus/dist/devenv/chrome-mv2

    Now you should be able to see the extension being added and enabled.

    Debugging @eyeo/webext-ad-filtering-solution

    In a lot of cases, the feature you are looking for is mostly in webext-ad-filtering-solution package. This package can be loaded as extension standalone.

    Installation

    git clone https://gitlab.com/eyeo/adblockplus/abc/webext-ad-filtering-solution.git
    cd extensions
    npm install

    Build and run in watch mode

    You can simply do npm run build, but I found it convenient to use watch mode:

    npx webpack --watch

    The built extensions can be found under dist folder

    • performance-mv2 for Manifest V2
    • performance-mv3 for Manifest V3

    Simply add desired extension to Chrome in dev mode for debugging.

    Debugging background script

    Let’s go to sdk/background/index.js and add some logging:

    ...
    export async function start(addonInfo) {
      console.log("start extension");
      ...
    }
    ...

    Then go to chrome://extensions/ and click on Inspect views -> background page (Service worker for Manifest V3 version) under eyeo's WebExtension Ad-Filtering Solution Test Extension.

    You should be able to see start extension log under Console tab. You can try reload extension if not seeing the log.

    Debugging content script

    Let’s go to sdk/content/index.js and add some logging:

    ...
    async function initContentFeatures() {
      console.log("initContentFeatures");
      ...
    }
    ...

    Open a new tab and go to any page, then open console and you should be able to see initContentFeatures log.

  • How Do Ad Blocking Browser Extensions Work?

    Partial of below content were copied from answers using Perplexity AI

    Ad blocking browser extensions work by intercepting and filtering web content before it’s rendered in your browser. Here’s a detailed explanation of how they function:

    Filter lists and rules

    Ad blockers rely on filter lists, which are collections of predefined rules that determine what content should be blocked or hidden on web pages. These lists contain patterns and rules that match known ad servers, tracking scripts, and other unwanted elements.

    Content interception process

    When you visit a website, the ad blocker extension activates before the page is fully loaded. It performs the following steps:

    • HTTPS Request Blocking: The extension listens to outgoing HTTPS requests from your browser. It compares these requests against its filter lists and blocks any that match known ad platforms or tracking services.
    • URL Filtering: As the page loads, the ad blocker checks URLs of various elements against its filter lists. If a match is found, the content from that URL is blocked.
    • Content Filtering: The extension analyzes the HTML, CSS, and JavaScript of the page, looking for patterns that indicate ads or unwanted content.
    • CSS Injection: Ad blockers may inject custom CSS rules to hide elements that couldn’t be blocked at the network level.
    • JavaScript Injection: Some ad blockers inject their own JavaScript code to counteract advertising scripts and prevent them from functioning.

    What happens behind scene?

    Ad blocking browser extensions utilize both content scripts and background scripts to effectively block ads and unwanted content. Let’s delve deeper into how these scripts work together.

    Background scripts

    Background scripts run continuously in the extension’s background page, separate from any particular web page. They play a crucial role in ad blocking:

    • Filter List Management: Background scripts are responsible for downloading, parsing, and updating filter lists. These lists contain rules for blocking ads and are typically updated periodically to stay current with new ad servers and patterns.
    • Request Interception: Background scripts use the browser’s webRequest API to intercept and analyze network requests before they are sent. This allows the ad blocker to block requests to known ad servers or tracking domains at the network level, preventing the ads from loading in the first place.
    • Communication Hub: The background script acts as a central communication point, receiving messages from content scripts and popup interfaces, and coordinating the extension’s overall behavior.
    • Rule Matching: When a request is intercepted, the background script quickly checks it against the loaded filter lists to determine if it should be blocked.

    Content scripts

    Content scripts are injected into web pages and can manipulate the DOM (Document Object Model) directly. They are essential for handling ad blocking tasks that can’t be accomplished through network request blocking alone:

    • Element Hiding: Content scripts can inject CSS rules or modify the page’s existing CSS to hide ad elements that have already loaded. This is useful for ads that are served from the same domain as the main content and can’t be blocked at the network level.
    • DOM Scanning: These scripts can scan the page’s DOM structure to identify and remove ad-related elements based on specific patterns or rules.
    • Script Injection: Content scripts can inject additional JavaScript into the page to neutralize ad-related scripts or prevent them from executing.
    • Cosmetic Filtering: They apply cosmetic filters to remove empty spaces left by blocked ads, improving the page’s appearance.

    Interaction between background and content scripts

    The background and content scripts work together to provide comprehensive ad blocking:

    • Message Passing: Content scripts communicate with the background script via message passing, sending information about the current page and receiving instructions on what to block or modify.
    • Dynamic Rule Application: The background script can send updated blocking rules to content scripts in real-time, allowing for dynamic ad blocking that adapts to changes on the page.
    • Performance Optimization: By dividing tasks between background and content scripts, ad blockers can optimize performance. Network-level blocking happens in the background script, while page-specific modifications occur in the content script.

    This combination of background and content scripts allows ad blocking extensions to provide a comprehensive and efficient ad-blocking experience, handling both network-level blocking and page-specific content manipulation.

  • Different Type Of Ad Blocking Software

    There are several types of ad blocking software available, each with its own approach to filtering out unwanted advertisements. Here are the main categories:

    Browser extensions

    Browser extensions are the most common type of ad blockers. They integrate directly into web browsers and filter content as pages load.

    Popular examples include:

    • AdBlock/AdBlockPlus: owned by eyeo, which are most popular ad blocking browser extensions.
      • eyeo does offer Acceptable Ads program which is a paid service to allowlist some ads.
    • uBlock Origin: fully open source and block ads extensively without compromise.
    • AdGuard: Cross-platform solutions

    Pros

    • Easy to install and use
    • Often free or low-cost
    • Can be quickly enabled/disabled with a few clicks
    • Regularly updated by developers
    • Highly customizable, allowing fine-grained control over blocked content
    • Can block ads http request as well as page html elements

    Cons

    • Only work within the specific browser they’re installed in
    • May need to be installed separately for each browser used
    • Some extensions can access sensitive browsing data
    • Potential for ownership changes or compromised developers

    Desktop applications

    These are standalone programs that run independently of the browser, often blocking ads across multiple applications.

    Popular examples include:

    • AdGuard: Cross-platform solutions. Available for Windows and Mac.
    • AdLock: Runs as a separate program to block ads in browsers and other applications like Skype or games.

    Pros

    • Block ads across multiple applications, not just browsers
    • Often include additional security features
    • Can protect against malicious websites and tracking
    • Some offer parental control features

    Cons

    • Require installation and configuration on each device
    • May need manual disabling when interfering with legitimate content
    • Often come with a cost for full features

    Mobile Ad Blockers

    Ad blocking solutions designed specifically for mobile devices:

    Popular examples include:

    • AdGuard: Cross-platform solutions. Available for iOS and Andriod.
    • AdBlock Browser for Android.

    Pros

    • Designed specifically for mobile devices
    • Can block ads in apps as well as browsers
    • Often include privacy-enhancing features

    Cons

    • May not be as comprehensive as desktop solutions
    • Can be limited by mobile operating system restrictions
    • Some require rooting or jailbreaking for full functionality

    VPN-based ad blockers

    A lot of VPN services include ad blocking features as part of their offering, like ExpressVPN, NordVPN and etc. They usually block ads through network requests across different type of applications.

    Pros

    • Block ads at the network level
    • Provide additional privacy and security benefits of a VPN
    • Work across all applications and browsers

    Cons

    • Generally less effective at blocking ads than dedicated solutions
    • May slow down internet connection due to VPN routing
    • Often more expensive than standalone ad blockers

    Browser with build-in ad blocking features

    Some browsers come with built-in ad blocking capabilities:

    • Opera: Includes ad blocking features without the need for additional extensions
    • Brave: A free browser with built-in ad blocking for desktop and mobile devices

    Pros

    • No need for additional installations
    • Integrated into the browser’s security features
    • Often optimized for performance

    Cons

    • Usually less customizable than third-party solutions
    • May not be as comprehensive in ad blocking
    • Limited to the specific browser with built-in blocking