chrome-remote-interface icon indicating copy to clipboard operation
chrome-remote-interface copied to clipboard

Sending Fetch.failRequest to new tab before initial request

Open wxkk opened this issue 1 year ago • 1 comments

Environment

Component Version
Node.js 18.13.0
Client (Chrome/Chromium/...) Chrome 120.0.6099.63
OS running Node.js Windows 10
OS running the client Windows 10
chrome-remote-interface 0.33.0

Is the client running in a container? NO

Description

I am testing this simple example that uses Fetch.failRequest to block an url. To test it, launch a chrome using the flags --remote-debugging-port=9222 https://example.com, then run the example script.

Now try clicking the More information... link while holding CTRL to make it open in a new tab. The new tab loads the url when it should be blocked, if you press F5, the url is correctly blocked. Fetch.requestPaused is being triggered for the stylesheets and js files, but not for the main request.

waitForDebuggerOnStart on Target.setAutoAttach doesn't seem to be doing anything since I am not having to send Runtime.runIfWaitingForDebugger.

In the title i meant Fetch.enable

How can I make the Fetch.failRequest work on new tabs for the initial request?

Example

const CDP = require('chrome-remote-interface');

async function start() {
    const client = await CDP();
    const { Target } = client;

    Target.setDiscoverTargets({discover: true});
    
    Target.setAutoAttach({
        autoAttach: true,
        waitForDebuggerOnStart: true,
        flatten: true
    });

    Target.attachedToTarget(async (params) => {
        console.log('attached', params);
        client.send('Fetch.enable', {
            patterns: [{ urlPattern: "https://www.iana.org/*" }] // url to block
        }, params.sessionId)
    });

    Target.targetCreated(async(params) => { // without this attachedToTarget doesn't trigger
        if(params.targetInfo.type != 'page') return;
        console.log("created", params);
        Target.attachToTarget({
            targetId: params.targetInfo.targetId,
            flatten: true
        })
    });

    client.on('event', (message) => {
        if(message.method == 'Fetch.requestPaused') {
            console.log('blocking', message);
            client.send('Fetch.failRequest', {
                requestId: message.params.requestId,
                errorReason: 'BlockedByResponse'
            }, message.sessionId)
        }
    });
}

start();

wxkk avatar Dec 06 '23 13:12 wxkk