-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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
Intercept target creation #3667
Comments
Is there any workaround for this problem in mean time ? I tried to apply stealth using extension instead of |
I am sorry this might be a silly question but can you show how const browserContext = await browser.defaultBrowserContext()
//Error: browserContext.setTargetInterception is not a function
await browserContext.setTargetInterception(true); What is the right way of defining |
It's not a working code, the method is a feature request that not yet be implemented: look the OP's word:
|
@swang2019 hahaha. Thanks. I get it. I can't wait for this feature to be out. |
Is there an ETA on a fix for this? Browsing pages I do not control, I am watching for the After the fact, I can get the URLs of things that loaded by inspecting the trace file, but it's difficult to pinpoint only the ones loaded by the popup. In addition, I don't have access to all the other goodies provided by the Request object (headers, content, etc.). A fix or any workaround suggestion would be greatly appreciated! |
Hey. |
I'm playing with the CDP protocol a little bit in this regard and got a rough prototype working (intercept implicitly created targets, attach scripts or modify headers and continue loading the page). My main issue is the "continue loading" bit: When emitting The result is empty, so I'm not sure that's successful (I tried adding
I'm probably using it incorrectly but unfortunately I can't really find documentation or code snippets for this. If anyone knows if I'm missing a crucial step here to make the page load please tell me :) I'll continue playing with this and see if I can make a plugin for Here are the main bits (very much WIP): const puppeteer = require("puppeteer-extra")
const { CDPSession } = require("puppeteer/lib/Connection")
puppeteer
.launch({
headless: false
})
.then(async browser => {
const page = await browser.newPage()
await page._client.send("Target.setAutoAttach", {
autoAttach: true,
flatten: true,
windowOpen: true,
waitForDebuggerOnStart: true // is set to false in pptr
})
page._client.on(
"Target.attachedToTarget",
async event => {
if (event.waitingForDebugger) {
console.log("continue")
const newConn = await browser._connection.createSession(
event.targetInfo
)
await newConn.send("Network.setExtraHTTPHeaders", {
headers: { "x-foo": "hi-there" }
})
await newConn.send("Runtime.enable")
await newConn.send("Page.setLifecycleEventsEnabled", {
enabled: true
})
await newConn.send("Network.enable")
await newConn.send("Page.enable")
await newConn.send("Runtime.runIfWaitingForDebugger")
console.log("all sent")
await newConn.send("Page.reload") // Will not load without
}
}
)
await page.goto(
"file:///temp/popup.html" // HTML page that will open a popup
)
}) Custom |
That looks like good progress @berstend. I checked and if you call By the way the relevant issue in the Chrome Devtools issue tracker is: ChromeDevTools/devtools-protocol#77 |
That's good to know. Still haven't found a "proper" way to make the target continue loading after we've intercepted it though. I'll be looking into testing the various |
Playwright seems to have worked around this/provided a fix: microsoft/playwright#1226 |
@cris691 Playwright comes with a Chromium nightly build, so it's hard to say if they fixed it in the Playwright code or if Chromium had a patch. But it seems the bug is still present when testing in Puppeteer with the latest Chromium. Overwriting window.open is no solution, the same issue happens with regular links with |
Not sure what you're trying to say, but yes - I tried in Playwright and it works there. Here's a simple test case I submitted to the Chromium bug tracker: https://bugs.chromium.org/p/chromium/issues/attachmentText?aid=447951 (works directly on CDP protocol). |
Is this still not achieved? |
This is ridiculous that such obvious thing just undoable. Device simulation with multiple tabs is just unusable without this :( |
———————————————————————————————————————————————————————— |
Everyone who's interested in seeing this fixed: Please star this chromium bug reported by Niek, so the chromium devs know there's strong interest in getting this resolved - thanks! https://bugs.chromium.org/p/chromium/issues/detail?id=1070568 |
In this PR, we refactor Puppeteer to make use of CDP's auto-attach mechanism. This allows the backend to pause new targets and give Puppeteer a chance to configure them properly. This fixes the flakiness related to dealing with OOPIFs and should fix a multitude of issues related to the network interception and navigations. If those are not fixed completely by this PR, the PR serves a solid base for fixing them Closes #8507, #7990 Unlocks #3667 BREAKING CHANGE: With Chromium, Puppeteer will now attach to page/iframe targets immediately. BREAKING CHANGE: Browser.connect requires an explicit product name when connecting to Firefox since Firefox does not support CDP's auto-attach.
In this PR, we refactor Puppeteer to make use of CDP's auto-attach mechanism. This allows the backend to pause new targets and give Puppeteer a chance to configure them properly. This fixes the flakiness related to dealing with OOPIFs and should fix a multitude of issues related to the network interception and navigations. If those are not fixed completely by this PR, the PR serves a solid base for fixing them Closes #8507, #7990 Unlocks #3667 BREAKING CHANGE: With Chromium, Puppeteer will now attach to page/iframe targets immediately. BREAKING CHANGE: Browser.connect requires an explicit product name when connecting to Firefox since Firefox does not support CDP's auto-attach.
In this PR, we refactor Puppeteer to make use of CDP's auto-attach mechanism. This allows the backend to pause new targets and give Puppeteer a chance to configure them properly. This fixes the flakiness related to dealing with OOPIFs and should fix a multitude of issues related to the network interception and navigations. If those are not fixed completely by this PR, the PR serves a solid base for fixing them Closes #8507, #7990 Unlocks #3667 BREAKING CHANGE: With Chromium, Puppeteer will now attach to page/iframe targets immediately. BREAKING CHANGE: Browser.connect requires an explicit product name when connecting to Firefox since Firefox does not support CDP's auto-attach.
In this PR, we refactor Puppeteer to make use of CDP's auto-attach mechanism. This allows the backend to pause new targets and give Puppeteer a chance to configure them properly. This fixes the flakiness related to dealing with OOPIFs and should fix a multitude of issues related to the network interception and navigations. If those are not fixed completely by this PR, the PR serves a solid base for fixing them Closes #8507, #7990 Unlocks #3667 BREAKING CHANGE: With Chromium, Puppeteer will now attach to page/iframe targets immediately. BREAKING CHANGE: Browser.connect requires an explicit product name when connecting to Firefox since Firefox does not support CDP's auto-attach.
In this PR, we refactor Puppeteer to make use of CDP's auto-attach mechanism. This allows the backend to pause new targets and give Puppeteer a chance to configure them properly. This fixes the flakiness related to dealing with OOPIFs and should fix a multitude of issues related to the network interception and navigations. If those are not fixed completely by this PR, the PR serves a solid base for fixing them Closes #8507, #7990 Unlocks #3667 BREAKING CHANGE: With Chromium, Puppeteer will now attach to page/iframe targets immediately. BREAKING CHANGE: Browser.connect requires an explicit product name when connecting to Firefox since Firefox does not support CDP's auto-attach.
In this PR, we refactor Puppeteer to make use of CDP's auto-attach mechanism. This allows the backend to pause new targets and give Puppeteer a chance to configure them properly. This fixes the flakiness related to dealing with OOPIFs and should fix a multitude of issues related to the network interception and navigations. If those are not fixed completely by this PR, the PR serves a solid base for fixing them Closes #8507, #7990 Unlocks #3667 BREAKING CHANGE: With Chromium, Puppeteer will now attach to page/iframe targets immediately. BREAKING CHANGE: Browser.connect requires an explicit product name when connecting to Firefox since Firefox does not support CDP's auto-attach.
In this PR, we refactor Puppeteer to make use of CDP's auto-attach mechanism. This allows the backend to pause new targets and give Puppeteer a chance to configure them properly. This fixes the flakiness related to dealing with OOPIFs and should fix a multitude of issues related to the network interception and navigations. If those are not fixed completely by this PR, the PR serves a solid base for fixing them Closes #8507, #7990 Unlocks #3667 BREAKING CHANGE: With Chromium, Puppeteer will now attach to page/iframe targets immediately. BREAKING CHANGE: Browser.connect requires an explicit product name when connecting to Firefox since Firefox does not support CDP's auto-attach.
In this PR, we refactor Puppeteer to make use of CDP's auto-attach mechanism. This allows the backend to pause new targets and give Puppeteer a chance to configure them properly. This fixes the flakiness related to dealing with OOPIFs and should fix a multitude of issues related to the network interception and navigations. If those are not fixed completely by this PR, the PR serves a solid base for fixing them Closes #8507, #7990 Unlocks #3667 BREAKING CHANGE: With Chromium, Puppeteer will now attach to page/iframe targets immediately. BREAKING CHANGE: Browser.connect requires an explicit product name when connecting to Firefox since Firefox does not support CDP's auto-attach.
In this PR, we refactor Puppeteer to make use of CDP's auto-attach mechanism. This allows the backend to pause new targets and give Puppeteer a chance to configure them properly. This fixes the flakiness related to dealing with OOPIFs and should fix a multitude of issues related to the network interception and navigations. If those are not fixed completely by this PR, the PR serves a solid base for fixing them Closes #8507, #7990 Unlocks #3667 BREAKING CHANGE: With Chromium, Puppeteer will now attach to page/iframe targets immediately. BREAKING CHANGE: Browser.connect requires an explicit product name when connecting to Firefox since Firefox does not support CDP's auto-attach.
In this PR, we refactor Puppeteer to make use of CDP's auto-attach mechanism. This allows the backend to pause new targets and give Puppeteer a chance to configure them properly. This fixes the flakiness related to dealing with OOPIFs and should fix a multitude of issues related to the network interception and navigations. If those are not fixed completely by this PR, the PR serves a solid base for fixing them Closes #8507, #7990 Unlocks #3667 BREAKING CHANGE: With Chromium, Puppeteer will now attach to page/iframe targets immediately. BREAKING CHANGE: Browser.connect requires an explicit product name when connecting to Firefox since Firefox does not support CDP's auto-attach.
In this PR, we refactor Puppeteer to make use of CDP's auto-attach mechanism. This allows the backend to pause new targets and give Puppeteer a chance to configure them properly. This fixes the flakiness related to dealing with OOPIFs and should fix a multitude of issues related to the network interception and navigations. If those are not fixed completely by this PR, the PR serves a solid base for fixing them Closes #8507, #7990 Unlocks #3667 BREAKING CHANGE: With Chromium, Puppeteer will now attach to page/iframe targets immediately. BREAKING CHANGE: Browser.connect requires an explicit product name when connecting to Firefox since Firefox does not support CDP's auto-attach.
* feat: use CDP's auto-attach mechanism In this PR, we refactor Puppeteer to make use of the CDP's auto-attach mechanism. This allows the backend to pause new targets and give Puppeteer a chance to configure them properly. This fixes the flakiness related to dealing with OOPIFs and should fix some other issues related to the network interception and navigations. If those are not fixed completely by this PR, the PR serves a solid base for fixing them. Closes #8507, #7990 Unlocks #3667 BREAKING CHANGE: With Chromium, Puppeteer will now attach to page/iframe targets immediately. BREAKING CHANGE: Browser.connect requires an explicit product name when connecting to Firefox since Firefox does not support CDP's auto-attach.
* feat: use CDP's auto-attach mechanism In this PR, we refactor Puppeteer to make use of the CDP's auto-attach mechanism. This allows the backend to pause new targets and give Puppeteer a chance to configure them properly. This fixes the flakiness related to dealing with OOPIFs and should fix some other issues related to the network interception and navigations. If those are not fixed completely by this PR, the PR serves a solid base for fixing them. Closes #8507, #7990 Unlocks #3667 BREAKING CHANGE: With Chromium, Puppeteer will now attach to page/iframe targets immediately. Browser.connect requires an explicit product name when connecting to Firefox since Firefox does not support CDP's auto-attach.
* feat: use CDP's auto-attach mechanism In this PR, we refactor Puppeteer to make use of the CDP's auto-attach mechanism. This allows the backend to pause new targets and give Puppeteer a chance to configure them properly. This fixes the flakiness related to dealing with OOPIFs and should fix some other issues related to the network interception and navigations. If those are not fixed completely by this PR, the PR serves a solid base for fixing them. Closes #8507, #7990 Unlocks #3667 BREAKING CHANGE: With Chromium, Puppeteer will now attach to page/iframe targets immediately to allow reliable configuration of targets.
A temporary workaround until this issue is fixed, which I believe might be useful for some of you. import * as puppeteer from 'puppeteer';
const browser = await puppeteer.launch({
headless: false,
args: [
'--headless',
'--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
],
defaultViewport: { width: 1920, height: 900 },
}); |
hi @Junyan , it seems that newer versions of pupeteer now hide the I could hack it with: const { CDPBrowser } = require('../node_modules/puppeteer/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Browser.js');
.
.
.
let browserConnection;
const oldCreate = CDPBrowser._create;
CDPBrowser._create = (product, connection, ...args) => {
browserConnection = connection;
return oldCreate(product, connection, ...args);
} it would really be great if this could be done properly. |
@tripodsan It looks like a solution.. I don't have any new ideas because we have not upgraded puppeteer. |
The proposal in this issue should now be possible if anyone wants to try to implement it. in ChromeTargetManager it is possible to safely await until the target is is configured by the user code. |
Have puppeteer any possibility to catch which scripts call new tab open for catching popups? |
I have found a solution for intercepting popups using CDP in the latest version of puppeteer for my extremely specific use case, but hopefully this is helpful for others. Essentially, what you need to do is:
Here's some example code for my use case - I wanted to see what URL a popup is trying to go to, without letting it actually load the popup: import puppeteer from 'puppeteer';
let browser = await puppeteer.launch({
headless: false
});
const page = (await browser.pages())[0];
// Set this to a URL where you can click on something that opens a popup
await page.goto('https://www.example.com');
const cdp = await browser.target().createCDPSession();
// Set up auto-attaching, set the filter to pages only
await cdp.send('Target.setAutoAttach', {
waitForDebuggerOnStart: true,
autoAttach: true,
flatten: true,
filter: [
{ type: 'page', exclude: false },
]
})
cdp.on('Target.attachedToTarget', async ({sessionId, targetInfo, waitingForDebugger}) => {
// Get the puppeteer Connection object from the existing CDP connection
let connection = cdp.connection();
if (!connection) {
console.log('Could not get connection');
return;
}
// Get the CDP session that has attached to the popup
let popupCdp = connection.session(sessionId);
if (!popupCdp) {
console.log('Could not get popup CDP session');
return;
}
// Do whatever setup you want to do in the popup here
await popupCdp.send('Fetch.enable');
popupCdp.on('Fetch.requestPaused', async ({requestId, request}) => {
console.log('Intercepted popup - attempted to open URL:', request.url);
popupCdp?.send('Fetch.failRequest', {requestId, errorReason: 'Aborted'});
});
// Allow the popup to continue executing
await popupCdp.send('Runtime.runIfWaitingForDebugger');
});
console.log('ready');
// Click on something that opens a popup |
seems like However, I get |
For anyone still looking for a solution to this, a potential workaround would be to create an extension that blocks network requests for newly created tabs for a while, giving you enough time to do whatever you need - then trigger a reload. |
In October 2024, the issue is still there. |
In many ways, users want to intercept targets being created to attach and set them up.
Usecases:
We might be able to do this with CDP using
Target.setAutoAttach
andwaitForDebugger
option. I'd like this to be scoped to browser context though so that's there's a better flexibility.The API might look like this:
Related issues: #1378, #3648
The text was updated successfully, but these errors were encountered: