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

When open a new tab, some network events are skipped. #319

Closed
Dry7 opened this issue Jan 9, 2018 · 6 comments
Closed

When open a new tab, some network events are skipped. #319

Dry7 opened this issue Jan 9, 2018 · 6 comments

Comments

@Dry7
Copy link

Dry7 commented Jan 9, 2018

When I click on a page http://getresto.gifts48.ru/1/

A window opens with the following redirect chain:

http://getresto.gifts48.ru/1/popup1.php
http://getresto.gifts48.ru/1/popup2.php
http://getresto.gifts48.ru/1/popup3.php
http://getresto.gifts48.ru/1/popup4.php
http://getresto.gifts48.ru/1/popup5.html
http://getresto.gifts48.ru/1/popup6.html
http://getresto.gifts48.ru/1/popup7.html
http://getresto.gifts48.ru/1/popup8.html

I'm trying to collect this chain with this code

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

CDP(async (client) => {
    const {Network, Page, Target} = client;
    Network.requestWillBeSent(({request, redirectResponse}) => {
        console.log((redirectResponse || {}).url + ' -> ' + request.url);
    });

	Target.targetCreated((params) => {
		if(params.targetInfo.type != "page") {
            return;
		}

        const {targetId} = params.targetInfo;
        const findTarget = (targets) => {
            return targets.find(target => target.id === targetId);
        };
        CDP({target: findTarget}, async (popup) => {
			popup.Network.requestWillBeSent(({request, redirectResponse}) => {
                console.log((redirectResponse || {}).url + ' -> ' + request.url);
            });
            await popup.Network.enable();
        });
    });

    try {
		await Target.setDiscoverTargets({discover: true});
        await Network.enable();
        await Page.enable();
        await Page.navigate({url: 'http://getresto.gifts48.ru/1/'});
        await Page.loadEventFired();
		clickPage(client);
    } catch (err) {
        console.error(err);
    } finally {
//        client.close();
    }
}).on('error', (err) => {
    console.error(err);
});

function clickPage(client) {
	const options = {
            x: 100,
            y: 100,
            button: 'left',
            clickCount: 1
        };
        Promise.resolve().then(() => {
            options.type = 'mousePressed';
            return client.Input.dispatchMouseEvent(options);
        }).then(() => {
            options.type = 'mouseReleased';
            return client.Input.dispatchMouseEvent(options);
        }).catch((err) => {
            console.error(err);
        });
}

But the first three pages are missing.
How can I fix this?

@cyrus-and
Copy link
Owner

I think this is a timing problem: when the popup window opens and the Target.targetCreated event fires, the loading of the page starts before allowing to register for events with await popup.Network.enable();.

The solution would be to pause the target on start:

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

Then setup events and finally resume with Runtime.runIfWaitingForDebugger.

But unfortunately this doesn't work, even the auto-attach feature itself doesn't seem to work. And even when the target used to perform these operations is the DevTools target (ws://127.0.0.1:9222/devtools/browser/...) as it is supposed to be. Plus some segmentation faults appeared during my tests. Keep in mind that this is an experimental feature, things may not work as expected or maybe this isn't the right approach, you can try to ask this in the Google Group.

The documentation says:

Controls whether to automatically attach to new targets which are considered to be related to this one.

Maybe a popup window is not considered to be related to its opener in this context.

@Dry7
Copy link
Author

Dry7 commented Jan 9, 2018

Thanks for the help.
I asked questions, maybe the developers will respond with something new.
But a month ago they said that this is not yet supported.
https://groups.google.com/forum/#!msg/chrome-debugging-protocol/n1FQ4Ypww6w/rb4Hf-_0BgAJ

@Dry7 Dry7 closed this as completed Jan 9, 2018
@cyrus-and
Copy link
Owner

Oh, good to know, thanks for the link.

@Dry7
Copy link
Author

Dry7 commented Jan 10, 2018

@cyrus-and I wrote an email to one of the developers and he replied that this task is not even worth a priority.
But if there are more comments on this topic - ChromeDevTools/devtools-protocol#77
then they will do it faster.

@Niek
Copy link

Niek commented Mar 21, 2019

A year+ later, and it's still impossible to attach to new targets upon creation. Anyone who knows a workaround?

@Niek
Copy link

Niek commented Nov 10, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants