-
-
Notifications
You must be signed in to change notification settings - Fork 742
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
Solving request interception chain #364
Comments
@XavierGeerinck thanks for the well thought-out comment 👍 It's been a while since I worked on that aspect, but I remember the main annoyance being that when request interception is enabled someone needs to decide if the request shall be continued or aborted (but that decision can only happen once). That makes it a bit annoying to work with from a plugin framework perspective 😄 If I remember correctly puppeteer is holding an internal state for this. If all code happens within plugins this would be relatively simple to fix, it get's more tricky when we need to take care of code in the user's scripts as well (by patching I would need to refresh my memory on the issues I ran into while working on a solution to this a while back, happy for other input on this though. |
Well I am personally fine if plugins have to be written for this to work inside the core, but I also believe a better solution could be found. The solution I personally was thinking was to indeed handle this within the plugin framework and:
Is this something that could be added? Would love to see this upstream! In the meantime I also opened a request on puppeteer for official plugin support as well as solving this use case: puppeteer/puppeteer#6567 let me know what you think! |
A simple solution would be. page.on('request', async (req) => {
if (req._interceptionHandled) return;
/* ... */
} |
I am not sure tbh.. this is what I used before and it will then crash further on in the code if you have multiple listeners, since the request was already handled then. |
I'm lacking the bandwidth at the moment to look into this properly, but I'm pretty sure there's a way to fix this so multiple plugins as well as external scripts can use request interception at the same time. :-) I will revisit this once playwright support and the new stealth plugin has landed, if anyone else wants to take a stab at this in the meantime that'd be great as well. |
I guess one "transparent" way to handle this could be to patch In terms of order of execution: First come first served? That would mean plugins get the first pick (in order they're added), followed by handlers the user might've defined. |
edit: @berstend I had a similar idea, PR in puppeteer/puppeteer#6735 I'm here because I hit this same issue. My use case is I want to intercept pptr requests and potentially pull them from a Redis cache rather than repeating the request or relying on the pptr cache. Unfortunately, whoever calls To me, this means pptr can have at most one request interceptor in page.on('request', req=> {
const votes = await Promise.all(px.onRequestListeners.map( (e)=>e(req) ))
const shouldContinue = votes.reduce( (a, e)=>a&&e, true)
if(shouldContinue) req.continue() else req.abort()
}); |
Note: I published a package to fix this https://www.npmjs.com/package/enchant-puppeteer |
…: No resource with given identifier found` We recently started using [`enchant-puppeteer`](berstend#364 (comment)) and doing our own asynchronous request interception while also using `puppeteer-extra-plugin-stealth`. It seems like this combination makes it much more likely for this error to happen, especially on pages with redirects. The error was causing the whole process to be killed. This seems relevant: puppeteer/puppeteer#2258 (comment) We aren’t sure this is the right way to handle this problem. However, this seems like an improvement.
@benallfree any specific reason your upstream PR is closed but not merged? 🤔 |
@berstend Sorry for the confusion, it was moved to puppeteer/puppeteer#6735 |
We recently started using [`enchant-puppeteer`](#364 (comment)) and doing our own asynchronous request interception while also using `puppeteer-extra-plugin-stealth`. It seems like this combination makes it much more likely for this error to happen, especially on pages with redirects. The error was causing the whole process to be killed. This seems relevant: puppeteer/puppeteer#2258 (comment) We aren’t sure this is the right way to handle this problem. However, this seems like an improvement.
@benallfree Your PR puppeteer/puppeteer#6735 was already merged. What would need to be done to completely fix the issue in |
@FdezRomero Yes, here are the upgrade instructions: https://github.com/puppeteer/puppeteer/blob/v11.0.0/docs/api.md#upgrading-to-cooperative-mode-for-package-maintainers Basically it entails adding a 2nd arg to each Also take note of the If you'd like hands-on help implementing this upgrade, please let me know. |
@benallfree Thanks a lot for the details! I'll try adding support for Cooperative Mode for the stealth, adblocker and resource blocker plugins and confirm that they get along well 👍🏻 I agree that it should be made possible to define the priorities, so I'll add |
@berstend @benallfree I created a PR to fix this issue for both plugins in #592, feedback appreciated 😄 |
@berstend Looks like PR is stuck waiting for review for few months now. Can we please merge this? |
Hi Everyone!
Currently I am having the problem that when you have 2 plugins that try to intercept and handle requests, that the dependencies start to fail. (E.g. when I have the adblocker plugin and a personal plugin that both try to block resources, then it starts stating that a request has already been handled).
I agree that this is how it is with Puppeteer and that it will be hard to fix it (sadly enough). But seeing the plugin interface, I think it might be worthwhile to find a solution to this.
Some thoughts that I had:
I'd love to hear some thoughts from the community to see if this is feasible or not? Looking forward to the responses!
The text was updated successfully, but these errors were encountered: