Obscura Request Interception — Block APIs, Mock Responses, Inject Scripts

Three request interception approaches: CDP Fetch.enable, Rust Library Interception API, built-in Stealth Tracker Blocklist. Block ads, mock APIs, inject preload scripts.

16Yun Engineering TeamJul 7, 20262 min read

Control Every Request

Pages make many network requests — HTML, CSS, JS, images, analytics, API calls. For scraping and automation, you rarely need all of them. Obscura provides three levels of request interception:

  1. CDP Fetch.enable — intercept from Puppeteer/Playwright
  2. Rust Library Interception API — intercept from Rust code
  3. Built-in Stealth Tracker Blocklist — automatic net-layer blocking

Level 1: CDP Fetch.enable (Puppeteer/Playwright)

Block by Resource Type

// Puppeteer
await page.setRequestInterception(true);
page.on('request', req => {
  if (['image', 'media', 'font'].includes(req.resourceType())) {
    req.abort();
  } else {
    req.continue();
  }
});
```bash
 
```javascript
// Playwright
await page.route('**/*', route => {
  if (['image', 'media', 'font'].includes(route.request().resourceType())) {
    route.abort();
  } else {
    route.continue();
  }
});
```bash
 
### Block by URL Pattern
 
```javascript
// Puppeteer
page.on('request', req => {
  if (req.url().includes('google-analytics.com')) {
    req.abort();
  } else {
    req.continue();
  }
});
```bash
 
```javascript
// Playwright: regex support
await page.route(/google-analytics\.com/, route => route.abort());
```bash
 
### Mock API Responses
 
```javascript
// Puppeteer
page.on('request', req => {
  if (req.url().endsWith('/api/feature-flags')) {
    req.respond({
      status: 200,
      contentType: 'application/json',
      body: JSON.stringify({ newDashboard: true }),
    });
  } else {
    req.continue();
  }
});
```bash
 
## Level 2: Stealth Built-in Blocklist
 
Zero-config tracker blocking with Stealth mode:
 
```bash
obscura fetch https://example.com --stealth
```bash
 
Peter Lowe's blocklist — 3500+ domains blocked at the network layer. Supports exact and subdomain wildcard matching.
 
## Level 3: Rust Library Interception API
 
### enable_interception Channel
 
```rust
use obscura::{Browser, InterceptResolution};
 
let mut page = browser.new_page().await?;
let mut rx = page.enable_interception();
 
tokio::spawn(async move {
    while let Some(req) = rx.recv().await {
        let action = if req.url.contains("/ads") {
            InterceptResolution::Fail { reason: "blocked".into() }
        } else if req.url.ends_with("/api/flags") {
            InterceptResolution::Fulfill {
                status: 200,
                headers: Default::default(),
                body: r#"{"newDashboard":true}"#.into(),
            }
        } else {
            InterceptResolution::Continue {
                url: None, method: None, headers: None, body: None,
            }
        };
        let _ = req.resolver.send(action);
    }
});
```bash
 
Three resolutions:
 
| Resolution | Meaning |
|------------|---------|
| `Continue` | Pass through, optionally rewrite |
| `Fulfill` | Return a fake response |
| `Fail` | Block with error reason |
 
### Preload Script
 
Run code before the page's own `<script>` tags:
 
```rust
page.add_preload_script("window.__patched = true;");
page.goto("https://example.com").await?;
```bash
 
### Passive Callbacks
 
```rust
page.on_request(Arc::new(|info| {
    println!("Request: {} {}", info.method, info.url);
}));
 
page.on_response(Arc::new(|info, resp| {
    if info.resource_type == ResourceType::Fetch {
        println!("API: {} -> {} bytes", info.url, resp.body.len());
    }
}));
```bash
 
## Comparison
 
| Level | Layer | Use case | Code needed |
|-------|-------|----------|-------------|
| CDP Fetch.enable | CDP protocol | Existing Puppeteer/Playwright | JavaScript |
| Stealth Blocklist | Network | Zero-config | None |
| Rust Library API | Rust crate | Deep integration | Rust |
 
## Security
 
URL rewrites in `Continue` re-validate against the SSRF gate, so interception cannot bypass `--allow-private-network`.
 
## Summary
 
Obscura provides complete request control from the network layer through CDP to the Rust library. Stealth mode handles most tracker blocking automatically; CDP interception and the Rust API provide full flexibility for fine-grained control.

Need an enterprise proxy plan?

We can tailor architecture to your target domains, concurrency, and reliability goals.