mirror of
https://github.com/getmaxun/maxun.git
synced 2026-10-17 04:30:39 +08:00
fix: error and crash handling browser
This commit is contained in:
@@ -253,49 +253,63 @@ export class RemoteBrowser {
|
||||
}
|
||||
|
||||
page.on('framenavigated', async (frame) => {
|
||||
if (frame === page.mainFrame()) {
|
||||
const currentUrl = page.url();
|
||||
if (this.shouldEmitUrlChange(currentUrl)) {
|
||||
this.lastEmittedUrl = currentUrl;
|
||||
this.broadcast('urlChanged', { url: currentUrl, userId: this.userId });
|
||||
}
|
||||
|
||||
await page.evaluate(() => {
|
||||
if (window.rrweb && window.isRecording) {
|
||||
window.isRecording = false;
|
||||
try {
|
||||
if (frame === page.mainFrame()) {
|
||||
if (page.isClosed()) {
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
if (this.isRecordingMode) {
|
||||
await page.waitForLoadState('networkidle', { timeout: 10000 }).catch(() => {
|
||||
logger.warn('[rrweb] Network idle timeout on navigation, proceeding with rrweb initialization');
|
||||
});
|
||||
const currentUrl = page.url();
|
||||
if (this.shouldEmitUrlChange(currentUrl)) {
|
||||
this.lastEmittedUrl = currentUrl;
|
||||
this.broadcast('urlChanged', { url: currentUrl, userId: this.userId });
|
||||
}
|
||||
|
||||
await this.initializeRRWebRecording(page);
|
||||
await page.evaluate(() => {
|
||||
if (window.rrweb && window.isRecording) {
|
||||
window.isRecording = false;
|
||||
}
|
||||
}).catch(() => {});
|
||||
|
||||
if (this.isRecordingMode && !page.isClosed()) {
|
||||
await page.waitForLoadState('networkidle', { timeout: 10000 }).catch(() => {
|
||||
logger.warn('[rrweb] Network idle timeout on navigation, proceeding with rrweb initialization');
|
||||
});
|
||||
|
||||
await this.initializeRRWebRecording(page).catch((error: any) => {
|
||||
logger.warn(`[rrweb] Failed to initialize recording on navigation: ${error?.message}`);
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error: any) {
|
||||
logger.warn(`Error handling framenavigated event: ${error?.message}`);
|
||||
}
|
||||
});
|
||||
|
||||
page.on('load', async () => {
|
||||
const injectScript = async (): Promise<boolean> => {
|
||||
try {
|
||||
await page.waitForLoadState('networkidle', { timeout: 5000 });
|
||||
try {
|
||||
const injectScript = async (): Promise<boolean> => {
|
||||
try {
|
||||
await page.waitForLoadState('networkidle', { timeout: 5000 });
|
||||
|
||||
if (page.isClosed()) {
|
||||
logger.debug('Page is closed, cannot inject script');
|
||||
if (page.isClosed()) {
|
||||
logger.debug('Page is closed, cannot inject script');
|
||||
return false;
|
||||
}
|
||||
|
||||
await page.evaluate(getInjectableScript());
|
||||
return true;
|
||||
} catch (error: any) {
|
||||
logger.log('warn', `Script injection attempt failed: ${error.message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
await page.evaluate(getInjectableScript());
|
||||
return true;
|
||||
} catch (error: any) {
|
||||
logger.log('warn', `Script injection attempt failed: ${error.message}`);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const success = await injectScript();
|
||||
console.log("Script injection result:", success);
|
||||
const success = await injectScript();
|
||||
console.log("Script injection result:", success);
|
||||
} catch (error: any) {
|
||||
logger.warn(`Error handling page load event: ${error?.message}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -71,6 +71,7 @@ export async function processRobotOutputFormats(
|
||||
delete pageResult.html;
|
||||
}
|
||||
if (!effectiveFormats.includes('text')) delete pageResult.text;
|
||||
if (!effectiveFormats.includes('links')) delete pageResult.links;
|
||||
|
||||
const pageUrl = pageResult.metadata?.url || pageResult.url;
|
||||
const hasPageUrl = typeof pageUrl === 'string' && pageUrl.trim() !== '';
|
||||
@@ -141,6 +142,7 @@ export async function processRobotOutputFormats(
|
||||
delete result.html;
|
||||
}
|
||||
if (!effectiveFormats.includes('text')) delete result.text;
|
||||
if (!effectiveFormats.includes('links')) delete result.links;
|
||||
|
||||
const resultUrl = result.metadata?.url || result.url;
|
||||
const hasResultUrl = typeof resultUrl === 'string' && resultUrl.trim() !== '';
|
||||
|
||||
Reference in New Issue
Block a user