@jackwener/opencli 1.0.0 → 1.0.3

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.
Files changed (171) hide show
  1. package/.github/workflows/build-extension.yml +62 -0
  2. package/.github/workflows/ci.yml +6 -6
  3. package/.github/workflows/e2e-headed.yml +2 -2
  4. package/.github/workflows/pkg-pr-new.yml +2 -2
  5. package/.github/workflows/release.yml +2 -5
  6. package/.github/workflows/security.yml +2 -2
  7. package/CDP.md +1 -1
  8. package/CDP.zh-CN.md +1 -1
  9. package/README.md +35 -8
  10. package/README.zh-CN.md +35 -8
  11. package/SKILL.md +3 -5
  12. package/dist/browser/cdp.d.ts +27 -0
  13. package/dist/browser/cdp.js +295 -0
  14. package/dist/browser/daemon-client.d.ts +1 -1
  15. package/dist/browser/index.d.ts +4 -2
  16. package/dist/browser/index.js +5 -5
  17. package/dist/browser/mcp.d.ts +5 -8
  18. package/dist/browser/mcp.js +9 -10
  19. package/dist/browser/page.d.ts +8 -1
  20. package/dist/browser/page.js +25 -40
  21. package/dist/browser/utils.d.ts +10 -0
  22. package/dist/browser/utils.js +27 -0
  23. package/dist/browser.test.js +48 -7
  24. package/dist/chaoxing.d.ts +58 -0
  25. package/dist/chaoxing.js +225 -0
  26. package/dist/chaoxing.test.d.ts +1 -0
  27. package/dist/chaoxing.test.js +38 -0
  28. package/dist/cli-manifest.json +597 -14
  29. package/dist/cli.d.ts +1 -0
  30. package/dist/cli.js +197 -0
  31. package/dist/clis/apple-podcasts/episodes.d.ts +1 -0
  32. package/dist/clis/apple-podcasts/episodes.js +28 -0
  33. package/dist/clis/apple-podcasts/search.d.ts +1 -0
  34. package/dist/clis/apple-podcasts/search.js +29 -0
  35. package/dist/clis/apple-podcasts/top.d.ts +1 -0
  36. package/dist/clis/apple-podcasts/top.js +34 -0
  37. package/dist/clis/apple-podcasts/utils.d.ts +11 -0
  38. package/dist/clis/apple-podcasts/utils.js +30 -0
  39. package/dist/clis/apple-podcasts/utils.test.d.ts +1 -0
  40. package/dist/clis/apple-podcasts/utils.test.js +57 -0
  41. package/dist/clis/boss/chatlist.d.ts +1 -0
  42. package/dist/clis/boss/chatlist.js +50 -0
  43. package/dist/clis/boss/chatmsg.d.ts +1 -0
  44. package/dist/clis/boss/chatmsg.js +73 -0
  45. package/dist/clis/boss/send.d.ts +1 -0
  46. package/dist/clis/boss/send.js +176 -0
  47. package/dist/clis/chaoxing/assignments.d.ts +1 -0
  48. package/dist/clis/chaoxing/assignments.js +74 -0
  49. package/dist/clis/chaoxing/exams.d.ts +1 -0
  50. package/dist/clis/chaoxing/exams.js +74 -0
  51. package/dist/clis/chatgpt/ask.js +15 -14
  52. package/dist/clis/chatgpt/ax.d.ts +1 -0
  53. package/dist/clis/chatgpt/ax.js +78 -0
  54. package/dist/clis/chatgpt/read.js +5 -6
  55. package/dist/clis/chatwise/history.js +18 -1
  56. package/dist/clis/discord-app/channels.js +33 -21
  57. package/dist/clis/twitter/accept.d.ts +1 -0
  58. package/dist/clis/twitter/accept.js +202 -0
  59. package/dist/clis/twitter/followers.js +30 -22
  60. package/dist/clis/twitter/following.js +19 -14
  61. package/dist/clis/twitter/notifications.js +29 -22
  62. package/dist/clis/twitter/post.js +9 -2
  63. package/dist/clis/twitter/reply-dm.d.ts +1 -0
  64. package/dist/clis/twitter/reply-dm.js +181 -0
  65. package/dist/clis/twitter/search.js +30 -11
  66. package/dist/clis/weread/book.d.ts +1 -0
  67. package/dist/clis/weread/book.js +26 -0
  68. package/dist/clis/weread/highlights.d.ts +1 -0
  69. package/dist/clis/weread/highlights.js +23 -0
  70. package/dist/clis/weread/notebooks.d.ts +1 -0
  71. package/dist/clis/weread/notebooks.js +21 -0
  72. package/dist/clis/weread/notes.d.ts +1 -0
  73. package/dist/clis/weread/notes.js +29 -0
  74. package/dist/clis/weread/ranking.d.ts +1 -0
  75. package/dist/clis/weread/ranking.js +28 -0
  76. package/dist/clis/weread/search.d.ts +1 -0
  77. package/dist/clis/weread/search.js +25 -0
  78. package/dist/clis/weread/shelf.d.ts +1 -0
  79. package/dist/clis/weread/shelf.js +24 -0
  80. package/dist/clis/weread/utils.d.ts +20 -0
  81. package/dist/clis/weread/utils.js +72 -0
  82. package/dist/clis/weread/utils.test.d.ts +1 -0
  83. package/dist/clis/weread/utils.test.js +85 -0
  84. package/dist/clis/xiaohongshu/download.d.ts +1 -1
  85. package/dist/clis/xiaohongshu/download.js +1 -1
  86. package/dist/daemon.js +2 -2
  87. package/dist/doctor.d.ts +0 -21
  88. package/dist/doctor.js +2 -24
  89. package/dist/engine.js +24 -13
  90. package/dist/explore.js +46 -101
  91. package/dist/main.js +4 -203
  92. package/dist/output.d.ts +1 -1
  93. package/dist/registry.d.ts +3 -3
  94. package/dist/runtime.d.ts +1 -4
  95. package/dist/runtime.js +1 -4
  96. package/dist/scripts/framework.d.ts +4 -0
  97. package/dist/scripts/framework.js +21 -0
  98. package/dist/scripts/interact.d.ts +4 -0
  99. package/dist/scripts/interact.js +20 -0
  100. package/dist/scripts/store.d.ts +9 -0
  101. package/dist/scripts/store.js +44 -0
  102. package/dist/setup.js +2 -2
  103. package/dist/synthesize.js +1 -1
  104. package/extension/dist/background.js +392 -0
  105. package/extension/manifest.json +3 -3
  106. package/extension/package.json +1 -1
  107. package/extension/src/background.ts +101 -24
  108. package/extension/src/protocol.ts +1 -1
  109. package/package.json +1 -1
  110. package/src/browser/cdp.ts +295 -0
  111. package/src/browser/daemon-client.ts +1 -1
  112. package/src/browser/index.ts +5 -6
  113. package/src/browser/mcp.ts +14 -15
  114. package/src/browser/page.ts +25 -41
  115. package/src/browser/utils.ts +27 -0
  116. package/src/browser.test.ts +52 -6
  117. package/src/chaoxing.test.ts +45 -0
  118. package/src/chaoxing.ts +268 -0
  119. package/src/cli.ts +185 -0
  120. package/src/clis/antigravity/SKILL.md +5 -0
  121. package/src/clis/apple-podcasts/episodes.ts +28 -0
  122. package/src/clis/apple-podcasts/search.ts +29 -0
  123. package/src/clis/apple-podcasts/top.ts +34 -0
  124. package/src/clis/apple-podcasts/utils.test.ts +72 -0
  125. package/src/clis/apple-podcasts/utils.ts +37 -0
  126. package/src/clis/boss/chatlist.ts +50 -0
  127. package/src/clis/boss/chatmsg.ts +70 -0
  128. package/src/clis/boss/send.ts +193 -0
  129. package/src/clis/chaoxing/README.md +36 -0
  130. package/src/clis/chaoxing/README.zh-CN.md +35 -0
  131. package/src/clis/chaoxing/assignments.ts +88 -0
  132. package/src/clis/chaoxing/exams.ts +88 -0
  133. package/src/clis/chatgpt/ask.ts +14 -15
  134. package/src/clis/chatgpt/ax.ts +81 -0
  135. package/src/clis/chatgpt/read.ts +5 -7
  136. package/src/clis/chatwise/history.ts +15 -1
  137. package/src/clis/discord-app/channels.ts +33 -21
  138. package/src/clis/twitter/accept.ts +213 -0
  139. package/src/clis/twitter/followers.ts +36 -29
  140. package/src/clis/twitter/following.ts +25 -20
  141. package/src/clis/twitter/notifications.ts +34 -27
  142. package/src/clis/twitter/post.ts +9 -2
  143. package/src/clis/twitter/reply-dm.ts +193 -0
  144. package/src/clis/twitter/search.ts +34 -12
  145. package/src/clis/weread/book.ts +28 -0
  146. package/src/clis/weread/highlights.ts +25 -0
  147. package/src/clis/weread/notebooks.ts +23 -0
  148. package/src/clis/weread/notes.ts +31 -0
  149. package/src/clis/weread/ranking.ts +29 -0
  150. package/src/clis/weread/search.ts +26 -0
  151. package/src/clis/weread/shelf.ts +26 -0
  152. package/src/clis/weread/utils.test.ts +104 -0
  153. package/src/clis/weread/utils.ts +74 -0
  154. package/src/clis/xiaohongshu/download.ts +1 -1
  155. package/src/daemon.ts +2 -2
  156. package/src/doctor.ts +2 -19
  157. package/src/engine.ts +20 -13
  158. package/src/explore.ts +51 -100
  159. package/src/main.ts +4 -186
  160. package/src/output.ts +12 -12
  161. package/src/registry.ts +3 -3
  162. package/src/runtime.ts +2 -6
  163. package/src/scripts/framework.ts +20 -0
  164. package/src/scripts/interact.ts +22 -0
  165. package/src/scripts/store.ts +40 -0
  166. package/src/setup.ts +2 -2
  167. package/src/synthesize.ts +1 -1
  168. package/tests/e2e/public-commands.test.ts +68 -1
  169. package/dist/clis/grok/debug.d.ts +0 -1
  170. package/dist/clis/grok/debug.js +0 -45
  171. package/src/clis/grok/debug.ts +0 -49
