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

Please add the initiator information #1395

Closed
thewulf00 opened this issue Nov 15, 2017 · 29 comments
Closed

Please add the initiator information #1395

thewulf00 opened this issue Nov 15, 2017 · 29 comments
Labels
chromium Issues with Puppeteer-Chromium feature upstream

Comments

@thewulf00
Copy link

Requests, that are fired after the basic request is done, are initiated by some source - f.e. a script or something. Since chrome does provide this information in the network tab, I wonder if you can give it via puppeteer?

Example:
image

@jawadst
Copy link

jawadst commented Nov 26, 2017

+1, that'd be very helpful

@zicongxie
Copy link

@aslushnikov Please! That is useful for me.

@thewulf00
Copy link
Author

Thank you for your replies. I hoped that somebody will need it too.
@aslushnikov Is it possible to get this? I find puppeteer very useful!

@Garbee
Copy link
Contributor

Garbee commented Dec 5, 2017

Out of curiosity, could someone provide an example use-case for needing this information through puppeteer?

@ctavan
Copy link

ctavan commented Dec 6, 2017

@Garbee assuming you use puppeteer to analyze a website which includes 3rd-party code like social sharing buttons. You may want to know which requests were initiated by your own 1st-party code and which requests were initiated by 3rd-party code. This could help you spot performance problems like 3rd-party widgets that cause a lot of subsequent requests.

@sebastianneubert
Copy link

@Garbee Same here. We want to build request maps to vizualise which request was called from where. So we can check, where are performance/memory leaks at 3rd party includes. This could be very helpful to point out, which scripts or iframes initate a waste amount of requests/cookies/trackers. We have all information to build this, except of the initiator of the request.

@Laywanger
Copy link

+1 I need this to track malvertising
@aslushnikov Is it possible ?

@aslushnikov
Copy link
Contributor

We don't currently have initiator information in the request interception events.

Filed upstream: crbug.com/800623

@joshgoodwin
Copy link

I believe that initiator is available to the event object (event.initiator) in NetworkManager.js here:

https://github.com/GoogleChrome/puppeteer/blob/master/lib/NetworkManager.js#L209

You should be able to just forward that property along like you do with event.requestId, no?

joshgoodwin pushed a commit to joshgoodwin/puppeteer that referenced this issue Jan 17, 2018
this change allows you to check the initiator of a request.

Fixes puppeteer#1395
@aslushnikov
Copy link
Contributor

@joshgoodwin initiator is not available for intercepted requests; that's what the upstream bug is about.

https://github.com/GoogleChrome/puppeteer/blob/8c930843160d7fff76a82bf154f151e2bdd29383/lib/NetworkManager.js#L127-L133

@sebastianneubert
Copy link

Any news about the refactoring of #1817 and the initiator information in an intercepted request?

@madecki
Copy link

madecki commented Apr 16, 2018

It would be great to get information about cookie and request initiators!

@phackt
Copy link

phackt commented Jul 18, 2018

Hello, any news about the initiator attribute ? thanks

@sebastianneubert
Copy link

Not really as initiator. But in the last updates of puppeteer, most requests have the referer information in the header, which is fine for me.

page.on('request', interceptedRequest => {
    const url = interceptedRequest.url();
    header_referer = interceptedRequest.headers()['referer'];
    console.log(interceptedRequest);
    interceptedRequest.continue();
});

@phackt
Copy link

phackt commented Jul 19, 2018

@sebastianneubert yes not really the same, you don't have the name of the exact resource involved in the request, also no referer is sent with XHR. The field initiator is really missing.

@rylyade1
Copy link

@phackt actually referrer is also sent with XHR, but there is no guarantee of this header being present (e.g when the referrer policy is set to 'no-referrer'). In these cases @sebastianneubert your solution will probably throw an exception.

@nwingert
Copy link

FYI, initiator info is available using CDP directly. Here's an example from the solution I'm using atm.

