browser_add_network_rule
Add a rule to intercept network requests matching a URL pattern. Rules can block requests (e.g., ads, trackers), mock responses with custom data (useful for testing), or redirect to different URLs. Supports glob patterns (*.js, *://api.example.com/*) or regex for flexible matching. Returns a rule_id for later removal.
Usage Example
Parameters
Required
context_idstringrequiredThe unique identifier of the browser context (e.g., 'ctx_000001')
url_patternstringrequiredURL pattern to match requests against. Supports glob patterns (e.g., '*://api.example.com/*', '*.js') or regex if is_regex=true. Glob uses * for any characters, ? for single character
actionenumallowblockmockredirectWhat to do with matching requests: 'allow' lets them through (useful with blocking enabled), 'block' drops the request, 'mock' returns a fake response, 'redirect' sends to different URL
Optional
is_regexbooleanTreat url_pattern as a regular expression instead of glob pattern. Default: false (uses glob). Example regex: '^https://api\\.example\\.com/v[0-9]+/'
redirect_urlstringThe URL to redirect matching requests to. Only used when action='redirect'. Example: redirect tracking scripts to empty response
mock_bodystringResponse body to return for mocked requests. Only used when action='mock'. Can be JSON, HTML, or any text content. Example: '{"success": true}'
mock_statusstringHTTP status code for mocked responses (e.g., 200, 404, 500). Only used when action='mock'. Default: 200
mock_content_typestringContent-Type header for mocked responses. Only used when action='mock'. Examples: 'application/json', 'text/html', 'text/plain'. Default: 'text/plain'
Response
Returns a JSON object with the operation result.
{
"success": true,
"result": <value>
}