@deflectbot/deflect-sdk 1.2.1 → 1.2.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.
- package/dist/index.js +108 -196
- package/dist/types/index.d.ts +17 -28
- package/package.json +1 -1
- package/src/index.ts +91 -161
package/dist/index.js
CHANGED
|
@@ -1,196 +1,108 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
class Deflect {
|
|
11
|
-
constructor() {
|
|
12
|
-
this.
|
|
13
|
-
this.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (
|
|
26
|
-
return;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
sessionId
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
return URL.createObjectURL(blob);
|
|
110
|
-
}
|
|
111
|
-
loadScriptElement(blobUrl) {
|
|
112
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
113
|
-
return new Promise((resolve, reject) => {
|
|
114
|
-
const script = document.createElement("script");
|
|
115
|
-
script.type = "module";
|
|
116
|
-
script.src = blobUrl;
|
|
117
|
-
script.onload = () => resolve(script);
|
|
118
|
-
script.onerror = () => reject(new Error("Script failed to load"));
|
|
119
|
-
document.head.appendChild(script);
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
getTokenFromScript() {
|
|
124
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
125
|
-
var _a;
|
|
126
|
-
if (typeof window === "undefined" ||
|
|
127
|
-
typeof ((_a = window.Deflect) === null || _a === void 0 ? void 0 : _a.getToken) !== "function") {
|
|
128
|
-
throw new Error("Script did not load properly - getToken not available");
|
|
129
|
-
}
|
|
130
|
-
try {
|
|
131
|
-
return yield window.Deflect.getToken();
|
|
132
|
-
}
|
|
133
|
-
catch (error) {
|
|
134
|
-
throw new Error(`Script execution failed: ${error}`);
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
cleanup(blobUrl, scriptElement) {
|
|
139
|
-
var _a;
|
|
140
|
-
URL.revokeObjectURL(blobUrl);
|
|
141
|
-
scriptElement.remove();
|
|
142
|
-
if (typeof window !== "undefined" && ((_a = window.Deflect) === null || _a === void 0 ? void 0 : _a.getToken)) {
|
|
143
|
-
delete window.Deflect.getToken;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
configure(params) {
|
|
147
|
-
var _a;
|
|
148
|
-
if (!((_a = params.actionId) === null || _a === void 0 ? void 0 : _a.trim())) {
|
|
149
|
-
throw new Error("actionId is required and cannot be empty");
|
|
150
|
-
}
|
|
151
|
-
this.config = Object.assign({}, params);
|
|
152
|
-
if (typeof window !== "undefined") {
|
|
153
|
-
window.Deflect.actionId = params.actionId;
|
|
154
|
-
if (params.extraArgs) {
|
|
155
|
-
window.Deflect.extraArgs = Object.assign({}, params.extraArgs);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
this.tryWarmup();
|
|
159
|
-
}
|
|
160
|
-
solveChallenge() {
|
|
161
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
162
|
-
var _a;
|
|
163
|
-
if (!((_a = this.config) === null || _a === void 0 ? void 0 : _a.actionId)) {
|
|
164
|
-
throw new Error("Must call configure() before solveChallenge()");
|
|
165
|
-
}
|
|
166
|
-
let script;
|
|
167
|
-
if (this.scriptCache && !this.isWarmupInProgress) {
|
|
168
|
-
script = this.scriptCache;
|
|
169
|
-
this.scriptCache = null;
|
|
170
|
-
}
|
|
171
|
-
else {
|
|
172
|
-
script = yield this.fetchScript();
|
|
173
|
-
}
|
|
174
|
-
return this.executeScript(script);
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
warmup() {
|
|
178
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
179
|
-
var _a;
|
|
180
|
-
if (!((_a = this.config) === null || _a === void 0 ? void 0 : _a.actionId)) {
|
|
181
|
-
return false;
|
|
182
|
-
}
|
|
183
|
-
try {
|
|
184
|
-
yield this.tryWarmup();
|
|
185
|
-
return this.scriptCache !== null;
|
|
186
|
-
}
|
|
187
|
-
catch (_b) {
|
|
188
|
-
return false;
|
|
189
|
-
}
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
clearCache() {
|
|
193
|
-
this.scriptCache = null;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
export default new Deflect();
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
class Deflect {
|
|
11
|
+
constructor() {
|
|
12
|
+
this._prefetchedScriptText = null;
|
|
13
|
+
this._hasUsedPrefetch = false;
|
|
14
|
+
window.Deflect = window.Deflect || {};
|
|
15
|
+
window.Deflect.extraArgs = window.Deflect.extraArgs || {};
|
|
16
|
+
if (typeof window !== "undefined") {
|
|
17
|
+
window.addEventListener("load", () => {
|
|
18
|
+
setTimeout(() => {
|
|
19
|
+
this._warmupScript();
|
|
20
|
+
}, 500);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
_warmupScript() {
|
|
25
|
+
if (!window.Deflect.actionId)
|
|
26
|
+
return;
|
|
27
|
+
const nonce = Date.now().toString();
|
|
28
|
+
const scriptUrl = `https://js.deflect.bot/main.js?action_id=${window.Deflect.actionId}&_=${nonce}`;
|
|
29
|
+
fetch(scriptUrl, { cache: "no-store" })
|
|
30
|
+
.then((res) => __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
if (!res.ok)
|
|
32
|
+
return;
|
|
33
|
+
this._prefetchedScriptText = yield res.text();
|
|
34
|
+
}))
|
|
35
|
+
.catch(() => { });
|
|
36
|
+
}
|
|
37
|
+
setupReady() {
|
|
38
|
+
let resolveFn;
|
|
39
|
+
const promise = new Promise((resolve) => {
|
|
40
|
+
resolveFn = resolve;
|
|
41
|
+
});
|
|
42
|
+
window.Deflect.ready = {
|
|
43
|
+
promise,
|
|
44
|
+
resolve: () => resolveFn(),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
configure(params) {
|
|
48
|
+
if (!params.actionId) {
|
|
49
|
+
throw new Error("actionId is required in configuration");
|
|
50
|
+
}
|
|
51
|
+
window.Deflect.actionId = params.actionId;
|
|
52
|
+
}
|
|
53
|
+
solveChallenge() {
|
|
54
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
var _a;
|
|
56
|
+
if (!window.Deflect.actionId) {
|
|
57
|
+
throw new Error("actionId is missing in configuration");
|
|
58
|
+
}
|
|
59
|
+
this.setupReady();
|
|
60
|
+
let scriptText;
|
|
61
|
+
let sessionId = null;
|
|
62
|
+
if (this._prefetchedScriptText && !this._hasUsedPrefetch) {
|
|
63
|
+
scriptText = this._prefetchedScriptText;
|
|
64
|
+
this._hasUsedPrefetch = true;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
const nonce = Date.now().toString();
|
|
68
|
+
const scriptUrl = `https://js.deflect.bot/main.js?action_id=${window.Deflect.actionId}&_=${nonce}`;
|
|
69
|
+
const response = yield fetch(scriptUrl, { cache: "no-store" });
|
|
70
|
+
if (!response.ok) {
|
|
71
|
+
throw new Error("Failed to fetch the Deflect script");
|
|
72
|
+
}
|
|
73
|
+
sessionId = response.headers.get("session_id");
|
|
74
|
+
scriptText = yield response.text();
|
|
75
|
+
}
|
|
76
|
+
if (sessionId) {
|
|
77
|
+
window.Deflect.sessionId = sessionId;
|
|
78
|
+
}
|
|
79
|
+
const blob = new Blob([scriptText], { type: "text/javascript" });
|
|
80
|
+
const blobUrl = URL.createObjectURL(blob);
|
|
81
|
+
const scriptEl = document.createElement("script");
|
|
82
|
+
scriptEl.type = "module";
|
|
83
|
+
scriptEl.src = blobUrl;
|
|
84
|
+
document.head.appendChild(scriptEl);
|
|
85
|
+
yield new Promise((resolve, reject) => {
|
|
86
|
+
scriptEl.onload = () => resolve();
|
|
87
|
+
scriptEl.onerror = () => reject("Failed to load the Deflect script");
|
|
88
|
+
});
|
|
89
|
+
yield ((_a = window.Deflect.ready) === null || _a === void 0 ? void 0 : _a.promise);
|
|
90
|
+
if (typeof window.Deflect === "undefined" ||
|
|
91
|
+
typeof window.Deflect.getToken !== "function") {
|
|
92
|
+
throw new Error("Deflect script did not load properly");
|
|
93
|
+
}
|
|
94
|
+
let token;
|
|
95
|
+
try {
|
|
96
|
+
token = yield window.Deflect.getToken();
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
throw new Error(`Deflect script execution failed: ${error}`);
|
|
100
|
+
}
|
|
101
|
+
URL.revokeObjectURL(blobUrl);
|
|
102
|
+
scriptEl.remove();
|
|
103
|
+
delete window.Deflect.getToken;
|
|
104
|
+
return token;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
export default new Deflect();
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,28 +1,17 @@
|
|
|
1
|
-
interface DeflectConfig {
|
|
2
|
-
actionId: string;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
private
|
|
9
|
-
private
|
|
10
|
-
constructor();
|
|
11
|
-
private
|
|
12
|
-
private
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
private createScriptBlob;
|
|
19
|
-
private loadScriptElement;
|
|
20
|
-
private getTokenFromScript;
|
|
21
|
-
private cleanup;
|
|
22
|
-
configure(params: DeflectConfig): void;
|
|
23
|
-
solveChallenge(): Promise<string>;
|
|
24
|
-
warmup(): Promise<boolean>;
|
|
25
|
-
clearCache(): void;
|
|
26
|
-
}
|
|
27
|
-
declare const _default: Deflect;
|
|
28
|
-
export default _default;
|
|
1
|
+
interface DeflectConfig {
|
|
2
|
+
actionId: string;
|
|
3
|
+
extraArgs?: {
|
|
4
|
+
[key: string]: string;
|
|
5
|
+
};
|
|
6
|
+
}
|
|
7
|
+
declare class Deflect {
|
|
8
|
+
private _prefetchedScriptText;
|
|
9
|
+
private _hasUsedPrefetch;
|
|
10
|
+
constructor();
|
|
11
|
+
private _warmupScript;
|
|
12
|
+
private setupReady;
|
|
13
|
+
configure(params: DeflectConfig): void;
|
|
14
|
+
solveChallenge(): Promise<string>;
|
|
15
|
+
}
|
|
16
|
+
declare const _default: Deflect;
|
|
17
|
+
export default _default;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,217 +1,147 @@
|
|
|
1
1
|
interface DeflectConfig {
|
|
2
2
|
actionId: string;
|
|
3
3
|
scriptUrl?: string;
|
|
4
|
-
extraArgs?:
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
interface DeflectScript {
|
|
8
|
-
sessionId?: string;
|
|
9
|
-
content: string;
|
|
4
|
+
extraArgs?: { [key: string]: string };
|
|
10
5
|
}
|
|
11
6
|
|
|
12
7
|
class Deflect {
|
|
13
|
-
private
|
|
14
|
-
private
|
|
15
|
-
private
|
|
8
|
+
private _prefetchedScriptText: string | null = null;
|
|
9
|
+
private _hasUsedPrefetch = false;
|
|
10
|
+
private _customScriptUrl: string | null = null;
|
|
16
11
|
|
|
17
12
|
constructor() {
|
|
18
|
-
this.initializeGlobalState();
|
|
19
|
-
this.setupAutomaticWarmup();
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
private initializeGlobalState(): void {
|
|
23
|
-
if (typeof window === "undefined") return;
|
|
24
|
-
|
|
25
13
|
window.Deflect = window.Deflect || {};
|
|
26
14
|
window.Deflect.extraArgs = window.Deflect.extraArgs || {};
|
|
27
|
-
}
|
|
28
15
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
setTimeout(() => this.tryWarmup(), 100);
|
|
16
|
+
if (typeof window !== "undefined") {
|
|
17
|
+
window.addEventListener("load", () => {
|
|
18
|
+
setTimeout(() => {
|
|
19
|
+
this._warmupScript();
|
|
20
|
+
}, 500);
|
|
21
|
+
});
|
|
36
22
|
}
|
|
37
23
|
}
|
|
38
24
|
|
|
39
|
-
private
|
|
40
|
-
|
|
41
|
-
|
|
25
|
+
private _getScriptUrl(): string {
|
|
26
|
+
const nonce = Date.now().toString();
|
|
27
|
+
if (this._customScriptUrl) {
|
|
28
|
+
const separator = this._customScriptUrl.includes("?") ? "&" : "?";
|
|
29
|
+
return `${this._customScriptUrl}${separator}action_id=${window.Deflect.actionId}&_=${nonce}`;
|
|
42
30
|
}
|
|
31
|
+
return `https://js.deflect.bot/main.js?action_id=${window.Deflect.actionId}&_=${nonce}`;
|
|
32
|
+
}
|
|
43
33
|
|
|
44
|
-
|
|
34
|
+
private _warmupScript(): void {
|
|
35
|
+
if (!window.Deflect.actionId) return;
|
|
45
36
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
37
|
+
const scriptUrl = this._getScriptUrl();
|
|
38
|
+
fetch(scriptUrl, { cache: "no-store" })
|
|
39
|
+
.then(async (res) => {
|
|
40
|
+
if (!res.ok) return;
|
|
41
|
+
this._prefetchedScriptText = await res.text();
|
|
42
|
+
})
|
|
43
|
+
.catch(() => {});
|
|
53
44
|
}
|
|
54
45
|
|
|
55
|
-
private
|
|
56
|
-
|
|
46
|
+
private _refetchScript(): void {
|
|
47
|
+
this._hasUsedPrefetch = false;
|
|
48
|
+
this._prefetchedScriptText = null;
|
|
57
49
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
50
|
+
setTimeout(() => {
|
|
51
|
+
this._warmupScript();
|
|
52
|
+
}, 100);
|
|
53
|
+
}
|
|
61
54
|
|
|
62
|
-
|
|
63
|
-
|
|
55
|
+
private setupReady(): void {
|
|
56
|
+
let resolveFn: () => void;
|
|
57
|
+
const promise = new Promise<void>((resolve) => {
|
|
58
|
+
resolveFn = resolve;
|
|
59
|
+
});
|
|
60
|
+
window.Deflect.ready = {
|
|
61
|
+
promise,
|
|
62
|
+
resolve: () => resolveFn(),
|
|
63
|
+
};
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
|
|
67
|
-
if (!
|
|
68
|
-
throw new Error("actionId is required");
|
|
66
|
+
configure(params: DeflectConfig): void {
|
|
67
|
+
if (!params.actionId) {
|
|
68
|
+
throw new Error("actionId is required in configuration");
|
|
69
69
|
}
|
|
70
|
+
window.Deflect.actionId = params.actionId;
|
|
70
71
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (!response.ok) {
|
|
75
|
-
throw new Error(`Failed to fetch script: ${response.status}`);
|
|
72
|
+
if (params.scriptUrl) {
|
|
73
|
+
this._customScriptUrl = params.scriptUrl;
|
|
76
74
|
}
|
|
77
|
-
|
|
78
|
-
return {
|
|
79
|
-
content: await response.text(),
|
|
80
|
-
sessionId: response.headers.get("session_id") || undefined,
|
|
81
|
-
};
|
|
82
75
|
}
|
|
83
76
|
|
|
84
|
-
|
|
85
|
-
if (
|
|
86
|
-
|
|
77
|
+
async solveChallenge(): Promise<string> {
|
|
78
|
+
if (!window.Deflect.actionId) {
|
|
79
|
+
throw new Error("actionId is missing in configuration");
|
|
87
80
|
}
|
|
88
81
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const blobUrl = this.createScriptBlob(script.content);
|
|
92
|
-
const scriptElement = await this.loadScriptElement(blobUrl);
|
|
82
|
+
this.setupReady();
|
|
93
83
|
|
|
94
|
-
|
|
95
|
-
|
|
84
|
+
let scriptText: string;
|
|
85
|
+
let sessionId: string | null = null;
|
|
96
86
|
|
|
97
|
-
|
|
87
|
+
if (this._prefetchedScriptText && !this._hasUsedPrefetch) {
|
|
88
|
+
scriptText = this._prefetchedScriptText;
|
|
89
|
+
this._hasUsedPrefetch = true;
|
|
90
|
+
} else {
|
|
91
|
+
const scriptUrl = this._getScriptUrl();
|
|
92
|
+
const response = await fetch(scriptUrl, { cache: "no-store" });
|
|
98
93
|
|
|
99
|
-
|
|
94
|
+
if (!response.ok) {
|
|
95
|
+
throw new Error("Failed to fetch the Deflect script");
|
|
96
|
+
}
|
|
100
97
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
this.cleanup(blobUrl, scriptElement);
|
|
98
|
+
sessionId = response.headers.get("session_id");
|
|
99
|
+
scriptText = await response.text();
|
|
104
100
|
}
|
|
105
|
-
}
|
|
106
101
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
102
|
+
if (sessionId) {
|
|
103
|
+
window.Deflect.sessionId = sessionId;
|
|
104
|
+
}
|
|
110
105
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
if (typeof window !== "undefined") {
|
|
114
|
-
window.Deflect.ready = {
|
|
115
|
-
promise: new Promise<void>((innerResolve) => {
|
|
116
|
-
window.Deflect.ready.resolve = innerResolve;
|
|
117
|
-
}),
|
|
118
|
-
resolve: () => resolve(),
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
}
|
|
106
|
+
const blob = new Blob([scriptText], { type: "text/javascript" });
|
|
107
|
+
const blobUrl = URL.createObjectURL(blob);
|
|
123
108
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
109
|
+
const scriptEl = document.createElement("script");
|
|
110
|
+
scriptEl.type = "module";
|
|
111
|
+
scriptEl.src = blobUrl;
|
|
128
112
|
|
|
129
|
-
|
|
130
|
-
return new Promise((resolve, reject) => {
|
|
131
|
-
const script = document.createElement("script");
|
|
132
|
-
script.type = "module";
|
|
133
|
-
script.src = blobUrl;
|
|
113
|
+
document.head.appendChild(scriptEl);
|
|
134
114
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
document.head.appendChild(script);
|
|
115
|
+
await new Promise<void>((resolve, reject) => {
|
|
116
|
+
scriptEl.onload = () => resolve();
|
|
117
|
+
scriptEl.onerror = () => reject("Failed to load the Deflect script");
|
|
139
118
|
});
|
|
140
|
-
}
|
|
141
119
|
|
|
142
|
-
|
|
120
|
+
await window.Deflect.ready?.promise;
|
|
121
|
+
|
|
143
122
|
if (
|
|
144
|
-
typeof window === "undefined" ||
|
|
145
|
-
typeof window.Deflect
|
|
123
|
+
typeof window.Deflect === "undefined" ||
|
|
124
|
+
typeof window.Deflect.getToken !== "function"
|
|
146
125
|
) {
|
|
147
|
-
throw new Error("
|
|
126
|
+
throw new Error("Deflect script did not load properly");
|
|
148
127
|
}
|
|
149
128
|
|
|
129
|
+
let token: string;
|
|
130
|
+
|
|
150
131
|
try {
|
|
151
|
-
|
|
132
|
+
token = await window.Deflect.getToken();
|
|
152
133
|
} catch (error) {
|
|
153
|
-
throw new Error(`
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
private cleanup(blobUrl: string, scriptElement: HTMLScriptElement): void {
|
|
158
|
-
URL.revokeObjectURL(blobUrl);
|
|
159
|
-
scriptElement.remove();
|
|
160
|
-
|
|
161
|
-
if (typeof window !== "undefined" && window.Deflect?.getToken) {
|
|
162
|
-
delete window.Deflect.getToken;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
public configure(params: DeflectConfig): void {
|
|
167
|
-
if (!params.actionId?.trim()) {
|
|
168
|
-
throw new Error("actionId is required and cannot be empty");
|
|
134
|
+
throw new Error(`Deflect script execution failed: ${error}`);
|
|
169
135
|
}
|
|
170
136
|
|
|
171
|
-
this.
|
|
172
|
-
|
|
173
|
-
if (typeof window !== "undefined") {
|
|
174
|
-
window.Deflect.actionId = params.actionId;
|
|
175
|
-
if (params.extraArgs) {
|
|
176
|
-
window.Deflect.extraArgs = { ...params.extraArgs };
|
|
177
|
-
}
|
|
178
|
-
}
|
|
137
|
+
this._refetchScript();
|
|
179
138
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
public async solveChallenge(): Promise<string> {
|
|
184
|
-
if (!this.config?.actionId) {
|
|
185
|
-
throw new Error("Must call configure() before solveChallenge()");
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
let script: DeflectScript;
|
|
189
|
-
|
|
190
|
-
if (this.scriptCache && !this.isWarmupInProgress) {
|
|
191
|
-
script = this.scriptCache;
|
|
192
|
-
this.scriptCache = null;
|
|
193
|
-
} else {
|
|
194
|
-
script = await this.fetchScript();
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
return this.executeScript(script);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
public async warmup(): Promise<boolean> {
|
|
201
|
-
if (!this.config?.actionId) {
|
|
202
|
-
return false;
|
|
203
|
-
}
|
|
139
|
+
URL.revokeObjectURL(blobUrl);
|
|
140
|
+
scriptEl.remove();
|
|
204
141
|
|
|
205
|
-
|
|
206
|
-
await this.tryWarmup();
|
|
207
|
-
return this.scriptCache !== null;
|
|
208
|
-
} catch {
|
|
209
|
-
return false;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
142
|
+
delete window.Deflect.getToken;
|
|
212
143
|
|
|
213
|
-
|
|
214
|
-
this.scriptCache = null;
|
|
144
|
+
return token;
|
|
215
145
|
}
|
|
216
146
|
}
|
|
217
147
|
|