@@ -172,7 +172,7 @@ export const CDPBrowser: React.FC<CDPBrowserProps> = ({ url, onError }) => {
172
172
defaultViewport : {
173
173
width : 1280 ,
174
174
height : 800 ,
175
- deviceScaleFactor : 1 ,
175
+ deviceScaleFactor : 0 ,
176
176
hasTouch : false ,
177
177
isLandscape : true ,
178
178
isMobile : false ,
@@ -187,7 +187,7 @@ export const CDPBrowser: React.FC<CDPBrowserProps> = ({ url, onError }) => {
187
187
await page . setViewport ( {
188
188
width : 1280 ,
189
189
height : 800 ,
190
- deviceScaleFactor : 1 ,
190
+ deviceScaleFactor : 0 ,
191
191
hasTouch : false ,
192
192
isLandscape : true ,
193
193
isMobile : false ,
@@ -268,16 +268,33 @@ export const CDPBrowser: React.FC<CDPBrowserProps> = ({ url, onError }) => {
268
268
} ) ;
269
269
} ;
270
270
271
- browser . on ( 'targetchanged' , async ( target : any ) => {
271
+ // 修改:同时监听 targetchanged 和 targetcreated 事件
272
+ const handleTarget = async ( target : any ) => {
272
273
if ( target . type ( ) === 'page' ) {
273
274
try {
274
275
const newPage = await target . page ( ) ;
275
276
if ( newPage && newPage !== pageRef . current ) {
277
+ if ( clientRef . current ) {
278
+ // 确保清理旧的 screencast
279
+ await clientRef . current
280
+ . send ( 'Page.stopScreencast' )
281
+ . catch ( console . error ) ;
282
+ clientRef . current . off ( 'Page.screencastFrame' ) ;
283
+ }
276
284
await setupPageScreencast ( newPage ) ;
277
285
}
278
- } catch ( error ) { }
286
+ } catch ( error ) {
287
+ console . error ( 'Failed to setup page screencast:' , error ) ;
288
+ if ( onError ) {
289
+ onError ( error instanceof Error ? error . message : String ( error ) ) ;
290
+ }
291
+ }
279
292
}
280
- } ) ;
293
+ } ;
294
+
295
+ browser . on ( 'targetchanged' , handleTarget ) ;
296
+ browser . on ( 'targetcreated' , handleTarget ) ;
297
+
281
298
const pages = await browser . pages ( ) ;
282
299
const page =
283
300
pages . length > 0 ? pages [ pages . length - 1 ] : await browser . newPage ( ) ;
@@ -296,7 +313,7 @@ export const CDPBrowser: React.FC<CDPBrowserProps> = ({ url, onError }) => {
296
313
await pageRef . current . setViewport ( {
297
314
width : 1280 ,
298
315
height : 800 ,
299
- deviceScaleFactor : 1 ,
316
+ deviceScaleFactor : 0 ,
300
317
hasTouch : false ,
301
318
isLandscape : true ,
302
319
isMobile : false ,
0 commit comments