Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending Fetch.failRequest to new tab before initial request #549

Open
wxkk opened this issue Dec 6, 2023 · 0 comments
Open

Sending Fetch.failRequest to new tab before initial request #549

wxkk opened this issue Dec 6, 2023 · 0 comments

Comments

@wxkk
Copy link

wxkk commented Dec 6, 2023

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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant