@intuned/runtime-dev 1.3.14-ts-runtime-helpers → 1.3.15-hook.0

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 (32) hide show
  1. package/WebTemplate.zip +0 -0
  2. package/dist/commands/intuned-cli/commands/run_authsession.command.d.ts +6 -6
  3. package/dist/commands/intuned-cli/commands/types.d.ts +2 -2
  4. package/dist/commands/intuned-cli/controller/authSession.d.ts +6 -6
  5. package/dist/commands/intuned-cli/helpers/auth.d.ts +1 -1
  6. package/dist/commands/intuned-cli/helpers/intunedJson.d.ts +1 -1
  7. package/dist/common/constants.d.ts +0 -2
  8. package/dist/common/constants.js +2 -4
  9. package/dist/common/{extension/extensionsHelpers.d.ts → extensionsHelpers.d.ts} +1 -1
  10. package/dist/common/{extension/extensionsHelpers.js → extensionsHelpers.js} +6 -62
  11. package/dist/common/intunedJson.d.ts +6 -6
  12. package/dist/common/launchBrowser.js +1 -3
  13. package/dist/common/playwrightContext.js +1 -1
  14. package/dist/common/runApi/index.js +0 -6
  15. package/dist/common/runApi/types.d.ts +20 -20
  16. package/dist/common/settingsSchema.d.ts +1 -15
  17. package/dist/common/settingsSchema.js +0 -1
  18. package/dist/common/setupContextHook.d.ts +2 -2
  19. package/dist/index.d.ts +0 -1
  20. package/dist/index.js +1 -44
  21. package/dist/runtime/getAiGatewayConfig.d.ts +10 -0
  22. package/dist/runtime/getAiGatewayConfig.js +16 -0
  23. package/dist/runtime/index.d.ts +1 -1
  24. package/dist/runtime/index.js +7 -43
  25. package/package.json +1 -2
  26. package/dist/common/extension/intunedExtensionServer.d.ts +0 -25
  27. package/dist/common/extension/intunedExtensionServer.js +0 -164
  28. package/dist/common/extension/types.d.ts +0 -21
  29. package/dist/common/extension/types.js +0 -5
  30. package/dist/runtime/captcha.d.ts +0 -18
  31. package/dist/runtime/captcha.js +0 -190
  32. package/dist/runtime/captcha.test.js +0 -214
package/dist/index.js CHANGED
@@ -45,42 +45,12 @@ Object.defineProperty(exports, "getExecutionContext", {
45
45
  return _asyncLocalStorage.getExecutionContext;
46
46
  }
47
47
  });
48
- Object.defineProperty(exports, "onCaptchaEvent", {
49
- enumerable: true,
50
- get: function () {
51
- return _captcha.onCaptchaEvent;
52
- }
53
- });
54
- Object.defineProperty(exports, "onceCaptchaEvent", {
55
- enumerable: true,
56
- get: function () {
57
- return _captcha.onceCaptchaEvent;
58
- }
59
- });
60
- Object.defineProperty(exports, "pauseCaptchaSolver", {
61
- enumerable: true,
62
- get: function () {
63
- return _captcha.pauseCaptchaSolver;
64
- }
65
- });
66
48
  Object.defineProperty(exports, "persistentStore", {
67
49
  enumerable: true,
68
50
  get: function () {
69
51
  return _runtime.persistentStore;
70
52
  }
71
53
  });
72
- Object.defineProperty(exports, "removeCaptchaEventListener", {
73
- enumerable: true,
74
- get: function () {
75
- return _captcha.removeCaptchaEventListener;
76
- }
77
- });
78
- Object.defineProperty(exports, "resumeCaptchaSolver", {
79
- enumerable: true,
80
- get: function () {
81
- return _captcha.resumeCaptchaSolver;
82
- }
83
- });
84
54
  Object.defineProperty(exports, "runInfo", {
85
55
  enumerable: true,
86
56
  get: function () {
@@ -93,26 +63,13 @@ Object.defineProperty(exports, "runWithContext", {
93
63
  return _asyncLocalStorage.runWithContext;
94
64
  }
95
65
  });
96
- Object.defineProperty(exports, "waitForCaptchaSolve", {
97
- enumerable: true,
98
- get: function () {
99
- return _captcha.waitForCaptchaSolve;
100
- }
101
- });
102
66
  Object.defineProperty(exports, "withPlaywrightContext", {
103
67
  enumerable: true,
104
68
  get: function () {
105
69
  return _playwrightContext.withPlaywrightContext;
106
70
  }
107
71
  });
108
- Object.defineProperty(exports, "withWaitForCaptchaSolve", {
109
- enumerable: true,
110
- get: function () {
111
- return _captcha.withWaitForCaptchaSolve;
112
- }
113
- });
114
72
  var _runtime = require("./runtime");
115
73
  var _asyncLocalStorage = require("./common/asyncLocalStorage");
116
74
  var _downloadDirectory = require("./runtime/downloadDirectory");
117
- var _playwrightContext = require("./common/playwrightContext");
118
- var _captcha = require("./runtime/captcha");
75
+ var _playwrightContext = require("./common/playwrightContext");
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Retrieves the base URL and API key for the Intuned AI Gateway.
3
+
4
+ * Returns:
5
+ * { baseUrl: string, apiKey: string }: An object containing the base URL and API key
6
+ */
7
+ export declare function getAiGatewayConfig(): {
8
+ baseUrl: string;
9
+ apiKey: string;
10
+ };
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getAiGatewayConfig = getAiGatewayConfig;
7
+ var _constants = require("../common/constants");
8
+ var _jwtTokenManager = require("../common/jwtTokenManager");
9
+ function getAiGatewayConfig() {
10
+ const baseUrl = `${_jwtTokenManager.backendFunctionsTokenManager.backendFunctionsBaseUrl}/intuned-ai-gateway`;
11
+ const apiKey = _jwtTokenManager.backendFunctionsTokenManager.token ?? process.env[_constants.API_KEY_ENV_VAR_KEY] ?? "";
12
+ return {
13
+ baseUrl,
14
+ apiKey
15
+ };
16
+ }
@@ -5,5 +5,5 @@ export { persistentStore } from "./persistentStore";
5
5
  export { getAuthSessionParameters } from "./getAuthSessionParameters";
6
6
  export { runInfo } from "./runInfo";
7
7
  export { RunError } from "./RunError";
8
- export { waitForCaptchaSolve, withWaitForCaptchaSolve, onCaptchaEvent, onceCaptchaEvent, removeCaptchaEventListener, pauseCaptchaSolver, resumeCaptchaSolver, } from "./captcha";
8
+ export { getAiGatewayConfig } from "./getAiGatewayConfig";
9
9
  export { getDownloadDirectoryPath } from "./downloadDirectory";
@@ -27,34 +27,22 @@ Object.defineProperty(exports, "extendTimeout", {
27
27
  return _extendTimeout.extendTimeout;
28
28
  }
29
29
  });
30
- Object.defineProperty(exports, "getAuthSessionParameters", {
31
- enumerable: true,
32
- get: function () {
33
- return _getAuthSessionParameters.getAuthSessionParameters;
34
- }
35
- });
36
- Object.defineProperty(exports, "getDownloadDirectoryPath", {
37
- enumerable: true,
38
- get: function () {
39
- return _downloadDirectory.getDownloadDirectoryPath;
40
- }
41
- });
42
- Object.defineProperty(exports, "onCaptchaEvent", {
30
+ Object.defineProperty(exports, "getAiGatewayConfig", {
43
31
  enumerable: true,
44
32
  get: function () {
45
- return _captcha.onCaptchaEvent;
33
+ return _getAiGatewayConfig.getAiGatewayConfig;
46
34
  }
47
35
  });
48
- Object.defineProperty(exports, "onceCaptchaEvent", {
36
+ Object.defineProperty(exports, "getAuthSessionParameters", {
49
37
  enumerable: true,
50
38
  get: function () {
51
- return _captcha.onceCaptchaEvent;
39
+ return _getAuthSessionParameters.getAuthSessionParameters;
52
40
  }
53
41
  });
54
- Object.defineProperty(exports, "pauseCaptchaSolver", {
42
+ Object.defineProperty(exports, "getDownloadDirectoryPath", {
55
43
  enumerable: true,
56
44
  get: function () {
57
- return _captcha.pauseCaptchaSolver;
45
+ return _downloadDirectory.getDownloadDirectoryPath;
58
46
  }
59
47
  });
60
48
  Object.defineProperty(exports, "persistentStore", {
@@ -63,36 +51,12 @@ Object.defineProperty(exports, "persistentStore", {
63
51
  return _persistentStore.persistentStore;
64
52
  }
65
53
  });
66
- Object.defineProperty(exports, "removeCaptchaEventListener", {
67
- enumerable: true,
68
- get: function () {
69
- return _captcha.removeCaptchaEventListener;
70
- }
71
- });
72
- Object.defineProperty(exports, "resumeCaptchaSolver", {
73
- enumerable: true,
74
- get: function () {
75
- return _captcha.resumeCaptchaSolver;
76
- }
77
- });
78
54
  Object.defineProperty(exports, "runInfo", {
79
55
  enumerable: true,
80
56
  get: function () {
81
57
  return _runInfo.runInfo;
82
58
  }
83
59
  });
84
- Object.defineProperty(exports, "waitForCaptchaSolve", {
85
- enumerable: true,
86
- get: function () {
87
- return _captcha.waitForCaptchaSolve;
88
- }
89
- });
90
- Object.defineProperty(exports, "withWaitForCaptchaSolve", {
91
- enumerable: true,
92
- get: function () {
93
- return _captcha.withWaitForCaptchaSolve;
94
- }
95
- });
96
60
  var _extendPayload = require("./extendPayload");
97
61
  var _extendTimeout = require("./extendTimeout");
98
62
  var _attemptStore = require("./attemptStore");
@@ -100,5 +64,5 @@ var _persistentStore = require("./persistentStore");
100
64
  var _getAuthSessionParameters = require("./getAuthSessionParameters");
101
65
  var _runInfo = require("./runInfo");
102
66
  var _RunError = require("./RunError");
103
- var _captcha = require("./captcha");
67
+ var _getAiGatewayConfig = require("./getAiGatewayConfig");
104
68
  var _downloadDirectory = require("./downloadDirectory");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intuned/runtime-dev",
3
- "version": "1.3.14-ts-runtime-helpers",
3
+ "version": "1.3.15-hook.0",
4
4
  "description": "Intuned runtime",
5
5
  "exports": {
6
6
  ".": "./dist/index.js",
@@ -70,7 +70,6 @@
70
70
  "commander": "14.0.0",
71
71
  "cross-fetch": "^4.0.0",
72
72
  "dotenv": "^16.3.1",
73
- "fastify": "4.29.0",
74
73
  "fs-extra": "^11.3.0",
75
74
  "image-size": "^1.1.1",
76
75
  "jsonc-parser": "^3.3.1",
@@ -1,25 +0,0 @@
1
- import type * as playwright from "playwright";
2
- import { Captcha, CaptchaCallback, CaptchaStatus } from "./types";
3
- export declare class ExtensionServer {
4
- private tabs;
5
- private app;
6
- isHealthy: boolean;
7
- constructor();
8
- private getOrCreateTab;
9
- private handleUpsertCaptcha;
10
- start({ port, host, }: {
11
- port: number;
12
- host?: string;
13
- }): Promise<void>;
14
- stop(): Promise<void>;
15
- getCaptchas(page: playwright.Page, status?: CaptchaStatus): Promise<Captcha[]>;
16
- subscribe(page: playwright.Page, handler: CaptchaCallback, status?: CaptchaStatus): Promise<void>;
17
- unsubscribe(page: playwright.Page, handler: CaptchaCallback, status?: CaptchaStatus): Promise<void>;
18
- }
19
- export declare function getIntunedExtensionServer(): ExtensionServer;
20
- export declare function setupIntunedExtensionServer({ port, host, }: {
21
- port: number;
22
- host?: string;
23
- }): Promise<void>;
24
- export declare function cleanIntunedExtensionServer(): Promise<void>;
25
- export declare function getTabId(page: playwright.Page): Promise<number>;
@@ -1,164 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.ExtensionServer = void 0;
7
- exports.cleanIntunedExtensionServer = cleanIntunedExtensionServer;
8
- exports.getIntunedExtensionServer = getIntunedExtensionServer;
9
- exports.getTabId = getTabId;
10
- exports.setupIntunedExtensionServer = setupIntunedExtensionServer;
11
- var _fastify = _interopRequireDefault(require("fastify"));
12
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
- class TabCaptchaState {
14
- constructor(tabId) {
15
- this.tabId = tabId;
16
- this.captchasById = new Map();
17
- this.subscribers = new Array();
18
- }
19
- subscribe(handler) {
20
- this.subscribers.push(handler);
21
- }
22
- unsubscribe(handler, status) {
23
- const index = this.subscribers.findIndex(subscriber => subscriber.handler === handler && (subscriber.status === status || !status));
24
- if (index !== -1) {
25
- this.subscribers = this.subscribers.splice(index, 1);
26
- }
27
- }
28
- getCaptchas() {
29
- return [...this.captchasById.values()];
30
- }
31
- async upsertCaptcha(captcha) {
32
- this.captchasById.set(captcha.id, captcha);
33
- for (const subscriber of this.subscribers) {
34
- await subscriber.handler(captcha);
35
- }
36
- }
37
- }
38
- class ExtensionServer {
39
- app = null;
40
- isHealthy = false;
41
- constructor() {
42
- this.tabs = new Map();
43
- }
44
- getOrCreateTab(tabId) {
45
- const existing = this.tabs.get(tabId);
46
- if (existing) return existing;
47
- const created = new TabCaptchaState(tabId);
48
- this.tabs.set(tabId, created);
49
- return created;
50
- }
51
- async handleUpsertCaptcha(captcha) {
52
- const tab = this.getOrCreateTab(captcha.tabId);
53
- await tab.upsertCaptcha(captcha);
54
- }
55
- async start({
56
- port,
57
- host = "0.0.0.0"
58
- }) {
59
- if (this.app) {
60
- return;
61
- }
62
- this.app = (0, _fastify.default)({
63
- logger: false,
64
- bodyLimit: 1_000_000
65
- });
66
- this.app.post("/state", async (request, reply) => {
67
- try {
68
- const data = request.body ?? {};
69
- const captcha = {
70
- id: String(data?.id ?? ""),
71
- tabId: Number(data?.tabId ?? 0),
72
- type: data?.type,
73
- status: data?.status,
74
- retryCount: data?.retryCount,
75
- error: data?.error ?? null
76
- };
77
- if (!captcha.id || !captcha.tabId || !captcha.type || !captcha.status) {
78
- return reply.code(400).send({
79
- error: "Invalid captcha payload"
80
- });
81
- }
82
- await this.handleUpsertCaptcha(captcha);
83
- return reply.code(200).send({});
84
- } catch (error) {
85
- return reply.code(500).send({
86
- error: "Internal server error",
87
- message: error?.message ?? String(error)
88
- });
89
- }
90
- });
91
- this.app.setNotFoundHandler((_request, reply) => {
92
- return reply.code(404).send({
93
- error: "Not found"
94
- });
95
- });
96
- await this.app.listen({
97
- port,
98
- host
99
- });
100
- this.isHealthy = true;
101
- }
102
- async stop() {
103
- if (!this.app) return;
104
- const toClose = this.app;
105
- this.app = null;
106
- this.isHealthy = false;
107
- await toClose.close();
108
- }
109
- async getCaptchas(page, status) {
110
- const tabId = await getTabId(page);
111
- const tab = this.tabs.get(tabId);
112
- if (!tab) return [];
113
- const captchas = tab.getCaptchas();
114
- if (!status) return captchas;
115
- return captchas.filter(c => c.status === status && c.tabId === tabId);
116
- }
117
- async subscribe(page, handler, status) {
118
- const tabId = await getTabId(page);
119
- const tab = this.getOrCreateTab(tabId);
120
- tab.subscribe({
121
- handler,
122
- status
123
- });
124
- }
125
- async unsubscribe(page, handler, status) {
126
- const tabId = await getTabId(page);
127
- const tab = this.tabs.get(tabId);
128
- if (!tab) return;
129
- tab.unsubscribe(handler, status);
130
- }
131
- }
132
- exports.ExtensionServer = ExtensionServer;
133
- let extensionServerSingleton = null;
134
- function getIntunedExtensionServer() {
135
- if (!extensionServerSingleton) {
136
- throw new Error("Intuned Extension Server is not initialized.");
137
- }
138
- return extensionServerSingleton;
139
- }
140
- async function setupIntunedExtensionServer({
141
- port,
142
- host
143
- }) {
144
- if (!extensionServerSingleton) {
145
- extensionServerSingleton = new ExtensionServer();
146
- }
147
- await extensionServerSingleton.start({
148
- port,
149
- host
150
- });
151
- }
152
- async function cleanIntunedExtensionServer() {
153
- if (extensionServerSingleton) {
154
- await extensionServerSingleton.stop();
155
- extensionServerSingleton = null;
156
- }
157
- }
158
- async function getTabId(page) {
159
- await page.waitForFunction("window.__INTUNED_TAB_ID__ !== undefined", {
160
- timeout: 15_000
161
- });
162
- const tabId = await page.evaluate("window.__INTUNED_TAB_ID__");
163
- return Number(tabId);
164
- }
@@ -1,21 +0,0 @@
1
- export type CaptchaType = "aws" | "cloudflare" | "customcaptcha" | "funcaptcha" | "geetest" | "hcaptcha" | "lemincaptcha" | "recaptcha" | "textcaptcha";
2
- export type CaptchaStatus = "attached" | "solving" | "solved" | "error" | "detached";
3
- export type CaptchaError = {
4
- code: "HIT_LIMIT" | "MAX_RETRIES" | "UNEXPECTED_SERVER_RESPONSE" | "UNEXPECTED_ERROR";
5
- error?: unknown;
6
- };
7
- export type CaptchaBase = {
8
- id: string;
9
- tabId: number;
10
- type: CaptchaType;
11
- retryCount?: number;
12
- };
13
- export type CaptchaNonError = CaptchaBase & {
14
- status: Exclude<CaptchaStatus, "error">;
15
- };
16
- export type CaptchaErrorStatus = CaptchaBase & {
17
- status: "error";
18
- error: CaptchaError;
19
- };
20
- export type Captcha = CaptchaNonError | CaptchaErrorStatus;
21
- export type CaptchaCallback = (captcha: Captcha) => void | Promise<void>;
@@ -1,5 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
@@ -1,18 +0,0 @@
1
- import type { Page, BrowserContext } from "playwright";
2
- import { Captcha, CaptchaStatus, CaptchaCallback } from "../common/extension/types";
3
- export type WithWaitForCaptchaSolveOptions = {
4
- page: Page;
5
- timeoutInMs?: number;
6
- settleDurationMs?: number;
7
- waitForNetworkSettled?: boolean;
8
- };
9
- export declare function withWaitForCaptchaSolve<T>(callback: (page: Page) => Promise<T>, options?: WithWaitForCaptchaSolveOptions): Promise<T>;
10
- export declare function waitForCaptchaSolve(page: Page, { timeoutInMs, settlePeriodInMs, }?: {
11
- timeoutInMs?: number;
12
- settlePeriodInMs?: number;
13
- }): Promise<void>;
14
- export declare function removeCaptchaEventListener(page: Page, status: CaptchaStatus, f: (captcha: Captcha) => any): Promise<void>;
15
- export declare function onCaptchaEvent(page: Page, status: CaptchaStatus, f: CaptchaCallback): Promise<void>;
16
- export declare function onceCaptchaEvent(page: Page, status: CaptchaStatus, f: CaptchaCallback): Promise<void>;
17
- export declare function pauseCaptchaSolver(context: BrowserContext): Promise<void>;
18
- export declare function resumeCaptchaSolver(context: BrowserContext): Promise<void>;
@@ -1,190 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.onCaptchaEvent = onCaptchaEvent;
7
- exports.onceCaptchaEvent = onceCaptchaEvent;
8
- exports.pauseCaptchaSolver = pauseCaptchaSolver;
9
- exports.removeCaptchaEventListener = removeCaptchaEventListener;
10
- exports.resumeCaptchaSolver = resumeCaptchaSolver;
11
- exports.waitForCaptchaSolve = waitForCaptchaSolve;
12
- exports.withWaitForCaptchaSolve = withWaitForCaptchaSolve;
13
- var _intunedExtensionServer = require("../common/extension/intunedExtensionServer");
14
- var _extensionsHelpers = require("../common/extension/extensionsHelpers");
15
- async function withWaitForCaptchaSolve(callback, options) {
16
- const {
17
- page,
18
- timeoutInMs = 10_000,
19
- settleDurationMs = 5_000,
20
- waitForNetworkSettled = true
21
- } = options || {};
22
- const extensionServer = (0, _intunedExtensionServer.getIntunedExtensionServer)();
23
- let settledResolve = null;
24
- let settledPromise = new Promise(resolve => {
25
- settledResolve = resolve;
26
- });
27
- let isTimeout = false;
28
- let captchasAppeared = false;
29
- let hasError = false;
30
- let errorMessage = "";
31
- let actionDone = false;
32
- const getPendingCaptchas = async () => await extensionServer.getCaptchas(page, "solving");
33
- const hasNoPendingCaptchas = async () => !captchasAppeared || (await getPendingCaptchas()).length === 0;
34
- const maybeSettle = async () => {
35
- if (await hasNoPendingCaptchas()) {
36
- settledResolve?.();
37
- }
38
- };
39
- const onCaptchaUpdate = async _captcha => {
40
- const solvingCaptchas = await extensionServer.getCaptchas(page, "solving");
41
- const errorCaptchas = await extensionServer.getCaptchas(page, "error");
42
- if (solvingCaptchas.length > 0) {
43
- captchasAppeared = true;
44
- }
45
- if (errorCaptchas.length > 0) {
46
- hasError = true;
47
- const err = errorCaptchas[0].error;
48
- errorMessage = err?.code ?? "Unknown error during CAPTCHA solving.";
49
- await maybeSettle();
50
- return;
51
- }
52
- if (solvingCaptchas.length === 0) {
53
- await maybeSettle();
54
- }
55
- };
56
- const timeoutPromise = new Promise(resolve => {
57
- setTimeout(() => {
58
- isTimeout = true;
59
- settledResolve?.();
60
- resolve();
61
- }, timeoutInMs);
62
- });
63
- await extensionServer.subscribe(page, onCaptchaUpdate);
64
- try {
65
- const initialPending = await getPendingCaptchas();
66
- if (initialPending.length > 0) {
67
- captchasAppeared = true;
68
- }
69
- const initialErrors = await extensionServer.getCaptchas(page, "error");
70
- if (initialErrors.length > 0) {
71
- hasError = true;
72
- const err = initialErrors[0].error;
73
- errorMessage = err?.code ?? "Unknown error during CAPTCHA solving.";
74
- }
75
- const result = await callback(page);
76
- actionDone = true;
77
- if (waitForNetworkSettled) {
78
- try {
79
- await page.waitForLoadState("networkidle");
80
- } catch (_) {}
81
- }
82
- await maybeSettle();
83
- let shouldContinue = true;
84
- while (shouldContinue) {
85
- await Promise.race([settledPromise, timeoutPromise]);
86
- await new Promise(r => setTimeout(r, settleDurationMs));
87
- if (hasError) {
88
- throw new Error(`CAPTCHA Solve Error: ${errorMessage}`);
89
- }
90
- const noPendingCaptchas = await hasNoPendingCaptchas();
91
- if (actionDone && noPendingCaptchas || isTimeout) {
92
- if (isTimeout && !noPendingCaptchas) {
93
- throw new Error("CAPTCHA Solve timed out with pending captchas.");
94
- }
95
- shouldContinue = false;
96
- } else {
97
- settledPromise = new Promise(resolve => {
98
- settledResolve = resolve;
99
- });
100
- }
101
- }
102
- return result;
103
- } finally {
104
- await extensionServer.unsubscribe(page, onCaptchaUpdate);
105
- }
106
- }
107
- async function waitForCaptchaSolve(page, {
108
- timeoutInMs = 10_000,
109
- settlePeriodInMs = 5_000
110
- } = {}) {
111
- await withWaitForCaptchaSolve(async () => undefined, {
112
- page,
113
- timeoutInMs,
114
- settleDurationMs: settlePeriodInMs,
115
- waitForNetworkSettled: false
116
- });
117
- }
118
- async function removeCaptchaEventListener(page, status, f) {
119
- const extensionServer = (0, _intunedExtensionServer.getIntunedExtensionServer)();
120
- await extensionServer.unsubscribe(page, f, status);
121
- }
122
- async function onCaptchaEvent(page, status, f) {
123
- const extensionServer = (0, _intunedExtensionServer.getIntunedExtensionServer)();
124
- const wrapper = async captcha => {
125
- if (captcha.status === status) {
126
- await f(captcha);
127
- }
128
- };
129
- await extensionServer.subscribe(page, wrapper, status);
130
- }
131
- async function onceCaptchaEvent(page, status, f) {
132
- const extensionServer = (0, _intunedExtensionServer.getIntunedExtensionServer)();
133
- const oneTimeHandler = async captcha => {
134
- if (captcha.status === status) {
135
- await extensionServer.unsubscribe(page, oneTimeHandler, status);
136
- }
137
- await f(captcha);
138
- };
139
- await extensionServer.subscribe(page, oneTimeHandler, status);
140
- }
141
- async function setAutoSolve(context, enabled) {
142
- const worker = await (0, _extensionsHelpers.getIntunedExtensionWorker)(context);
143
- if (!worker) {
144
- if (!enabled) {
145
- return;
146
- }
147
- throw new Error("Intuned service worker not found");
148
- }
149
- await worker.evaluate(`
150
- (enabled) => new Promise((resolve, reject) => {
151
- const updateSettings = () => {
152
- if (chrome?.storage?.local) {
153
- chrome.storage.local.get('settings', (result) => {
154
- if (chrome.runtime.lastError) {
155
- reject(new Error(chrome.runtime.lastError.message));
156
- return;
157
- }
158
- const settings = result.settings || {};
159
- if (!settings.settings) {
160
- settings.settings = {};
161
- }
162
- settings.settings.autoSolve = enabled;
163
- chrome.storage.local.set({ settings }, () => {
164
- if (chrome.runtime.lastError) {
165
- reject(new Error(chrome.runtime.lastError.message));
166
- } else {
167
- resolve(true);
168
- }
169
- });
170
- });
171
- } else {
172
- setTimeout(updateSettings, 50);
173
- }
174
- };
175
- updateSettings();
176
- })
177
- `, enabled);
178
- }
179
- async function pauseCaptchaSolver(context) {
180
- if (!(0, _extensionsHelpers.isIntunedExtensionEnabled)()) {
181
- throw new Error("Intuned extension is not enabled. Cannot pause captcha solver.");
182
- }
183
- await setAutoSolve(context, false);
184
- }
185
- async function resumeCaptchaSolver(context) {
186
- if (!(0, _extensionsHelpers.isIntunedExtensionEnabled)()) {
187
- throw new Error("Intuned extension is not enabled. Cannot resume captcha solver.");
188
- }
189
- await setAutoSolve(context, true);
190
- }