@@ -13,6 +13,7 @@
13
13
  import { formatSnapshot } from '../snapshotFormatter.js';
14
14
  import type { IPage } from '../types.js';
15
15
  import { sendCommand } from './daemon-client.js';
16
+ import { wrapForEval } from './utils.js';
16
17
 
17
18
  /**
18
19
  * Page — implements IPage by talking to the daemon via HTTP.
@@ -37,6 +38,15 @@ export class Page implements IPage {
37
38
  }
38
39
  }
39
40
 
41
+ /** Close the automation window in the extension */
42
+ async closeWindow(): Promise<void> {
43
+ try {
44
+ await sendCommand('close-window', {});
45
+ } catch {
46
+ // Window may already be closed or daemon may be down
47
+ }
48
+ }
49
+
40
50
  async evaluate(js: string): Promise<any> {
41
51
  const code = wrapForEval(js);
42
52
  return sendCommand('exec', { code, ...this._tabOpt() });
@@ -183,12 +193,12 @@ export class Page implements IPage {
183
193
  return sendCommand('exec', { code, ...this._tabOpt() });
184
194
  }
185
195
 
186
- async consoleMessages(level: string = 'info'): Promise<any> {
187
- // Console messages can't be retrospectively read via CDP Runtime.evaluate.
188
- // Would need Runtime.consoleAPICalled event listener, which is not yet implemented.
189
- if (process.env.OPENCLI_VERBOSE) {
190
- console.error('[page] consoleMessages() not supported in lightweight mode — returning empty');
191
- }
196
+ /**
197
+ * Console messages are not available in lightweight daemon mode.
198
+ * Would require CDP Runtime.consoleAPICalled event listener.
199
+ * @returns Always returns empty array.
200
+ */
201
+ async consoleMessages(_level: string = 'info'): Promise<any> {
192
202
  return [];
193
203
  }
194
204
 
@@ -260,46 +270,20 @@ export class Page implements IPage {
260
270
 
261
271
  async installInterceptor(pattern: string): Promise<void> {
262
272
  const { generateInterceptorJs } = await import('../interceptor.js');
263
- await sendCommand('exec', {
264
- code: generateInterceptorJs(JSON.stringify(pattern), {
265
- arrayName: '__opencli_xhr',
266
- patchGuard: '__opencli_interceptor_patched',
267
- }),
268
- ...this._tabOpt(),
269
- });
273
+ // Must use evaluate() so wrapForEval() converts the arrow function into an IIFE;
274
+ // sendCommand('exec') sends the code as-is, and CDP never executes a bare arrow.
275
+ await this.evaluate(generateInterceptorJs(JSON.stringify(pattern), {
276
+ arrayName: '__opencli_xhr',
277
+ patchGuard: '__opencli_interceptor_patched',
278
+ }));
270
279
  }
271
280
 
272
281
  async getInterceptedRequests(): Promise<any[]> {
273
282
  const { generateReadInterceptedJs } = await import('../interceptor.js');
274
- const result = await sendCommand('exec', {
275
- code: generateReadInterceptedJs('__opencli_xhr'),
276
- ...this._tabOpt(),
277
- });
283
+ // Same as installInterceptor: must go through evaluate() for IIFE wrapping
284
+ const result = await this.evaluate(generateReadInterceptedJs('__opencli_xhr'));
278
285
  return (result as any[]) || [];
279
286
  }
280
287
  }
281
288
 
282
- // ─── Helpers ─────────────────────────────────────────────────────────
283
-
284
- /**
285
- * Wrap JS code for CDP Runtime.evaluate:
286
- * - Already an IIFE `(...)()` → send as-is
287
- * - Arrow/function literal → wrap as IIFE `(code)()`
288
- * - `new Promise(...)` or raw expression → send as-is (expression)
289
- */
290
- function wrapForEval(js: string): string {
291
- const code = js.trim();
292
- if (!code) return 'undefined';
293
-
294
- // Already an IIFE: `(async () => { ... })()` or `(function() {...})()`
295
- if (/^\([\s\S]*\)\s*\(.*\)\s*$/.test(code)) return code;
296
-
297
- // Arrow function: `() => ...` or `async () => ...`
298
- if (/^(async\s+)?(\([^)]*\)|[A-Za-z_]\w*)\s*=>/.test(code)) return `(${code})()`;
299
-
300
- // Function declaration: `function ...` or `async function ...`
301
- if (/^(async\s+)?function[\s(]/.test(code)) return `(${code})()`;
302
-
303
- // Everything else: bare expression, `new Promise(...)`, etc. → evaluate directly
304
- return code;
305
- }
289
+ // (End of file)
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Utility functions for browser operations
3
+ */
4
+
5
+ /**
6
+ * Wrap JS code for CDP Runtime.evaluate:
7
+ * - Already an IIFE `(...)()` → send as-is
8
+ * - Arrow/function literal → wrap as IIFE `(code)()`
9
+ * - `new Promise(...)` or raw expression → send as-is (expression)
10
+ */
11
+ export function wrapForEval(js: string): string {
12
+ if (typeof js !== 'string') return 'undefined';
13
+ const code = js.trim();
14
+ if (!code) return 'undefined';
15
+
16
+ // Already an IIFE: `(async () => { ... })()` or `(function() {...})()`
17
+ if (/^\([\s\S]*\)\s*\(.*\)\s*$/.test(code)) return code;
18
+
19
+ // Arrow function: `() => ...` or `async () => ...`
20
+ if (/^(async\s+)?(\([^)]*\)|[A-Za-z_]\w*)\s*=>/.test(code)) return `(${code})()`;
21
+
22
+ // Function declaration: `function ...` or `async function ...`
23
+ if (/^(async\s+)?function[\s(]/.test(code)) return `(${code})()`;
24
+
25
+ // Everything else: bare expression, `new Promise(...)`, etc. → evaluate directly
26
+ return code;
27
+ }
@@ -1,5 +1,5 @@
1
1
  import { afterEach, describe, it, expect, vi } from 'vitest';
2
- import { PlaywrightMCP, __test__ } from './browser/index.js';
2
+ import { BrowserBridge, __test__ } from './browser/index.js';
3
3
 
4
4
  describe('browser helpers', () => {
5
5
  it('extracts tab entries from string snapshots', () => {
@@ -43,11 +43,57 @@ describe('browser helpers', () => {
43
43
  it('times out slow promises', async () => {
44
44
  await expect(__test__.withTimeoutMs(new Promise(() => {}), 10, 'timeout')).rejects.toThrow('timeout');
45
45
  });
46
+
47
+ it('prefers the real Electron app target over DevTools and blank pages', () => {
48
+ const target = __test__.selectCDPTarget([
49
+ {
50
+ type: 'page',
51
+ title: 'DevTools - localhost:9224',
52
+ url: 'devtools://devtools/bundled/inspector.html',
53
+ webSocketDebuggerUrl: 'ws://127.0.0.1:9224/devtools',
54
+ },
55
+ {
56
+ type: 'page',
57
+ title: '',
58
+ url: 'about:blank',
59
+ webSocketDebuggerUrl: 'ws://127.0.0.1:9224/blank',
60
+ },
61
+ {
62
+ type: 'app',
63
+ title: 'Antigravity',
64
+ url: 'http://localhost:3000/',
65
+ webSocketDebuggerUrl: 'ws://127.0.0.1:9224/app',
66
+ },
67
+ ]);
68
+
69
+ expect(target?.webSocketDebuggerUrl).toBe('ws://127.0.0.1:9224/app');
70
+ });
71
+
72
+ it('honors OPENCLI_CDP_TARGET when multiple inspectable targets exist', () => {
73
+ vi.stubEnv('OPENCLI_CDP_TARGET', 'codex');
74
+
75
+ const target = __test__.selectCDPTarget([
76
+ {
77
+ type: 'app',
78
+ title: 'Cursor',
79
+ url: 'http://localhost:3000/cursor',
80
+ webSocketDebuggerUrl: 'ws://127.0.0.1:9226/cursor',
81
+ },
82
+ {
83
+ type: 'app',
84
+ title: 'OpenAI Codex',
85
+ url: 'http://localhost:3000/codex',
86
+ webSocketDebuggerUrl: 'ws://127.0.0.1:9226/codex',
87
+ },
88
+ ]);
89
+
90
+ expect(target?.webSocketDebuggerUrl).toBe('ws://127.0.0.1:9226/codex');
91
+ });
46
92
  });
47
93
 
48
- describe('PlaywrightMCP state', () => {
94
+ describe('BrowserBridge state', () => {
49
95
  it('transitions to closed after close()', async () => {
50
- const mcp = new PlaywrightMCP();
96
+ const mcp = new BrowserBridge();
51
97
 
52
98
  expect(mcp.state).toBe('idle');
53
99
 
@@ -57,21 +103,21 @@ describe('PlaywrightMCP state', () => {
57
103
  });
58
104
 
59
105
  it('rejects connect() after the session has been closed', async () => {
60
- const mcp = new PlaywrightMCP();
106
+ const mcp = new BrowserBridge();
61
107
  await mcp.close();
62
108
 
63
109
  await expect(mcp.connect()).rejects.toThrow('Session is closed');
64
110
  });
65
111
 
66
112
  it('rejects connect() while already connecting', async () => {
67
- const mcp = new PlaywrightMCP();
113
+ const mcp = new BrowserBridge();
68
114
  (mcp as any)._state = 'connecting';
69
115
 
70
116
  await expect(mcp.connect()).rejects.toThrow('Already connecting');
71
117
  });
72
118
 
73
119
  it('rejects connect() while closing', async () => {
74
- const mcp = new PlaywrightMCP();
120
+ const mcp = new BrowserBridge();
75
121
  (mcp as any)._state = 'closing';
76
122
 
77
123
  await expect(mcp.connect()).rejects.toThrow('Session is closing');
@@ -0,0 +1,45 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { formatTimestamp, workStatusLabel } from './chaoxing.js';
3
+
4
+ describe('formatTimestamp', () => {
5
+ it('formats millisecond timestamp', () => {
6
+ // 2026-01-15 08:30 UTC+8
7
+ const ts = new Date('2026-01-15T00:30:00Z').getTime();
8
+ const result = formatTimestamp(ts);
9
+ expect(result).toMatch(/2026-01-15/);
10
+ });
11
+
12
+ it('formats second timestamp', () => {
13
+ const ts = Math.floor(new Date('2026-06-01T12:00:00Z').getTime() / 1000);
14
+ const result = formatTimestamp(ts);
15
+ expect(result).toMatch(/2026-06-01/);
16
+ });
17
+
18
+ it('returns empty for null/undefined/0', () => {
19
+ expect(formatTimestamp(null)).toBe('');
20
+ expect(formatTimestamp(undefined)).toBe('');
21
+ expect(formatTimestamp(0)).toBe('');
22
+ expect(formatTimestamp('')).toBe('');
23
+ });
24
+
25
+ it('passes through readable date strings', () => {
26
+ expect(formatTimestamp('2026-03-20 23:59')).toBe('2026-03-20 23:59');
27
+ });
28
+ });
29
+
30
+ describe('workStatusLabel', () => {
31
+ it('maps numeric status codes', () => {
32
+ expect(workStatusLabel(0)).toBe('未交');
33
+ expect(workStatusLabel(1)).toBe('已交');
34
+ expect(workStatusLabel(2)).toBe('已批阅');
35
+ });
36
+
37
+ it('passes through string status', () => {
38
+ expect(workStatusLabel('已交')).toBe('已交');
39
+ });
40
+
41
+ it('returns 未知 for empty/null', () => {
42
+ expect(workStatusLabel(null)).toBe('未知');
43
+ expect(workStatusLabel('')).toBe('未知');
44
+ });
45
+ });
@@ -0,0 +1,268 @@
1
+ /**
2
+ * Chaoxing (学习通) shared helpers.
3
+ *
4
+ * Flow: initSession → getCourses → enterCourse → getTabIframeUrl → navigate → parse DOM
5
+ * Chaoxing has no flat "list all assignments" API; data is behind session-gated
6
+ * course pages loaded as iframes.
7
+ */
8
+
9
+ import type { IPage } from './types.js';
10
+
11
+ // ── Utilities ────────────────────────────────────────────────────────
12
+
13
+ /** Sleep for given milliseconds (anti-scraping delay). */
14
+ export function sleep(ms: number): Promise<void> {
15
+ return new Promise(resolve => setTimeout(resolve, ms));
16
+ }
17
+
18
+ /** Execute a credentialed fetch in the browser context, returning JSON or text. */
19
+ export async function fetchChaoxing(page: IPage, url: string): Promise<any> {
20
+ const urlJs = JSON.stringify(url);
21
+ return page.evaluate(`
22
+ async () => {
23
+ const res = await fetch(${urlJs}, { credentials: "include" });
24
+ const text = await res.text();
25
+ try { return JSON.parse(text); } catch {}
26
+ return text;
27
+ }
28
+ `);
29
+ }
30
+
31
+ /** Format a timestamp (seconds or milliseconds or date string) to YYYY-MM-DD HH:mm. */
32
+ export function formatTimestamp(ts: unknown): string {
33
+ if (ts == null || ts === '' || ts === 0) return '';
34
+ if (typeof ts === 'string' && !/^\d+$/.test(ts.trim())) return ts.trim();
35
+ const num = Number(ts);
36
+ if (Number.isNaN(num) || num <= 0) return String(ts);
37
+ const millis = num > 1e12 ? num : num * 1000;
38
+ const d = new Date(millis);
39
+ if (Number.isNaN(d.getTime())) return String(ts);
40
+ const yyyy = d.getFullYear();
41
+ const mm = String(d.getMonth() + 1).padStart(2, '0');
42
+ const dd = String(d.getDate()).padStart(2, '0');
43
+ const hh = String(d.getHours()).padStart(2, '0');
44
+ const mi = String(d.getMinutes()).padStart(2, '0');
45
+ return `${yyyy}-${mm}-${dd} ${hh}:${mi}`;
46
+ }
47
+
48
+ /** Map numeric work status to Chinese label. */
49
+ export function workStatusLabel(status: unknown): string {
50
+ if (status == null || status === '') return '未知';
51
+ const s = Number(status);
52
+ if (s === 0) return '未交';
53
+ if (s === 1) return '已交';
54
+ if (s === 2) return '已批阅';
55
+ const str = String(status).trim();
56
+ return str || '未知';
57
+ }
58
+
59
+ // ── Course list ──────────────────────────────────────────────────────
60
+
61
+ export interface ChaoxingCourse {
62
+ courseId: string;
63
+ classId: string;
64
+ cpi: string;
65
+ title: string;
66
+ }
67
+
68
+ /** Fetch enrolled course list via backclazzdata JSON API. */
69
+ export async function getCourses(page: IPage): Promise<ChaoxingCourse[]> {
70
+ const resp = await fetchChaoxing(
71
+ page,
72
+ 'https://mooc1-api.chaoxing.com/mycourse/backclazzdata?view=json&rss=1',
73
+ );
74
+ if (!resp || typeof resp !== 'object') return [];
75
+ const channelList: any[] = resp.channelList ?? [];
76
+ const courses: ChaoxingCourse[] = [];
77
+ for (const channel of channelList) {
78
+ const content = channel?.content;
79
+ if (!content) continue;
80
+ const courseData = content.course?.data;
81
+ if (!Array.isArray(courseData)) continue;
82
+ for (const c of courseData) {
83
+ courses.push({
84
+ courseId: String(c.id ?? ''),
85
+ classId: String(content.id ?? ''),
86
+ cpi: String(channel.cpi ?? ''),
87
+ title: String(c.name ?? ''),
88
+ });
89
+ }
90
+ }
91
+ return courses;
92
+ }
93
+
94
+ // ── Session & course entry ───────────────────────────────────────────
95
+
96
+ /** Navigate to the interaction page to establish a Chaoxing session. */
97
+ export async function initSession(page: IPage): Promise<void> {
98
+ await page.goto('https://mooc2-ans.chaoxing.com/mooc2-ans/visit/interaction');
99
+ await page.wait(3);
100
+ }
101
+
102
+ /**
103
+ * Enter a course via stucoursemiddle redirect (establishes course session + enc).
104
+ * After this call the browser is on the course page.
105
+ */
106
+ export async function enterCourse(page: IPage, course: ChaoxingCourse): Promise<void> {
107
+ const url =
108
+ `https://mooc1.chaoxing.com/visit/stucoursemiddle` +
109
+ `?courseid=${course.courseId}&clazzid=${course.classId}&cpi=${course.cpi}&ismooc2=1&v=2`;
110
+ await page.goto(url);
111
+ await page.wait(3);
112
+ }
113
+
114
+ /**
115
+ * On the course page, click a tab (作业 / 考试) and return the iframe src
116
+ * that gets loaded. Returns empty string if the tab is not found.
117
+ */
118
+ export async function getTabIframeUrl(page: IPage, tabName: string): Promise<string> {
119
+ const nameJs = JSON.stringify(tabName);
120
+ const result: any = await page.evaluate(`
121
+ async () => {
122
+ const tabs = document.querySelectorAll('a[data-url]');
123
+ let target = null;
124
+ for (const tab of tabs) {
125
+ if ((tab.innerText || '').trim() === ${nameJs}) { target = tab; break; }
126
+ }
127
+ if (!target) return '';
128
+ target.click();
129
+ await new Promise(r => setTimeout(r, 2000));
130
+ const iframe = document.getElementById('frame_content-hd') || document.querySelector('iframe');
131
+ return iframe?.src || '';
132
+ }
133
+ `);
134
+ return typeof result === 'string' ? result : '';
135
+ }
136
+
137
+ // ── Assignment parsing ───────────────────────────────────────────────
138
+
139
+ export interface AssignmentRow {
140
+ course: string;
141
+ title: string;
142
+ deadline: string;
143
+ status: string;
144
+ score: string;
145
+ }
146
+
147
+ /**
148
+ * Parse assignments from the current page DOM (the 作业列表 page).
149
+ * The page uses `.ulDiv li` items with status/deadline/score info.
150
+ */
151
+ export async function parseAssignmentsFromDom(page: IPage, courseName: string): Promise<AssignmentRow[]> {
152
+ const raw: any[] = await page.evaluate(`
153
+ (() => {
154
+ const items = [];
155
+ // Each assignment is a li or div block; try multiple selectors
156
+ const blocks = document.querySelectorAll('.ulDiv li, .work-list-item, .listContent > div, ul > li');
157
+ for (const block of blocks) {
158
+ const text = (block.innerText || '').trim();
159
+ if (!text || text.length < 3) continue;
160
+ // Skip filter buttons and headers
161
+ if (/^(全部|已完成|未完成|筛选)$/.test(text)) continue;
162
+ items.push(text);
163
+ }
164
+ // Fallback: split body text by common patterns
165
+ if (items.length === 0) {
166
+ const body = (document.body?.innerText || '').trim();
167
+ return [body];
168
+ }
169
+ return items;
170
+ })()
171
+ `) ?? [];
172
+
173
+ const rows: AssignmentRow[] = [];
174
+ for (const text of raw) {
175
+ if (typeof text !== 'string' || text.length < 3) continue;
176
+ // Skip noise
177
+ if (/^(全部|已完成|未完成|筛选|暂无|提交的作业将经过)/.test(text)) continue;
178
+
179
+ const lines = text.split('\n').map((l: string) => l.trim()).filter(Boolean);
180
+ if (!lines.length) continue;
181
+
182
+ // First meaningful line is the title
183
+ const title = lines[0].replace(/\s+/g, ' ').trim();
184
+ if (!title || /^(全部|已完成|未完成|筛选)$/.test(title)) continue;
185
+
186
+ // Extract status: 未交 / 待批阅 / 已完成 / 已批阅
187
+ const statusMatch = text.match(/(未交|待批阅|已完成|已批阅)/);
188
+ const status = statusMatch?.[1] ?? '';
189
+
190
+ // Extract deadline: "剩余XXX" or date pattern
191
+ const remainMatch = text.match(/(剩余[\d天小时分钟秒]+)/);
192
+ const dateMatch = text.match(/(\d{4}[-/.]\d{1,2}[-/.]\d{1,2}(?:\s+\d{1,2}:\d{2})?)/);
193
+ const deadline = remainMatch?.[1] ?? dateMatch?.[1] ?? '';
194
+
195
+ // Extract score (exclude "分钟")
196
+ const scoreMatch = text.match(/(\d+(?:\.\d+)?)\s*分(?!钟)/);
197
+ const score = scoreMatch?.[1] ?? '';
198
+
199
+ rows.push({ course: courseName, title, deadline, status, score });
200
+ }
201
+ return rows;
202
+ }
203
+
204
+ // ── Exam parsing ─────────────────────────────────────────────────────
205
+
206
+ export interface ExamRow {
207
+ course: string;
208
+ title: string;
209
+ start: string;
210
+ end: string;
211
+ status: string;
212
+ score: string;
213
+ }
214
+
215
+ /** Parse exams from the current page DOM (the 考试列表 page). */
216
+ export async function parseExamsFromDom(page: IPage, courseName: string): Promise<ExamRow[]> {
217
+ const raw: any[] = await page.evaluate(`
218
+ (() => {
219
+ const items = [];
220
+ const blocks = document.querySelectorAll('.ulDiv li, .exam-list-item, .listContent > div, ul > li');
221
+ for (const block of blocks) {
222
+ const text = (block.innerText || '').trim();
223
+ if (!text || text.length < 3) continue;
224
+ if (/^(全部|已完成|未完成|筛选|暂无)$/.test(text)) continue;
225
+ items.push(text);
226
+ }
227
+ if (items.length === 0) {
228
+ const body = (document.body?.innerText || '').trim();
229
+ return [body];
230
+ }
231
+ return items;
232
+ })()
233
+ `) ?? [];
234
+
235
+ // Check for "暂无考试"
236
+ if (raw.length === 1 && typeof raw[0] === 'string' && raw[0].includes('暂无考试')) {
237
+ return [];
238
+ }
239
+
240
+ const rows: ExamRow[] = [];
241
+ for (const text of raw) {
242
+ if (typeof text !== 'string' || text.length < 3) continue;
243
+ if (/^(全部|已完成|未完成|筛选|暂无)/.test(text)) continue;
244
+
245
+ const lines = text.split('\n').map((l: string) => l.trim()).filter(Boolean);
246
+ if (!lines.length) continue;
247
+
248
+ const title = lines[0].replace(/\s+/g, ' ').trim();
249
+ if (!title || /^(全部|已完成|未完成|筛选)$/.test(title)) continue;
250
+
251
+ // Extract dates
252
+ const dates = text.match(/\d{4}[-/.]\d{1,2}[-/.]\d{1,2}\s+\d{1,2}:\d{2}/g) ?? [];
253
+ const start = dates[0] ?? '';
254
+ const end = dates[1] ?? '';
255
+
256
+ // Status
257
+ const statusMatch = text.match(/(未开始|进行中|已结束|已完成|未交|待批阅)/);
258
+ let status = statusMatch?.[1] ?? '';
259
+ if (!status && text.includes('剩余')) status = '进行中';
260
+
261
+ // Score (exclude "分钟")
262
+ const scoreMatch = text.match(/(\d+(?:\.\d+)?)\s*分(?!钟)/);
263
+ const score = scoreMatch?.[1] ?? '';
264
+
265
+ rows.push({ course: courseName, title, start, end, status, score });
266
+ }
267
+ return rows;
268
+ }