@apmantza/greedysearch-pi 1.7.0 → 1.7.2

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.
@@ -1,167 +1,167 @@
1
- #!/usr/bin/env node
2
-
3
- // extractors/bing-copilot.mjs
4
- // Navigate copilot.microsoft.com, wait for answer to complete, return clean answer + sources.
5
- //
6
- // Usage:
7
- // node extractors/bing-copilot.mjs "<query>" [--tab <prefix>]
8
- //
9
- // Output (stdout): JSON { answer, sources, query, url }
10
- // Errors go to stderr only — stdout is always clean JSON for piping.
11
-
12
- import {
13
- cdp,
14
- formatAnswer,
15
- getOrOpenTab,
16
- handleError,
17
- injectClipboardInterceptor,
18
- outputJson,
19
- parseArgs,
20
- parseSourcesFromMarkdown,
21
- validateQuery,
22
- } from "./common.mjs";
23
- import { dismissConsent, handleVerification } from "./consent.mjs";
24
- import { SELECTORS } from "./selectors.mjs";
25
-
26
- const S = SELECTORS.bing;
27
- const GLOBAL_VAR = "__bingClipboard";
28
-
29
- // ============================================================================
30
- // Bing Copilot-specific helpers
31
- // ============================================================================
32
-
33
- async function waitForCopyButton(tab, timeout = 60000) {
34
- const deadline = Date.now() + timeout;
35
- while (Date.now() < deadline) {
36
- await new Promise((r) => setTimeout(r, 700));
37
- const found = await cdp([
38
- "eval",
39
- tab,
40
- `!!document.querySelector('${S.copyButton}')`,
41
- ]).catch(() => "false");
42
- if (found === "true") return;
43
- }
44
- throw new Error(`Copilot copy button did not appear within ${timeout}ms`);
45
- }
46
-
47
- async function extractAnswer(tab) {
48
- await cdp([
49
- "eval",
50
- tab,
51
- `document.querySelector('${S.copyButton}')?.click()`,
52
- ]);
53
- await new Promise((r) => setTimeout(r, 400));
54
-
55
- const answer = await cdp(["eval", tab, `window.${GLOBAL_VAR} || ''`]);
56
- if (!answer) throw new Error("Clipboard interceptor returned empty text");
57
-
58
- const sources = parseSourcesFromMarkdown(answer);
59
- return { answer: answer.trim(), sources };
60
- }
61
-
62
- // ============================================================================
63
- // Main
64
- // ============================================================================
65
-
66
- const USAGE =
67
- 'Usage: node extractors/bing-copilot.mjs "<query>" [--tab <prefix>]\n';
68
-
69
- async function main() {
70
- const args = process.argv.slice(2);
71
- validateQuery(args, USAGE);
72
-
73
- const { query, tabPrefix, short } = parseArgs(args);
74
-
75
- try {
76
- await cdp(["list"]);
77
- const tab = await getOrOpenTab(tabPrefix);
78
-
79
- // Navigate to Copilot homepage and use the chat input
80
- await cdp(["nav", tab, "https://copilot.microsoft.com/"], 35000);
81
- await new Promise((r) => setTimeout(r, 2000));
82
- await dismissConsent(tab, cdp);
83
-
84
- // Handle verification challenges (Cloudflare Turnstile, Microsoft auth, etc.)
85
- const verifyResult = await handleVerification(tab, cdp, 90000);
86
- if (verifyResult === "needs-human") {
87
- throw new Error(
88
- "Copilot verification required — please solve it manually in the browser window",
89
- );
90
- }
91
-
92
- // After verification, page may have redirected or reloaded — wait for it to settle
93
- if (verifyResult === "clicked") {
94
- await new Promise((r) => setTimeout(r, 3000));
95
-
96
- // Re-navigate if we got redirected
97
- const currentUrl = await cdp([
98
- "eval",
99
- tab,
100
- "document.location.href",
101
- ]).catch(() => "");
102
- if (!currentUrl.includes("copilot.microsoft.com")) {
103
- await cdp(["nav", tab, "https://copilot.microsoft.com/"], 35000);
104
- await new Promise((r) => setTimeout(r, 2000));
105
- await dismissConsent(tab, cdp);
106
- }
107
- }
108
-
109
- // Wait for React app to mount input (up to 15s, longer after verification)
110
- const inputDeadline = Date.now() + 15000;
111
- while (Date.now() < inputDeadline) {
112
- const found = await cdp([
113
- "eval",
114
- tab,
115
- `!!document.querySelector('${S.input}')`,
116
- ]).catch(() => "false");
117
- if (found === "true") break;
118
- await new Promise((r) => setTimeout(r, 500));
119
- }
120
- await new Promise((r) => setTimeout(r, 300));
121
-
122
- // Verify input is actually there before proceeding
123
- const inputReady = await cdp([
124
- "eval",
125
- tab,
126
- `!!document.querySelector('${S.input}')`,
127
- ]).catch(() => "false");
128
- if (inputReady !== "true") {
129
- throw new Error(
130
- "Copilot input not found — verification may have failed or page is in unexpected state",
131
- );
132
- }
133
-
134
- await injectClipboardInterceptor(tab, GLOBAL_VAR);
135
- await cdp(["click", tab, S.input]);
136
- await new Promise((r) => setTimeout(r, 400));
137
- await cdp(["type", tab, query]);
138
- await new Promise((r) => setTimeout(r, 400));
139
-
140
- // Submit with Enter (most reliable across locales and Chrome instances)
141
- await cdp([
142
- "eval",
143
- tab,
144
- `document.querySelector('${S.input}')?.dispatchEvent(new KeyboardEvent('keydown',{key:'Enter',bubbles:true,keyCode:13})), 'ok'`,
145
- ]);
146
-
147
- await waitForCopyButton(tab);
148
-
149
- const { answer, sources } = await extractAnswer(tab);
150
- if (!answer)
151
- throw new Error("No answer extracted — Copilot may not have responded");
152
-
153
- const finalUrl = await cdp(["eval", tab, "document.location.href"]).catch(
154
- () => "",
155
- );
156
- outputJson({
157
- query,
158
- url: finalUrl,
159
- answer: formatAnswer(answer, short),
160
- sources,
161
- });
162
- } catch (e) {
163
- handleError(e);
164
- }
165
- }
166
-
167
- main();
1
+ #!/usr/bin/env node
2
+
3
+ // extractors/bing-copilot.mjs
4
+ // Navigate copilot.microsoft.com, wait for answer to complete, return clean answer + sources.
5
+ //
6
+ // Usage:
7
+ // node extractors/bing-copilot.mjs "<query>" [--tab <prefix>]
8
+ //
9
+ // Output (stdout): JSON { answer, sources, query, url }
10
+ // Errors go to stderr only — stdout is always clean JSON for piping.
11
+
12
+ import {
13
+ cdp,
14
+ formatAnswer,
15
+ getOrOpenTab,
16
+ handleError,
17
+ injectClipboardInterceptor,
18
+ outputJson,
19
+ parseArgs,
20
+ parseSourcesFromMarkdown,
21
+ validateQuery,
22
+ } from "./common.mjs";
23
+ import { dismissConsent, handleVerification } from "./consent.mjs";
24
+ import { SELECTORS } from "./selectors.mjs";
25
+
26
+ const S = SELECTORS.bing;
27
+ const GLOBAL_VAR = "__bingClipboard";
28
+
29
+ // ============================================================================
30
+ // Bing Copilot-specific helpers
31
+ // ============================================================================
32
+
33
+ async function waitForCopyButton(tab, timeout = 60000) {
34
+ const deadline = Date.now() + timeout;
35
+ while (Date.now() < deadline) {
36
+ await new Promise((r) => setTimeout(r, 700));
37
+ const found = await cdp([
38
+ "eval",
39
+ tab,
40
+ `!!document.querySelector('${S.copyButton}')`,
41
+ ]).catch(() => "false");
42
+ if (found === "true") return;
43
+ }
44
+ throw new Error(`Copilot copy button did not appear within ${timeout}ms`);
45
+ }
46
+
47
+ async function extractAnswer(tab) {
48
+ await cdp([
49
+ "eval",
50
+ tab,
51
+ `document.querySelector('${S.copyButton}')?.click()`,
52
+ ]);
53
+ await new Promise((r) => setTimeout(r, 400));
54
+
55
+ const answer = await cdp(["eval", tab, `window.${GLOBAL_VAR} || ''`]);
56
+ if (!answer) throw new Error("Clipboard interceptor returned empty text");
57
+
58
+ const sources = parseSourcesFromMarkdown(answer);
59
+ return { answer: answer.trim(), sources };
60
+ }
61
+
62
+ // ============================================================================
63
+ // Main
64
+ // ============================================================================
65
+
66
+ const USAGE =
67
+ 'Usage: node extractors/bing-copilot.mjs "<query>" [--tab <prefix>]\n';
68
+
69
+ async function main() {
70
+ const args = process.argv.slice(2);
71
+ validateQuery(args, USAGE);
72
+
73
+ const { query, tabPrefix, short } = parseArgs(args);
74
+
75
+ try {
76
+ await cdp(["list"]);
77
+ const tab = await getOrOpenTab(tabPrefix);
78
+
79
+ // Navigate to Copilot homepage and use the chat input
80
+ await cdp(["nav", tab, "https://copilot.microsoft.com/"], 35000);
81
+ await new Promise((r) => setTimeout(r, 2000));
82
+ await dismissConsent(tab, cdp);
83
+
84
+ // Handle verification challenges (Cloudflare Turnstile, Microsoft auth, etc.)
85
+ const verifyResult = await handleVerification(tab, cdp, 90000);
86
+ if (verifyResult === "needs-human") {
87
+ throw new Error(
88
+ "Copilot verification required — please solve it manually in the browser window",
89
+ );
90
+ }
91
+
92
+ // After verification, page may have redirected or reloaded — wait for it to settle
93
+ if (verifyResult === "clicked") {
94
+ await new Promise((r) => setTimeout(r, 3000));
95
+
96
+ // Re-navigate if we got redirected
97
+ const currentUrl = await cdp([
98
+ "eval",
99
+ tab,
100
+ "document.location.href",
101
+ ]).catch(() => "");
102
+ if (!currentUrl.includes("copilot.microsoft.com")) {
103
+ await cdp(["nav", tab, "https://copilot.microsoft.com/"], 35000);
104
+ await new Promise((r) => setTimeout(r, 2000));
105
+ await dismissConsent(tab, cdp);
106
+ }
107
+ }
108
+
109
+ // Wait for React app to mount input (up to 15s, longer after verification)
110
+ const inputDeadline = Date.now() + 15000;
111
+ while (Date.now() < inputDeadline) {
112
+ const found = await cdp([
113
+ "eval",
114
+ tab,
115
+ `!!document.querySelector('${S.input}')`,
116
+ ]).catch(() => "false");
117
+ if (found === "true") break;
118
+ await new Promise((r) => setTimeout(r, 500));
119
+ }
120
+ await new Promise((r) => setTimeout(r, 300));
121
+
122
+ // Verify input is actually there before proceeding
123
+ const inputReady = await cdp([
124
+ "eval",
125
+ tab,
126
+ `!!document.querySelector('${S.input}')`,
127
+ ]).catch(() => "false");
128
+ if (inputReady !== "true") {
129
+ throw new Error(
130
+ "Copilot input not found — verification may have failed or page is in unexpected state",
131
+ );
132
+ }
133
+
134
+ await injectClipboardInterceptor(tab, GLOBAL_VAR);
135
+ await cdp(["click", tab, S.input]);
136
+ await new Promise((r) => setTimeout(r, 400));
137
+ await cdp(["type", tab, query]);
138
+ await new Promise((r) => setTimeout(r, 400));
139
+
140
+ // Submit with Enter (most reliable across locales and Chrome instances)
141
+ await cdp([
142
+ "eval",
143
+ tab,
144
+ `document.querySelector('${S.input}')?.dispatchEvent(new KeyboardEvent('keydown',{key:'Enter',bubbles:true,keyCode:13})), 'ok'`,
145
+ ]);
146
+
147
+ await waitForCopyButton(tab);
148
+
149
+ const { answer, sources } = await extractAnswer(tab);
150
+ if (!answer)
151
+ throw new Error("No answer extracted — Copilot may not have responded");
152
+
153
+ const finalUrl = await cdp(["eval", tab, "document.location.href"]).catch(
154
+ () => "",
155
+ );
156
+ outputJson({
157
+ query,
158
+ url: finalUrl,
159
+ answer: formatAnswer(answer, short),
160
+ sources,
161
+ });
162
+ } catch (e) {
163
+ handleError(e);
164
+ }
165
+ }
166
+
167
+ main();