yiyan-browser-agent 1.0.22 → 1.0.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/browser-manager.js +26 -33
package/package.json
CHANGED
package/src/browser-manager.js
CHANGED
|
@@ -10,12 +10,16 @@ const config = require('./config');
|
|
|
10
10
|
|
|
11
11
|
const CDP_PORT_FILE = path.join(os.homedir(), '.yiyan-agent', 'cdp-port.json');
|
|
12
12
|
const CDP_PORT = 9222;
|
|
13
|
+
const SESSION_DIR = path.join(os.homedir(), '.yiyan-agent', 'session');
|
|
13
14
|
|
|
14
15
|
class BrowserManager {
|
|
15
16
|
/**
|
|
16
17
|
* Get browser - connect existing or launch new
|
|
17
18
|
*/
|
|
18
19
|
static async getInstance() {
|
|
20
|
+
// Ensure directories exist
|
|
21
|
+
fs.mkdirSync(SESSION_DIR, { recursive: true });
|
|
22
|
+
|
|
19
23
|
// Try connect existing browser
|
|
20
24
|
try {
|
|
21
25
|
if (fs.existsSync(CDP_PORT_FILE)) {
|
|
@@ -25,20 +29,24 @@ class BrowserManager {
|
|
|
25
29
|
logger.info('Connecting to existing browser...');
|
|
26
30
|
const browser = await chromium.connectOverCDP(browserURL, { timeout: 5000 });
|
|
27
31
|
|
|
28
|
-
// Create NEW page (tab) for this task
|
|
29
|
-
const
|
|
32
|
+
// Create NEW context and page (tab) for this task
|
|
33
|
+
const context = await browser.newContext({
|
|
34
|
+
viewport: { width: 1280, height: 900 },
|
|
35
|
+
userAgent: 'Mozilla/5.0 AppleWebKit/537.36 Chrome/124.0.0.0 Safari/537.36',
|
|
36
|
+
});
|
|
37
|
+
const page = await context.newPage();
|
|
30
38
|
await page.goto(config.YIYAN_URL, { waitUntil: 'domcontentloaded', timeout: 15000 });
|
|
31
39
|
await page.waitForTimeout(500);
|
|
32
40
|
|
|
33
|
-
logger.success('Connected!
|
|
34
|
-
return { browser, page, isNew: false };
|
|
41
|
+
logger.success('Connected! New tab opened.');
|
|
42
|
+
return { browser, context, page, isNew: false };
|
|
35
43
|
}
|
|
36
44
|
} catch (err) {
|
|
37
45
|
logger.warn('Connection failed, launching new browser...');
|
|
38
46
|
try { fs.unlinkSync(CDP_PORT_FILE); } catch {}
|
|
39
47
|
}
|
|
40
48
|
|
|
41
|
-
// Launch new browser with CDP
|
|
49
|
+
// Launch new browser with CDP port
|
|
42
50
|
return await this._launchNew();
|
|
43
51
|
}
|
|
44
52
|
|
|
@@ -46,38 +54,32 @@ class BrowserManager {
|
|
|
46
54
|
* Launch new browser with CDP port
|
|
47
55
|
*/
|
|
48
56
|
static async _launchNew() {
|
|
49
|
-
logger.info('Launching new browser...');
|
|
50
|
-
|
|
51
|
-
// Ensure session directory
|
|
52
|
-
const sessionDir = path.join(os.homedir(), '.yiyan-agent', 'session');
|
|
53
|
-
fs.mkdirSync(sessionDir, { recursive: true });
|
|
57
|
+
logger.info('Launching new browser with CDP port ' + CDP_PORT + '...');
|
|
54
58
|
|
|
55
|
-
// Launch
|
|
56
|
-
const
|
|
59
|
+
// Launch browser (not persistentContext) to expose CDP properly
|
|
60
|
+
const browser = await chromium.launch({
|
|
57
61
|
headless: false,
|
|
58
|
-
|
|
62
|
+
port: CDP_PORT,
|
|
59
63
|
args: [
|
|
60
|
-
`--remote-debugging-port=${CDP_PORT}`,
|
|
61
64
|
'--disable-blink-features=AutomationControlled',
|
|
62
65
|
'--no-first-run',
|
|
63
66
|
'--no-sandbox',
|
|
67
|
+
`--user-data-dir=${SESSION_DIR}`,
|
|
64
68
|
],
|
|
65
|
-
userAgent: 'Mozilla/5.0 AppleWebKit/537.36 Chrome/124.0.0.0 Safari/537.36',
|
|
66
69
|
});
|
|
67
70
|
|
|
68
|
-
//
|
|
69
|
-
const browser = context.browser();
|
|
70
|
-
|
|
71
|
-
// Save CDP port
|
|
71
|
+
// Save CDP port info
|
|
72
72
|
fs.writeFileSync(CDP_PORT_FILE, JSON.stringify({
|
|
73
73
|
port: CDP_PORT,
|
|
74
|
-
pid: process.pid,
|
|
75
74
|
launchedAt: Date.now()
|
|
76
75
|
}));
|
|
77
76
|
|
|
78
|
-
//
|
|
79
|
-
const
|
|
80
|
-
|
|
77
|
+
// Create context and page
|
|
78
|
+
const context = await browser.newContext({
|
|
79
|
+
viewport: { width: 1280, height: 900 },
|
|
80
|
+
userAgent: 'Mozilla/5.0 AppleWebKit/537.36 Chrome/124.0.0.0 Safari/537.36',
|
|
81
|
+
});
|
|
82
|
+
const page = await context.newPage();
|
|
81
83
|
|
|
82
84
|
await page.goto(config.YIYAN_URL, { waitUntil: 'domcontentloaded', timeout: 15000 });
|
|
83
85
|
await page.waitForTimeout(800);
|
|
@@ -86,21 +88,12 @@ class BrowserManager {
|
|
|
86
88
|
return { browser, context, page, isNew: true };
|
|
87
89
|
}
|
|
88
90
|
|
|
89
|
-
/**
|
|
90
|
-
* Close current page (tab) but keep browser running
|
|
91
|
-
*/
|
|
92
|
-
static async closePage(page) {
|
|
93
|
-
try {
|
|
94
|
-
await page.close();
|
|
95
|
-
} catch {}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
91
|
/**
|
|
99
92
|
* Force close everything (for cleanup)
|
|
100
93
|
*/
|
|
101
94
|
static async forceClose() {
|
|
102
95
|
try { fs.unlinkSync(CDP_PORT_FILE); } catch {}
|
|
103
|
-
// Browser
|
|
96
|
+
// Browser continues running independently
|
|
104
97
|
}
|
|
105
98
|
}
|
|
106
99
|
|