@detiq/crawler 0.1.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.
- package/Dockerfile +49 -0
- package/README.md +11 -0
- package/dist/accessibility-auditor.d.ts +29 -0
- package/dist/accessibility-auditor.js +57 -0
- package/dist/accessibility-auditor.js.map +7 -0
- package/dist/ai-login.d.ts +32 -0
- package/dist/ai-login.js +177 -0
- package/dist/ai-login.js.map +7 -0
- package/dist/auth-token-rotator.d.ts +47 -0
- package/dist/auth-token-rotator.js +58 -0
- package/dist/auth-token-rotator.js.map +7 -0
- package/dist/auth.d.ts +20 -0
- package/dist/auth.js +329 -0
- package/dist/auth.js.map +7 -0
- package/dist/browser-extension.d.ts +37 -0
- package/dist/browser-extension.js +113 -0
- package/dist/browser-extension.js.map +7 -0
- package/dist/captcha-solver.d.ts +16 -0
- package/dist/captcha-solver.js +105 -0
- package/dist/captcha-solver.js.map +7 -0
- package/dist/color-contrast.d.ts +30 -0
- package/dist/color-contrast.js +78 -0
- package/dist/color-contrast.js.map +7 -0
- package/dist/content-chunker.d.ts +18 -0
- package/dist/content-chunker.js +152 -0
- package/dist/content-chunker.js.map +7 -0
- package/dist/content-dedup.d.ts +23 -0
- package/dist/content-dedup.js +60 -0
- package/dist/content-dedup.js.map +7 -0
- package/dist/cookie-consent.d.ts +25 -0
- package/dist/cookie-consent.js +78 -0
- package/dist/cookie-consent.js.map +7 -0
- package/dist/cookie-jar.d.ts +20 -0
- package/dist/cookie-jar.js +61 -0
- package/dist/cookie-jar.js.map +7 -0
- package/dist/crawler.d.ts +179 -0
- package/dist/crawler.js +3672 -0
- package/dist/crawler.js.map +7 -0
- package/dist/custom-js-injector.d.ts +42 -0
- package/dist/custom-js-injector.js +50 -0
- package/dist/custom-js-injector.js.map +7 -0
- package/dist/dark-mode-capture.d.ts +23 -0
- package/dist/dark-mode-capture.js +75 -0
- package/dist/dark-mode-capture.js.map +7 -0
- package/dist/dead-link-detector.d.ts +37 -0
- package/dist/dead-link-detector.js +50 -0
- package/dist/dead-link-detector.js.map +7 -0
- package/dist/element-hash.d.ts +9 -0
- package/dist/element-hash.js +9 -0
- package/dist/element-hash.js.map +7 -0
- package/dist/email-otp.d.ts +42 -0
- package/dist/email-otp.js +177 -0
- package/dist/email-otp.js.map +7 -0
- package/dist/engine-router.d.ts +47 -0
- package/dist/engine-router.js +94 -0
- package/dist/engine-router.js.map +7 -0
- package/dist/export/bulk-export.d.ts +26 -0
- package/dist/export/bulk-export.js +76 -0
- package/dist/export/bulk-export.js.map +7 -0
- package/dist/export/vector-export.d.ts +76 -0
- package/dist/export/vector-export.js +85 -0
- package/dist/export/vector-export.js.map +7 -0
- package/dist/extractors/advanced.d.ts +54 -0
- package/dist/extractors/advanced.js +245 -0
- package/dist/extractors/advanced.js.map +7 -0
- package/dist/extractors/artifact-envelope.d.ts +63 -0
- package/dist/extractors/artifact-envelope.js +63 -0
- package/dist/extractors/artifact-envelope.js.map +7 -0
- package/dist/failure-analysis-agent.d.ts +16 -0
- package/dist/failure-analysis-agent.js +40 -0
- package/dist/failure-analysis-agent.js.map +7 -0
- package/dist/feature-flag-detector.d.ts +30 -0
- package/dist/feature-flag-detector.js +55 -0
- package/dist/feature-flag-detector.js.map +7 -0
- package/dist/form-detector.d.ts +34 -0
- package/dist/form-detector.js +58 -0
- package/dist/form-detector.js.map +7 -0
- package/dist/frontier.d.ts +32 -0
- package/dist/frontier.js +83 -0
- package/dist/frontier.js.map +7 -0
- package/dist/gap-pass.d.ts +35 -0
- package/dist/gap-pass.js +81 -0
- package/dist/gap-pass.js.map +7 -0
- package/dist/graphql-detector.d.ts +39 -0
- package/dist/graphql-detector.js +86 -0
- package/dist/graphql-detector.js.map +7 -0
- package/dist/http-engine.d.ts +80 -0
- package/dist/http-engine.js +304 -0
- package/dist/http-engine.js.map +7 -0
- package/dist/incremental-diff.d.ts +59 -0
- package/dist/incremental-diff.js +61 -0
- package/dist/incremental-diff.js.map +7 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +61 -0
- package/dist/index.js.map +7 -0
- package/dist/input-fuzzer.d.ts +30 -0
- package/dist/input-fuzzer.js +105 -0
- package/dist/input-fuzzer.js.map +7 -0
- package/dist/interactive-probe.d.ts +104 -0
- package/dist/interactive-probe.js +442 -0
- package/dist/interactive-probe.js.map +7 -0
- package/dist/job-queue.d.ts +171 -0
- package/dist/job-queue.js +2256 -0
- package/dist/job-queue.js.map +7 -0
- package/dist/js-coverage.d.ts +29 -0
- package/dist/js-coverage.js +56 -0
- package/dist/js-coverage.js.map +7 -0
- package/dist/js-router-detector.d.ts +17 -0
- package/dist/js-router-detector.js +90 -0
- package/dist/js-router-detector.js.map +7 -0
- package/dist/language-detector.d.ts +22 -0
- package/dist/language-detector.js +57 -0
- package/dist/language-detector.js.map +7 -0
- package/dist/link-classifier.d.ts +23 -0
- package/dist/link-classifier.js +40 -0
- package/dist/link-classifier.js.map +7 -0
- package/dist/live-view.d.ts +36 -0
- package/dist/live-view.js +100 -0
- package/dist/live-view.js.map +7 -0
- package/dist/logger.d.ts +2 -0
- package/dist/logger.js +10 -0
- package/dist/logger.js.map +7 -0
- package/dist/markdown-extract.d.ts +8 -0
- package/dist/markdown-extract.js +54 -0
- package/dist/markdown-extract.js.map +7 -0
- package/dist/native-crawler.d.ts +69 -0
- package/dist/native-crawler.js +839 -0
- package/dist/native-crawler.js.map +7 -0
- package/dist/network-observer.d.ts +49 -0
- package/dist/network-observer.js +149 -0
- package/dist/network-observer.js.map +7 -0
- package/dist/network-throttle.d.ts +25 -0
- package/dist/network-throttle.js +34 -0
- package/dist/network-throttle.js.map +7 -0
- package/dist/openapi-generator.d.ts +49 -0
- package/dist/openapi-generator.js +89 -0
- package/dist/openapi-generator.js.map +7 -0
- package/dist/pdf-extractor.d.ts +28 -0
- package/dist/pdf-extractor.js +81 -0
- package/dist/pdf-extractor.js.map +7 -0
- package/dist/performance-metrics.d.ts +53 -0
- package/dist/performance-metrics.js +83 -0
- package/dist/performance-metrics.js.map +7 -0
- package/dist/phash.d.ts +38 -0
- package/dist/phash.js +115 -0
- package/dist/phash.js.map +7 -0
- package/dist/pii-scrubber.d.ts +21 -0
- package/dist/pii-scrubber.js +75 -0
- package/dist/pii-scrubber.js.map +7 -0
- package/dist/playwright-recorder.d.ts +39 -0
- package/dist/playwright-recorder.js +123 -0
- package/dist/playwright-recorder.js.map +7 -0
- package/dist/policies/crawl-safety.d.ts +9 -0
- package/dist/policies/crawl-safety.js +37 -0
- package/dist/policies/crawl-safety.js.map +7 -0
- package/dist/profiles/domain-profile.d.ts +19 -0
- package/dist/profiles/domain-profile.js +140 -0
- package/dist/profiles/domain-profile.js.map +7 -0
- package/dist/proxy.d.ts +50 -0
- package/dist/proxy.js +165 -0
- package/dist/proxy.js.map +7 -0
- package/dist/rate-limiter.d.ts +31 -0
- package/dist/rate-limiter.js +64 -0
- package/dist/rate-limiter.js.map +7 -0
- package/dist/redirect-tracker.d.ts +37 -0
- package/dist/redirect-tracker.js +65 -0
- package/dist/redirect-tracker.js.map +7 -0
- package/dist/redis-frontier.d.ts +48 -0
- package/dist/redis-frontier.js +126 -0
- package/dist/redis-frontier.js.map +7 -0
- package/dist/redis-queue.d.ts +48 -0
- package/dist/redis-queue.js +165 -0
- package/dist/redis-queue.js.map +7 -0
- package/dist/response-cache.d.ts +38 -0
- package/dist/response-cache.js +140 -0
- package/dist/response-cache.js.map +7 -0
- package/dist/responsive-detector.d.ts +29 -0
- package/dist/responsive-detector.js +46 -0
- package/dist/responsive-detector.js.map +7 -0
- package/dist/retry-policy.d.ts +20 -0
- package/dist/retry-policy.js +54 -0
- package/dist/retry-policy.js.map +7 -0
- package/dist/robots-txt.d.ts +9 -0
- package/dist/robots-txt.js +77 -0
- package/dist/robots-txt.js.map +7 -0
- package/dist/screen-reader.d.ts +16 -0
- package/dist/screen-reader.js +46 -0
- package/dist/screen-reader.js.map +7 -0
- package/dist/screenshot-masker.d.ts +28 -0
- package/dist/screenshot-masker.js +62 -0
- package/dist/screenshot-masker.js.map +7 -0
- package/dist/seo-checker.d.ts +33 -0
- package/dist/seo-checker.js +91 -0
- package/dist/seo-checker.js.map +7 -0
- package/dist/session-capture.d.ts +35 -0
- package/dist/session-capture.js +267 -0
- package/dist/session-capture.js.map +7 -0
- package/dist/session-export.d.ts +49 -0
- package/dist/session-export.js +117 -0
- package/dist/session-export.js.map +7 -0
- package/dist/sitemap-writer.d.ts +21 -0
- package/dist/sitemap-writer.js +47 -0
- package/dist/sitemap-writer.js.map +7 -0
- package/dist/sitemap.d.ts +51 -0
- package/dist/sitemap.js +152 -0
- package/dist/sitemap.js.map +7 -0
- package/dist/skyvern.d.ts +56 -0
- package/dist/skyvern.js +129 -0
- package/dist/skyvern.js.map +7 -0
- package/dist/sqs-queue.d.ts +2 -0
- package/dist/sqs-queue.js +47 -0
- package/dist/sqs-queue.js.map +7 -0
- package/dist/sso-token.d.ts +29 -0
- package/dist/sso-token.js +76 -0
- package/dist/sso-token.js.map +7 -0
- package/dist/stealth/human-behavior.d.ts +20 -0
- package/dist/stealth/human-behavior.js +168 -0
- package/dist/stealth/human-behavior.js.map +7 -0
- package/dist/telemetry.d.ts +34 -0
- package/dist/telemetry.js +77 -0
- package/dist/telemetry.js.map +7 -0
- package/dist/test-executor.d.ts +67 -0
- package/dist/test-executor.js +311 -0
- package/dist/test-executor.js.map +7 -0
- package/dist/tls-fingerprint.d.ts +50 -0
- package/dist/tls-fingerprint.js +81 -0
- package/dist/tls-fingerprint.js.map +7 -0
- package/dist/url-pattern-completer.d.ts +28 -0
- package/dist/url-pattern-completer.js +87 -0
- package/dist/url-pattern-completer.js.map +7 -0
- package/dist/visual-diff-verdict.d.ts +40 -0
- package/dist/visual-diff-verdict.js +25 -0
- package/dist/visual-diff-verdict.js.map +7 -0
- package/dist/webhook-notifier.d.ts +30 -0
- package/dist/webhook-notifier.js +76 -0
- package/dist/webhook-notifier.js.map +7 -0
- package/dist/websocket-detector.d.ts +24 -0
- package/dist/websocket-detector.js +60 -0
- package/dist/websocket-detector.js.map +7 -0
- package/dist/worker.d.ts +1 -0
- package/dist/worker.js +33 -0
- package/dist/worker.js.map +7 -0
- package/package.json +45 -0
- package/src/accessibility-auditor.ts +102 -0
- package/src/ai-login.ts +218 -0
- package/src/auth-token-rotator.ts +89 -0
- package/src/auth.ts +396 -0
- package/src/browser-extension.ts +186 -0
- package/src/captcha-solver.ts +143 -0
- package/src/color-contrast.ts +116 -0
- package/src/content-chunker.ts +170 -0
- package/src/content-dedup.ts +99 -0
- package/src/cookie-consent.ts +106 -0
- package/src/cookie-jar.ts +91 -0
- package/src/crawler.ts +4305 -0
- package/src/custom-js-injector.ts +93 -0
- package/src/dark-mode-capture.ts +110 -0
- package/src/dead-link-detector.ts +76 -0
- package/src/element-hash.ts +16 -0
- package/src/email-otp.ts +252 -0
- package/src/engine-router.ts +141 -0
- package/src/export/bulk-export.ts +85 -0
- package/src/export/vector-export.ts +160 -0
- package/src/extractors/advanced.ts +283 -0
- package/src/extractors/artifact-envelope.ts +111 -0
- package/src/failure-analysis-agent.ts +49 -0
- package/src/feature-flag-detector.ts +97 -0
- package/src/form-detector.ts +103 -0
- package/src/frontier.test.ts +64 -0
- package/src/frontier.ts +112 -0
- package/src/gap-pass.ts +122 -0
- package/src/graphql-detector.ts +129 -0
- package/src/http-engine.ts +433 -0
- package/src/incremental-diff.ts +121 -0
- package/src/index.ts +35 -0
- package/src/input-fuzzer.ts +129 -0
- package/src/interactive-probe.ts +623 -0
- package/src/job-queue.test.ts +120 -0
- package/src/job-queue.ts +2701 -0
- package/src/js-coverage.ts +87 -0
- package/src/js-router-detector.ts +112 -0
- package/src/language-detector.ts +82 -0
- package/src/link-classifier.ts +65 -0
- package/src/live-view.ts +141 -0
- package/src/logger.ts +9 -0
- package/src/markdown-extract.test.ts +73 -0
- package/src/markdown-extract.ts +76 -0
- package/src/native-crawler.ts +1106 -0
- package/src/network-observer.ts +204 -0
- package/src/network-throttle.ts +69 -0
- package/src/openapi-generator.ts +135 -0
- package/src/pdf-extractor.ts +123 -0
- package/src/performance-metrics.ts +134 -0
- package/src/phash.ts +151 -0
- package/src/pii-scrubber.ts +99 -0
- package/src/playwright-recorder.ts +170 -0
- package/src/policies/crawl-safety.ts +46 -0
- package/src/profiles/domain-profile.ts +167 -0
- package/src/proxy.ts +238 -0
- package/src/rate-limiter.ts +87 -0
- package/src/redirect-tracker.ts +97 -0
- package/src/redis-frontier.ts +143 -0
- package/src/redis-queue.ts +208 -0
- package/src/response-cache.ts +150 -0
- package/src/responsive-detector.ts +92 -0
- package/src/retry-policy.ts +87 -0
- package/src/robots-txt.ts +96 -0
- package/src/screen-reader.ts +89 -0
- package/src/screenshot-masker.ts +98 -0
- package/src/seo-checker.ts +134 -0
- package/src/session-capture.ts +325 -0
- package/src/session-export.test.ts +75 -0
- package/src/session-export.ts +172 -0
- package/src/sitemap-writer.ts +65 -0
- package/src/sitemap.test.ts +131 -0
- package/src/sitemap.ts +224 -0
- package/src/skyvern.ts +199 -0
- package/src/sqs-queue.ts +49 -0
- package/src/sso-token.ts +120 -0
- package/src/stealth/human-behavior.ts +191 -0
- package/src/telemetry.ts +122 -0
- package/src/test-executor.test.ts +72 -0
- package/src/test-executor.ts +424 -0
- package/src/tls-fingerprint.ts +111 -0
- package/src/url-pattern-completer.ts +114 -0
- package/src/visual-diff-verdict.test.ts +80 -0
- package/src/visual-diff-verdict.ts +57 -0
- package/src/webhook-notifier.ts +109 -0
- package/src/websocket-detector.ts +83 -0
- package/src/worker.ts +48 -0
- package/tsconfig.json +13 -0
- package/types/accessibility-auditor.d.ts +29 -0
- package/types/ai-login.d.ts +32 -0
- package/types/auth-token-rotator.d.ts +47 -0
- package/types/auth.d.ts +20 -0
- package/types/browser-extension.d.ts +37 -0
- package/types/bull-worker.d.ts +6 -0
- package/types/captcha-solver.d.ts +16 -0
- package/types/color-contrast.d.ts +30 -0
- package/types/content-chunker.d.ts +18 -0
- package/types/content-dedup.d.ts +23 -0
- package/types/cookie-consent.d.ts +25 -0
- package/types/cookie-jar.d.ts +20 -0
- package/types/crawler.d.ts +179 -0
- package/types/custom-js-injector.d.ts +42 -0
- package/types/dark-mode-capture.d.ts +23 -0
- package/types/dead-link-detector.d.ts +37 -0
- package/types/element-hash.d.ts +9 -0
- package/types/email-otp.d.ts +42 -0
- package/types/engine-router.d.ts +47 -0
- package/types/export/bulk-export.d.ts +26 -0
- package/types/export/vector-export.d.ts +76 -0
- package/types/extractors/advanced.d.ts +54 -0
- package/types/extractors/artifact-envelope.d.ts +63 -0
- package/types/failure-analysis-agent.d.ts +16 -0
- package/types/feature-flag-detector.d.ts +30 -0
- package/types/form-detector.d.ts +34 -0
- package/types/frontier.d.ts +32 -0
- package/types/gap-pass.d.ts +35 -0
- package/types/graphql-detector.d.ts +39 -0
- package/types/http-engine.d.ts +80 -0
- package/types/incremental-diff.d.ts +59 -0
- package/types/index.d.ts +35 -0
- package/types/input-fuzzer.d.ts +30 -0
- package/types/interactive-probe.d.ts +104 -0
- package/types/job-queue.d.ts +171 -0
- package/types/js-coverage.d.ts +29 -0
- package/types/js-router-detector.d.ts +17 -0
- package/types/language-detector.d.ts +22 -0
- package/types/link-classifier.d.ts +23 -0
- package/types/live-view.d.ts +36 -0
- package/types/logger.d.ts +2 -0
- package/types/markdown-extract.d.ts +8 -0
- package/types/native-crawler.d.ts +69 -0
- package/types/network-observer.d.ts +49 -0
- package/types/network-throttle.d.ts +25 -0
- package/types/openapi-generator.d.ts +49 -0
- package/types/pdf-extractor.d.ts +28 -0
- package/types/performance-metrics.d.ts +53 -0
- package/types/phash.d.ts +38 -0
- package/types/pii-scrubber.d.ts +21 -0
- package/types/playwright-recorder.d.ts +39 -0
- package/types/policies/crawl-safety.d.ts +9 -0
- package/types/profiles/domain-profile.d.ts +19 -0
- package/types/proxy.d.ts +50 -0
- package/types/rate-limiter.d.ts +31 -0
- package/types/redirect-tracker.d.ts +37 -0
- package/types/redis-frontier.d.ts +48 -0
- package/types/redis-queue.d.ts +48 -0
- package/types/response-cache.d.ts +38 -0
- package/types/responsive-detector.d.ts +29 -0
- package/types/retry-policy.d.ts +20 -0
- package/types/robots-txt.d.ts +9 -0
- package/types/screen-reader.d.ts +16 -0
- package/types/screenshot-masker.d.ts +28 -0
- package/types/seo-checker.d.ts +33 -0
- package/types/session-capture.d.ts +38 -0
- package/types/session-export.d.ts +49 -0
- package/types/sitemap-writer.d.ts +21 -0
- package/types/sitemap.d.ts +51 -0
- package/types/skyvern.d.ts +56 -0
- package/types/sqs-queue.d.ts +2 -0
- package/types/sso-token.d.ts +29 -0
- package/types/stealth/human-behavior.d.ts +20 -0
- package/types/telemetry.d.ts +34 -0
- package/types/test-executor.d.ts +67 -0
- package/types/tls-fingerprint.d.ts +50 -0
- package/types/url-pattern-completer.d.ts +28 -0
- package/types/visual-diff-verdict.d.ts +40 -0
- package/types/webhook-notifier.d.ts +30 -0
- package/types/websocket-detector.d.ts +24 -0
- package/types/worker.d.ts +1 -0
package/src/auth.ts
ADDED
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import type { Page } from 'playwright';
|
|
2
|
+
|
|
3
|
+
export interface Credentials {
|
|
4
|
+
username: string;
|
|
5
|
+
password: string;
|
|
6
|
+
oneTimeCode?: string;
|
|
7
|
+
getOneTimeCode?: () => Promise<string>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Recorded login step captured by the Chrome extension.
|
|
12
|
+
* {username} and {password} in `value` are substituted at replay time.
|
|
13
|
+
*/
|
|
14
|
+
export interface LoginStep {
|
|
15
|
+
action: 'fill' | 'click' | 'wait';
|
|
16
|
+
selector: string;
|
|
17
|
+
value?: string;
|
|
18
|
+
ms?: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
22
|
+
|
|
23
|
+
async function firstVisible(page: Page, selector: string) {
|
|
24
|
+
const loc = page.locator(selector);
|
|
25
|
+
const count = await loc.count().catch(() => 0);
|
|
26
|
+
for (let i = 0; i < count; i += 1) {
|
|
27
|
+
const item = loc.nth(i);
|
|
28
|
+
if (await item.isVisible().catch(() => false)) return item;
|
|
29
|
+
}
|
|
30
|
+
// iframe fallback — Salesforce, Zendesk, and embedded login widgets hide forms in iframes
|
|
31
|
+
for (const frame of page.frames()) {
|
|
32
|
+
if (frame === page.mainFrame()) continue;
|
|
33
|
+
const floc = frame.locator(selector);
|
|
34
|
+
const fcount = await floc.count().catch(() => 0);
|
|
35
|
+
for (let i = 0; i < fcount; i += 1) {
|
|
36
|
+
const item = floc.nth(i);
|
|
37
|
+
if (await item.isVisible().catch(() => false)) return item;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Type into framework-controlled inputs — handles React, Angular, Vue, and plain HTML forms. */
|
|
44
|
+
async function reactFill(field: Awaited<ReturnType<typeof firstVisible>>, value: string) {
|
|
45
|
+
if (!field) return;
|
|
46
|
+
await field.click();
|
|
47
|
+
await field.selectText().catch(() => {});
|
|
48
|
+
await field.press('Control+a').catch(() => {});
|
|
49
|
+
await field.press('Backspace').catch(() => {});
|
|
50
|
+
await field.pressSequentially(value, { delay: 30 });
|
|
51
|
+
// Fire native input + change events for Angular/Vue/custom frameworks that ignore DOM keypresses.
|
|
52
|
+
// React's synthetic listener catches pressSequentially above; this covers the rest.
|
|
53
|
+
await field.evaluate((el, v) => {
|
|
54
|
+
const input = el as HTMLInputElement;
|
|
55
|
+
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value')?.set;
|
|
56
|
+
if (nativeInputValueSetter) nativeInputValueSetter.call(input, v);
|
|
57
|
+
input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
58
|
+
input.dispatchEvent(new Event('change', { bubbles: true }));
|
|
59
|
+
input.dispatchEvent(new Event('blur', { bubbles: true }));
|
|
60
|
+
}, value).catch(() => {});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Find and click the most likely submit button.
|
|
65
|
+
* Waits up to 2 s for React's disabled={!email||!password} to clear before clicking.
|
|
66
|
+
*/
|
|
67
|
+
async function clickLikelySubmit(page: Page) {
|
|
68
|
+
// Prefer explicit submit selectors first; "Continue" is last resort to avoid
|
|
69
|
+
// accidentally clicking "Continue with Google/GitHub/Apple" SSO buttons.
|
|
70
|
+
let button = await firstVisible(page, [
|
|
71
|
+
'button[type="submit"]',
|
|
72
|
+
'input[type="submit"]',
|
|
73
|
+
'button:has-text("Next")',
|
|
74
|
+
'button:has-text("Sign in")',
|
|
75
|
+
'button:has-text("Log in")',
|
|
76
|
+
'button:has-text("Login")',
|
|
77
|
+
'button:has-text("Sign up")',
|
|
78
|
+
].join(', '));
|
|
79
|
+
|
|
80
|
+
if (!button) {
|
|
81
|
+
// Find "Continue" buttons but exclude OAuth provider ones ("Continue with Google" etc.)
|
|
82
|
+
const candidates = page.locator('button:has-text("Continue")');
|
|
83
|
+
const count = await candidates.count().catch(() => 0);
|
|
84
|
+
for (let i = 0; i < count; i++) {
|
|
85
|
+
const c = candidates.nth(i);
|
|
86
|
+
if (!(await c.isVisible().catch(() => false))) continue;
|
|
87
|
+
const text = (await c.textContent().catch(() => '')) ?? '';
|
|
88
|
+
if (/continue\s+with\s+/i.test(text)) continue; // skip "Continue with Google/GitHub/Apple"
|
|
89
|
+
button = c;
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (button) {
|
|
94
|
+
// Poll for enabled state — waitForFunction not available on Stagehand V3 proxy
|
|
95
|
+
const handle = await button.elementHandle().catch(() => null);
|
|
96
|
+
if (handle) {
|
|
97
|
+
const deadline = Date.now() + 2_000;
|
|
98
|
+
while (Date.now() < deadline) {
|
|
99
|
+
const disabled = await handle.evaluate((el) => (el as HTMLButtonElement).disabled).catch(() => false);
|
|
100
|
+
if (!disabled) break;
|
|
101
|
+
await new Promise(r => setTimeout(r, 100));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
await Promise.all([
|
|
105
|
+
page.waitForLoadState('networkidle').catch(() => {}),
|
|
106
|
+
button.click({ timeout: 5_000 }).catch(() => {}), // page may navigate before click completes — ignore
|
|
107
|
+
]);
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
await page.keyboard.press('Enter');
|
|
111
|
+
await page.waitForLoadState('networkidle').catch(() => {});
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Dismiss cookie/GDPR consent banners so they don't block form fields. */
|
|
116
|
+
async function dismissCookieBanners(page: Page) {
|
|
117
|
+
const acceptSel = [
|
|
118
|
+
'button:has-text("Accept")',
|
|
119
|
+
'button:has-text("Accept all")',
|
|
120
|
+
'button:has-text("Allow all")',
|
|
121
|
+
'button:has-text("I agree")',
|
|
122
|
+
'button:has-text("Agree")',
|
|
123
|
+
'button:has-text("OK")',
|
|
124
|
+
'button:has-text("Got it")',
|
|
125
|
+
'button:has-text("Close")',
|
|
126
|
+
'[aria-label*="Accept" i]',
|
|
127
|
+
'[aria-label*="Dismiss" i]',
|
|
128
|
+
'[class*="cookie"] button',
|
|
129
|
+
'[id*="cookie"] button',
|
|
130
|
+
'[class*="consent"] button',
|
|
131
|
+
'[class*="banner"] button[class*="close" i]',
|
|
132
|
+
].join(', ');
|
|
133
|
+
const btn = await firstVisible(page, acceptSel);
|
|
134
|
+
if (btn) await btn.click().catch(() => {});
|
|
135
|
+
await page.waitForTimeout(300).catch(() => {});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Look for a Sign In / Log In nav link on a marketing homepage and navigate to the actual login page.
|
|
140
|
+
* Returns true if navigation happened.
|
|
141
|
+
*/
|
|
142
|
+
async function navigateToLoginPage(page: Page, appUrl: string): Promise<boolean> {
|
|
143
|
+
// First try common login URL suffixes
|
|
144
|
+
const origin = (() => { try { return new URL(appUrl).origin; } catch { return ''; } })();
|
|
145
|
+
const loginPaths = ['/login', '/sign-in', '/signin', '/auth/login', '/auth', '/account/login', '/user/login', '/app/login'];
|
|
146
|
+
|
|
147
|
+
// Look for a "Sign In" / "Login" / "Get started" link in the nav
|
|
148
|
+
const navLoginSel = [
|
|
149
|
+
'a:has-text("Sign in")',
|
|
150
|
+
'a:has-text("Sign In")',
|
|
151
|
+
'a:has-text("Log in")',
|
|
152
|
+
'a:has-text("Log In")',
|
|
153
|
+
'a:has-text("Login")',
|
|
154
|
+
'a:has-text("Get started")',
|
|
155
|
+
'a[href*="login" i]',
|
|
156
|
+
'a[href*="sign-in" i]',
|
|
157
|
+
'a[href*="signin" i]',
|
|
158
|
+
'a[href*="auth" i]',
|
|
159
|
+
'button:has-text("Sign in")',
|
|
160
|
+
'button:has-text("Log in")',
|
|
161
|
+
'button:has-text("Login")',
|
|
162
|
+
].join(', ');
|
|
163
|
+
|
|
164
|
+
const navLink = await firstVisible(page, navLoginSel);
|
|
165
|
+
if (navLink) {
|
|
166
|
+
const href = await navLink.getAttribute('href').catch(() => null);
|
|
167
|
+
// If the link has an href, navigate directly — avoids modal-only flows
|
|
168
|
+
if (href && href.startsWith('http')) {
|
|
169
|
+
await page.goto(href, { waitUntil: 'networkidle', timeout: 15_000 }).catch(() => {});
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
// Click and immediately check for email field (modal opened) OR wait for navigation
|
|
173
|
+
await navLink.click();
|
|
174
|
+
const emailAppeared = await page.waitForSelector(EMAIL_SELECTOR, { timeout: 3_000 })
|
|
175
|
+
.then(() => true).catch(() => false);
|
|
176
|
+
if (!emailAppeared) {
|
|
177
|
+
// No modal — likely navigating to a new page
|
|
178
|
+
await page.waitForLoadState('networkidle', { timeout: 8_000 }).catch(() => {});
|
|
179
|
+
}
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Fallback: try common login paths on the same origin
|
|
184
|
+
if (origin) {
|
|
185
|
+
for (const p of loginPaths) {
|
|
186
|
+
const loginUrl = `${origin}${p}`;
|
|
187
|
+
const res = await page.goto(loginUrl, { waitUntil: 'networkidle', timeout: 10_000 }).catch(() => null);
|
|
188
|
+
if (res && res.status() < 400) {
|
|
189
|
+
// Check if this page actually has a login form
|
|
190
|
+
const hasForm = (await page.locator(
|
|
191
|
+
'input[type="email"], input[type="password"], input[name*="email" i], input[name*="user" i]'
|
|
192
|
+
).count().catch(() => 0)) > 0;
|
|
193
|
+
if (hasForm) return true;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// Restore original page
|
|
197
|
+
await page.goto(appUrl, { waitUntil: 'networkidle', timeout: 15_000 }).catch(() => {});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Detect if current page redirected to a 3rd-party OAuth provider (SSO flow we can't automate). */
|
|
204
|
+
function isOAuthProviderUrl(url: string): string | null {
|
|
205
|
+
const u = url.toLowerCase();
|
|
206
|
+
if (u.includes('accounts.google.com')) return 'Google';
|
|
207
|
+
if (u.includes('github.com/login')) return 'GitHub';
|
|
208
|
+
if (u.includes('login.microsoftonline.com')) return 'Microsoft';
|
|
209
|
+
if (u.includes('login.live.com')) return 'Microsoft';
|
|
210
|
+
if (u.includes('appleid.apple.com')) return 'Apple';
|
|
211
|
+
if (u.includes('okta.com')) return 'Okta';
|
|
212
|
+
if (u.includes('auth0.com')) return 'Auth0';
|
|
213
|
+
if (u.includes('onelogin.com')) return 'OneLogin';
|
|
214
|
+
if (u.includes('pingidentity.com')) return 'Ping Identity';
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const EMAIL_SELECTOR = 'input[type="email"], input[name*="user" i], input[name*="email" i], input[autocomplete="username"]';
|
|
219
|
+
const PASS_SELECTOR = 'input[type="password"], input[autocomplete="current-password"]';
|
|
220
|
+
|
|
221
|
+
/** Replay recorded login steps from the Chrome extension. Returns true if login succeeded. */
|
|
222
|
+
async function replayLoginRecipe(page: Page, steps: LoginStep[], creds: { username: string; password: string }): Promise<boolean> {
|
|
223
|
+
for (const step of steps) {
|
|
224
|
+
try {
|
|
225
|
+
if (step.action === 'wait') {
|
|
226
|
+
await page.waitForTimeout(step.ms ?? 500).catch(() => {});
|
|
227
|
+
} else if (step.action === 'click') {
|
|
228
|
+
const el = await firstVisible(page, step.selector);
|
|
229
|
+
if (!el) return false;
|
|
230
|
+
await el.click({ timeout: 5_000 }).catch(() => {});
|
|
231
|
+
} else if (step.action === 'fill') {
|
|
232
|
+
const el = await firstVisible(page, step.selector);
|
|
233
|
+
if (!el) return false;
|
|
234
|
+
const val = (step.value ?? '')
|
|
235
|
+
.replace('{username}', creds.username)
|
|
236
|
+
.replace('{password}', creds.password);
|
|
237
|
+
await reactFill(el, val);
|
|
238
|
+
}
|
|
239
|
+
await page.waitForTimeout(200).catch(() => {});
|
|
240
|
+
} catch { return false; }
|
|
241
|
+
}
|
|
242
|
+
await page.waitForLoadState('networkidle', { timeout: 5_000 }).catch(() => {});
|
|
243
|
+
const postUrl = page.url();
|
|
244
|
+
const LOGIN_PATH_RE = /\/(login|signin|sign-in|auth|log-in|sso|oauth)(\/|$|\?)/i;
|
|
245
|
+
if (!LOGIN_PATH_RE.test(new URL(postUrl).pathname)) return true; // navigated away — success
|
|
246
|
+
const passCount = await page.locator('input[type="password"]').count().catch(() => 0);
|
|
247
|
+
return passCount === 0;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// ─── Main login function ───────────────────────────────────────────────────────
|
|
251
|
+
|
|
252
|
+
export async function login(page: Page, appUrl: string, creds: Credentials & { loginRecipe?: LoginStep[] }): Promise<boolean> {
|
|
253
|
+
await page.goto(appUrl, { waitUntil: 'networkidle', timeout: 30_000 }).catch(() => {});
|
|
254
|
+
|
|
255
|
+
// Recipe replay — try exact recorded steps first when available
|
|
256
|
+
if (creds.loginRecipe && creds.loginRecipe.length > 0) {
|
|
257
|
+
const recipeOk = await replayLoginRecipe(page, creds.loginRecipe, creds);
|
|
258
|
+
if (recipeOk) return true;
|
|
259
|
+
// Recipe failed — reload page and fall through to DOM heuristic
|
|
260
|
+
await page.goto(appUrl, { waitUntil: 'networkidle', timeout: 30_000 }).catch(() => {});
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Dismiss cookie banners that might block form interaction
|
|
264
|
+
await dismissCookieBanners(page);
|
|
265
|
+
|
|
266
|
+
let userField = await firstVisible(page, EMAIL_SELECTOR);
|
|
267
|
+
|
|
268
|
+
// No email field on the landing page — this is a marketing homepage.
|
|
269
|
+
// Try to navigate to the actual login page.
|
|
270
|
+
if (!userField) {
|
|
271
|
+
await navigateToLoginPage(page, appUrl);
|
|
272
|
+
// NOTE: do NOT call dismissCookieBanners here — it can close login modals
|
|
273
|
+
// navigateToLoginPage already waits for email field or networkidle
|
|
274
|
+
userField = await firstVisible(page, EMAIL_SELECTOR);
|
|
275
|
+
if (!userField) {
|
|
276
|
+
// Last attempt: wait up to 5s for form to appear (SPA redirect or slow modal)
|
|
277
|
+
await page.waitForSelector(EMAIL_SELECTOR, { timeout: 5_000 }).catch(() => {});
|
|
278
|
+
userField = await firstVisible(page, EMAIL_SELECTOR);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (!userField) return false;
|
|
283
|
+
|
|
284
|
+
await reactFill(userField, creds.username);
|
|
285
|
+
await page.keyboard.press('Tab');
|
|
286
|
+
|
|
287
|
+
let passField = await firstVisible(page, PASS_SELECTOR);
|
|
288
|
+
if (!passField) {
|
|
289
|
+
// Multi-step form: email-first, then password on next screen
|
|
290
|
+
await clickLikelySubmit(page);
|
|
291
|
+
// Wait for password field to appear (up to 8s) rather than a fixed 2s sleep —
|
|
292
|
+
// slow IdP servers (Okta, Azure) take 3-5s to validate email and render the next step.
|
|
293
|
+
await page.waitForSelector(PASS_SELECTOR, { timeout: 8_000 }).catch(() => {});
|
|
294
|
+
// Check for SSO redirect after email submission
|
|
295
|
+
const ssoProvider = isOAuthProviderUrl(page.url());
|
|
296
|
+
if (ssoProvider) {
|
|
297
|
+
throw Object.assign(
|
|
298
|
+
new Error(
|
|
299
|
+
`This app uses ${ssoProvider} SSO — ZeTa cannot automate OAuth login. ` +
|
|
300
|
+
`Use the ZeTa Capture Chrome Extension instead: log in normally in Chrome, ` +
|
|
301
|
+
`then click the extension to capture your session.`
|
|
302
|
+
),
|
|
303
|
+
{ isLoginError: true, isSSOError: true, ssoProvider }
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
passField = await firstVisible(page, PASS_SELECTOR);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (!passField) {
|
|
310
|
+
// Still no password field — check if we landed on an SSO provider
|
|
311
|
+
const ssoProvider = isOAuthProviderUrl(page.url());
|
|
312
|
+
if (ssoProvider) {
|
|
313
|
+
throw Object.assign(
|
|
314
|
+
new Error(
|
|
315
|
+
`This app uses ${ssoProvider} SSO — ZeTa cannot automate OAuth login. ` +
|
|
316
|
+
`Use the ZeTa Capture Chrome Extension instead: log in normally in Chrome, ` +
|
|
317
|
+
`then click the extension to capture your session.`
|
|
318
|
+
),
|
|
319
|
+
{ isLoginError: true, isSSOError: true, ssoProvider }
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
await reactFill(passField, creds.password);
|
|
326
|
+
// Press Enter — submits regardless of button disabled state
|
|
327
|
+
await page.waitForTimeout(150).catch(() => {});
|
|
328
|
+
await Promise.all([
|
|
329
|
+
page.waitForLoadState('networkidle').catch(() => {}),
|
|
330
|
+
page.keyboard.press('Enter'),
|
|
331
|
+
]);
|
|
332
|
+
|
|
333
|
+
// Fallback: if Enter didn't navigate away, try Tab → click
|
|
334
|
+
const stillHasPass = await page.locator(PASS_SELECTOR).count().catch(() => 0);
|
|
335
|
+
if (stillHasPass > 0) {
|
|
336
|
+
await page.keyboard.press('Tab');
|
|
337
|
+
await clickLikelySubmit(page);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Wait for post-submit navigation to complete — waitForLoadState resolves immediately
|
|
341
|
+
// if page is already idle, so use waitForURL to detect actual navigation away from login.
|
|
342
|
+
const LOGIN_PATH_RE_NAV = /\/(login|signin|sign-in|auth|log-in|sso|oauth)(\/|$|\?)/i;
|
|
343
|
+
if (LOGIN_PATH_RE_NAV.test(new URL(page.url()).pathname)) {
|
|
344
|
+
await page.waitForURL(
|
|
345
|
+
(url) => { try { return !LOGIN_PATH_RE_NAV.test(new URL(url).pathname); } catch { return true; } },
|
|
346
|
+
{ timeout: 12_000 }
|
|
347
|
+
).catch(() => {});
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// Check for SSO redirect after password submit
|
|
351
|
+
const ssoAfterLogin = isOAuthProviderUrl(page.url());
|
|
352
|
+
if (ssoAfterLogin) {
|
|
353
|
+
throw Object.assign(
|
|
354
|
+
new Error(
|
|
355
|
+
`This app uses ${ssoAfterLogin} SSO — ZeTa cannot automate OAuth login. ` +
|
|
356
|
+
`Use the ZeTa Capture Chrome Extension instead.`
|
|
357
|
+
),
|
|
358
|
+
{ isLoginError: true, isSSOError: true, ssoProvider: ssoAfterLogin }
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// Handle OTP / 2FA
|
|
363
|
+
const otpField = await firstVisible(page, [
|
|
364
|
+
'input[autocomplete="one-time-code"]',
|
|
365
|
+
'input[name*="otp" i]',
|
|
366
|
+
'input[name*="code" i]',
|
|
367
|
+
'input[aria-label*="code" i]',
|
|
368
|
+
'input[placeholder*="code" i]',
|
|
369
|
+
].join(', '));
|
|
370
|
+
if (otpField) {
|
|
371
|
+
const code = creds.oneTimeCode ?? await creds.getOneTimeCode?.();
|
|
372
|
+
if (!code) return false;
|
|
373
|
+
await reactFill(otpField, code);
|
|
374
|
+
await clickLikelySubmit(page);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// Success check: navigated away from login page, and/or no password field remains.
|
|
378
|
+
// Use URL as primary signal — dashboards can have hidden password fields (profile forms, etc.)
|
|
379
|
+
// that would cause false negatives if we only check input[type="password"].
|
|
380
|
+
const postLoginUrl = page.url();
|
|
381
|
+
const LOGIN_PATH_RE = /\/(login|signin|sign-in|auth|log-in|sso|oauth)(\/|$|\?)/i;
|
|
382
|
+
const stillOnLoginPath = LOGIN_PATH_RE.test(new URL(postLoginUrl).pathname);
|
|
383
|
+
const stillOnLogin = await page.locator(PASS_SELECTOR).count();
|
|
384
|
+
|
|
385
|
+
// Only treat as failure if BOTH: still on a login-path URL AND password field present
|
|
386
|
+
if (stillOnLoginPath && stillOnLogin > 0) {
|
|
387
|
+
const errorLoc = page.locator('[role="alert"], .error, [class*="error"], [class*="alert"]');
|
|
388
|
+
await errorLoc.first().waitFor({ state: 'visible', timeout: 2_000 }).catch(() => {});
|
|
389
|
+
const pageError = await errorLoc.first().textContent().catch(() => null);
|
|
390
|
+
if (pageError?.trim()) {
|
|
391
|
+
throw Object.assign(new Error(pageError.trim()), { isLoginError: true });
|
|
392
|
+
}
|
|
393
|
+
return false;
|
|
394
|
+
}
|
|
395
|
+
return true;
|
|
396
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser extension injection support.
|
|
3
|
+
*
|
|
4
|
+
* Chrome extensions require a persistent profile directory — incompatible
|
|
5
|
+
* with headless Docker by default. This module handles:
|
|
6
|
+
* 1. Persistent profile directory per extension set
|
|
7
|
+
* 2. Extension download from .crx URL or load from local path
|
|
8
|
+
* 3. Launch args for Playwright / Stagehand
|
|
9
|
+
*
|
|
10
|
+
* Usage:
|
|
11
|
+
* const profile = await prepareExtensionProfile(['/path/to/ext.crx']);
|
|
12
|
+
* // pass profile.userDataDir and profile.args to browser launch
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
16
|
+
import { createWriteStream, existsSync, mkdirSync } from 'node:fs';
|
|
17
|
+
import { writeFile, mkdir, rm } from 'node:fs/promises';
|
|
18
|
+
import { join } from 'node:path';
|
|
19
|
+
import { tmpdir } from 'node:os';
|
|
20
|
+
import { pipeline } from 'node:stream/promises';
|
|
21
|
+
import { Readable } from 'node:stream';
|
|
22
|
+
|
|
23
|
+
const PROFILE_BASE = process.env.EXTENSION_PROFILE_DIR ?? join(tmpdir(), 'zeta-ext-profiles');
|
|
24
|
+
|
|
25
|
+
export interface ExtensionSource {
|
|
26
|
+
/** Local .crx file path or https:// URL to download from */
|
|
27
|
+
source: string;
|
|
28
|
+
/** Friendly name for logging */
|
|
29
|
+
name?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ExtensionProfile {
|
|
33
|
+
/** Chrome user-data-dir containing Preferences + extension unpacked dirs */
|
|
34
|
+
userDataDir: string;
|
|
35
|
+
/** Args to append to chromium launch — includes --load-extension and --disable-extensions-except */
|
|
36
|
+
args: string[];
|
|
37
|
+
/** Cleanup the profile directory when done */
|
|
38
|
+
cleanup: () => Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Download a .crx from a URL to a temp file. Returns path.
|
|
43
|
+
*/
|
|
44
|
+
async function downloadCrx(url: string): Promise<string> {
|
|
45
|
+
const dest = join(tmpdir(), `zeta-ext-${randomBytes(6).toString('hex')}.crx`);
|
|
46
|
+
const res = await fetch(url);
|
|
47
|
+
if (!res.ok) throw new Error(`Failed to download extension from ${url}: ${res.status}`);
|
|
48
|
+
const writer = createWriteStream(dest);
|
|
49
|
+
await pipeline(Readable.fromWeb(res.body as any), writer);
|
|
50
|
+
return dest;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Unpack a .crx file to a directory Chrome can load as an unpacked extension.
|
|
55
|
+
* CRX3 format: 4-byte magic + 4-byte version + 4-byte header_size + proto header + zip body
|
|
56
|
+
*/
|
|
57
|
+
async function unpackCrx(crxPath: string, destDir: string): Promise<void> {
|
|
58
|
+
const { readFile } = await import('node:fs/promises');
|
|
59
|
+
const buf = await readFile(crxPath);
|
|
60
|
+
|
|
61
|
+
const magic = buf.readUInt32LE(0);
|
|
62
|
+
if (magic !== 0x43723234) {
|
|
63
|
+
// Not CRX — assume it's already a zip (unpacked extension)
|
|
64
|
+
await mkdir(destDir, { recursive: true });
|
|
65
|
+
await writeFile(join(destDir, '__raw.crx'), buf);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const version = buf.readUInt32LE(4);
|
|
70
|
+
let zipStart: number;
|
|
71
|
+
if (version === 3) {
|
|
72
|
+
const headerSize = buf.readUInt32LE(8);
|
|
73
|
+
zipStart = 12 + headerSize;
|
|
74
|
+
} else if (version === 2) {
|
|
75
|
+
const pubKeyLen = buf.readUInt32LE(8);
|
|
76
|
+
const sigLen = buf.readUInt32LE(12);
|
|
77
|
+
zipStart = 16 + pubKeyLen + sigLen;
|
|
78
|
+
} else {
|
|
79
|
+
throw new Error(`Unknown CRX version ${version}`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const zipBuf = buf.subarray(zipStart);
|
|
83
|
+
const tmpZip = join(tmpdir(), `zeta-ext-${randomBytes(4).toString('hex')}.zip`);
|
|
84
|
+
await writeFile(tmpZip, zipBuf);
|
|
85
|
+
|
|
86
|
+
await mkdir(destDir, { recursive: true });
|
|
87
|
+
|
|
88
|
+
// Extract using unzipper (available in Node without native deps)
|
|
89
|
+
// @ts-ignore – no types for unzipper
|
|
90
|
+
const unzipper = await import('unzipper').catch(() => null);
|
|
91
|
+
if (unzipper) {
|
|
92
|
+
await unzipper.Open.file(tmpZip).then((d: any) => d.extract({ path: destDir }));
|
|
93
|
+
} else {
|
|
94
|
+
// Fallback: shell unzip
|
|
95
|
+
const { execFile } = await import('node:child_process');
|
|
96
|
+
await new Promise<void>((res, rej) =>
|
|
97
|
+
execFile('unzip', ['-o', tmpZip, '-d', destDir], (err) => err ? rej(err) : res())
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
await rm(tmpZip, { force: true });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Prepare a Chrome user-data-dir with the given extensions loaded.
|
|
105
|
+
* Returns profile info + cleanup function.
|
|
106
|
+
*/
|
|
107
|
+
export async function prepareExtensionProfile(
|
|
108
|
+
extensions: ExtensionSource[]
|
|
109
|
+
): Promise<ExtensionProfile> {
|
|
110
|
+
// Stable profile key based on extension sources
|
|
111
|
+
const key = createHash('md5')
|
|
112
|
+
.update(extensions.map(e => e.source).sort().join('|'))
|
|
113
|
+
.digest('hex')
|
|
114
|
+
.slice(0, 12);
|
|
115
|
+
|
|
116
|
+
const profileDir = join(PROFILE_BASE, key);
|
|
117
|
+
const extensionDirs: string[] = [];
|
|
118
|
+
|
|
119
|
+
mkdirSync(profileDir, { recursive: true });
|
|
120
|
+
|
|
121
|
+
for (const ext of extensions) {
|
|
122
|
+
const extHash = createHash('md5').update(ext.source).digest('hex').slice(0, 8);
|
|
123
|
+
const extDir = join(profileDir, `ext-${extHash}`);
|
|
124
|
+
|
|
125
|
+
if (!existsSync(extDir)) {
|
|
126
|
+
let crxPath = ext.source;
|
|
127
|
+
|
|
128
|
+
if (ext.source.startsWith('http://') || ext.source.startsWith('https://')) {
|
|
129
|
+
crxPath = await downloadCrx(ext.source);
|
|
130
|
+
await unpackCrx(crxPath, extDir);
|
|
131
|
+
await rm(crxPath, { force: true });
|
|
132
|
+
} else if (ext.source.endsWith('.crx')) {
|
|
133
|
+
await unpackCrx(ext.source, extDir);
|
|
134
|
+
} else {
|
|
135
|
+
// Assume already-unpacked extension directory — symlink or copy
|
|
136
|
+
extensionDirs.push(ext.source);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
extensionDirs.push(extDir);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const userDataDir = join(profileDir, 'user-data');
|
|
145
|
+
mkdirSync(userDataDir, { recursive: true });
|
|
146
|
+
|
|
147
|
+
// Write a minimal Preferences file so Chrome doesn't show first-run UI
|
|
148
|
+
const defaultsDir = join(userDataDir, 'Default');
|
|
149
|
+
mkdirSync(defaultsDir, { recursive: true });
|
|
150
|
+
const prefsPath = join(defaultsDir, 'Preferences');
|
|
151
|
+
if (!existsSync(prefsPath)) {
|
|
152
|
+
await writeFile(prefsPath, JSON.stringify({
|
|
153
|
+
browser: { has_seen_welcome_page: true },
|
|
154
|
+
extensions: { alerts: { initialized: true } },
|
|
155
|
+
}), 'utf8');
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const loadExtensionArg = extensionDirs.join(',');
|
|
159
|
+
const args: string[] = [
|
|
160
|
+
`--load-extension=${loadExtensionArg}`,
|
|
161
|
+
`--disable-extensions-except=${loadExtensionArg}`,
|
|
162
|
+
`--user-data-dir=${userDataDir}`,
|
|
163
|
+
'--no-first-run',
|
|
164
|
+
'--no-default-browser-check',
|
|
165
|
+
];
|
|
166
|
+
|
|
167
|
+
return {
|
|
168
|
+
userDataDir,
|
|
169
|
+
args,
|
|
170
|
+
cleanup: async () => {
|
|
171
|
+
await rm(profileDir, { recursive: true, force: true });
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Ad-blocker extension profile using the uBlock Origin CRX.
|
|
178
|
+
* Reduces noise from ad networks during crawls.
|
|
179
|
+
*/
|
|
180
|
+
export async function adBlockerProfile(): Promise<ExtensionProfile> {
|
|
181
|
+
// uBlock Origin 1.57.2 from GitHub release (MIT licensed)
|
|
182
|
+
return prepareExtensionProfile([{
|
|
183
|
+
name: 'uBlock Origin',
|
|
184
|
+
source: 'https://github.com/gorhill/uBlock/releases/download/1.57.2/uBlock0_1.57.2.chromium.zip',
|
|
185
|
+
}]);
|
|
186
|
+
}
|