// ChromeDevTools/devtools-protocol#56
// increase CallStackDepth to fix initiator stack issue in chrome
const client = await testPage.target().createCDPSession();
await client.send('Debugger.enable');
await client.send('Debugger.setAsyncCallStackDepth', { maxDepth: 32 });

// #1395
// puppeteer request events don't propagate initiator currently
// so register for the event directly with CDP instead
await client.send('Network.enable');
await client.on('Network.requestWillBeSent', (params) => {
const reqURL = params.request.url;
logger.info(request to: ${reqURL});
logger.info(from initiator: ${getInitiatorUrl(params.initiator)});
});

getInitiatorUrl is a little recursive function to search the initiator callstack for the info I need, not included because it's too specific to my requirements.

@aslushnikov
Copy link
Contributor

To everybody: do I get it right that you guys need both initiator type and a stacktrace?

@ctavan
Copy link

ctavan commented Sep 4, 2018

@aslushnikov speaking for me: yes, both would be great!

This patch worked for me (kind of): ctavan@8cdda16

With this initiator() information at hand I used code like this:

    const initiator = request.initiator();
    let simplifiedInitiator;
    if (initiator.type === 'parser') {
      simplifiedInitiator = {
        type: 'parser',
        url: initiator.url
      };
    } else if (initiator.type === 'script') {
      simplifiedInitiator = {
        type: 'script',
        urls: [...(new Set(initiator.stack.callFrames.map(frame => frame.url).filter(v => Boolean(v))))],
        functions: [...(new Set(initiator.stack.callFrames.map(frame => frame.functionName).filter(v => Boolean(v))))],
      };
    } else {
      simplifiedInitiator = initiator;
    }

to extract a "simplified" version of the initiator.

@FranklinYu
Copy link

Note to myself (hope this helps someone else): the type definition of initiator is at

https://chromedevtools.github.io/devtools-protocol/tot/Network#type-Initiator

@aslushnikov aslushnikov added the chromium Issues with Puppeteer-Chromium label Dec 6, 2018
@Marmeladenbrot
Copy link

Any news about that / solution for Puppeteer to get the initiator?

atersolis added a commit to atersolis/puppeteer that referenced this issue Jun 12, 2019
add request.initiator() method, with tests and api doc

Requested by puppeteer#1395
@Vasile-Peste
Copy link

Is there a way to get a initiator also for cookies?

@ojaswa1942
Copy link

ojaswa1942 commented Oct 1, 2020

@Vasile-Peste +1. Looking for this too! Did you find any solution or workaround to this?

Currently the only solution I can think is to create a map between various existing parameters from requests and cookies, but that is likely to give false positives as per my use case (due to possible variations in domain and path properties of cookies).

But even then, a mapping solution with requests will still not work for cookies set using document.cookie.

@HarshiShah
Copy link

Any plans on adding the initiator information? I see pull requests for this feature open for more than 2 years.

jschfflr pushed a commit to atersolis/puppeteer that referenced this issue Sep 13, 2021
add request.initiator() method, with tests and api doc

Requested by puppeteer#1395
jschfflr pushed a commit to atersolis/puppeteer that referenced this issue Sep 13, 2021
add request.initiator() method, with tests and api doc

Requested by puppeteer#1395
@whalderman
Copy link

Thank you @jschfflr !

@BennyAlex
Copy link

@jschfflr

Hello,

Is it also possible to get the whole request Initiator chain?
Thanks!

@ojaswa1942
Copy link

@BennyAlex Did you happen to extract this information?

@BennyAlex
Copy link

@ojaswa1942
No unfortunatelly not :/

@OrKoN
Copy link
Collaborator

OrKoN commented Jan 24, 2024

Looks like it's been implemented. I believe you would only get the initiator for the initial request in a redirect chain. Please file a new feature request for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chromium Issues with Puppeteer-Chromium feature upstream
Projects
None yet
Development

No branches or pull requests