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

Page.addScriptToEvaluateOnNewDocument #15

Open
cyberfuhrer opened this issue Apr 7, 2022 · 2 comments
Open

Page.addScriptToEvaluateOnNewDocument #15

cyberfuhrer opened this issue Apr 7, 2022 · 2 comments

Comments

@cyberfuhrer
Copy link

cyberfuhrer commented Apr 7, 2022

How can I use 'Page.addScriptToEvaluateOnNewDocument' for all new opened tabs?
If switching to a new tab init user by click link. Or use one injection for all new opened tabs

@o0101
Copy link

o0101 commented Sep 28, 2022

Check the Chrome remote debugging protocol docs

You see that you need to identify the target you are adding to

This is normally done by adding a Target ID or session ID for the target. The session ID comes from the target attached event when you attach to the target.

I'm not sure how closely this library mirrors the protocol but checking the Chrome remote debugging protocol API docs is a really good place to understand more about each of the methods.

Ignore anything about puppeteer because mostly they have a completely different API and you weren't learning about how the debugging protocol works from Reading puppeteer API code. But puppeteer can be a good place to go to look at how they implement a particular puppeteer API method using the underlying remote debugging protocol.

So if I was to use the straight protocol the sequence of commands that I would roughly follow would be fetching all targets so you can do that on the HTTP endpoint /json

Then for each Target attach to it, using flat session mode, and listen for the attached event and retrieve the session ID. And then send the relevant command in this case page adscript to evaluate on new document, including the session ID.

And repeat for each of those targets. You probably only want to add it to Page level targets so check that the target has a type property equal to page before you attach to it.

@fake-name
Copy link
Owner

I'm not sure how closely this library mirrors the protocol

It's base-level is literally a machine-generated wrapper that ingests from the official chromium debug protocol description JSON files.

There's some additional handy features on top of that layer, but the entire chrome debug protocol should be available basically exactly as it's documented in the debug protocol documentation.

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

3 participants