@deflectbot/deflect-sdk 1.1.9 → 1.1.12

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 CHANGED
@@ -9,9 +9,22 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  class Deflect {
11
11
  constructor() {
12
- this._prefetchedBlobUrl = null;
13
12
  window.Deflect = window.Deflect || {};
14
13
  window.Deflect.extraArgs = window.Deflect.extraArgs || {};
14
+ if (typeof window !== "undefined") {
15
+ window.addEventListener("load", () => {
16
+ setTimeout(() => {
17
+ this._warmupScript();
18
+ }, 500);
19
+ });
20
+ }
21
+ }
22
+ _warmupScript() {
23
+ if (!window.Deflect.siteKey)
24
+ return;
25
+ const nonce = Date.now().toString();
26
+ const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
27
+ fetch(scriptUrl, { cache: "no-store", method: "GET" }).catch(() => { });
15
28
  }
16
29
  setupReady() {
17
30
  let resolveFn;
@@ -28,21 +41,7 @@ class Deflect {
28
41
  throw new Error("siteKey is required in configuration");
29
42
  }
30
43
  window.Deflect.siteKey = params.siteKey;
31
- }
32
- prefetchScript() {
33
- if (!window.Deflect.siteKey || this._prefetchedBlobUrl)
34
- return;
35
- const nonce = Date.now().toString();
36
- const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
37
- fetch(scriptUrl, { cache: "no-store" })
38
- .then((res) => __awaiter(this, void 0, void 0, function* () {
39
- if (!res.ok)
40
- return;
41
- const text = yield res.text();
42
- const blob = new Blob([text], { type: "text/javascript" });
43
- this._prefetchedBlobUrl = URL.createObjectURL(blob);
44
- }))
45
- .catch(() => { });
44
+ console.log("Deflect configured with siteKey");
46
45
  }
47
46
  solveChallenge() {
48
47
  return __awaiter(this, void 0, void 0, function* () {
@@ -51,26 +50,19 @@ class Deflect {
51
50
  throw new Error("API key (siteKey) is missing in configuration");
52
51
  }
53
52
  this.setupReady();
54
- let blobUrl;
55
- let sessionId = null;
56
- if (this._prefetchedBlobUrl) {
57
- blobUrl = this._prefetchedBlobUrl;
58
- }
59
- else {
60
- const nonce = Date.now().toString();
61
- const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
62
- const response = yield fetch(scriptUrl, { cache: "no-store" });
63
- if (!response.ok) {
64
- throw new Error("Failed to fetch the Deflect script");
65
- }
66
- sessionId = response.headers.get("session_id");
67
- const text = yield response.text();
68
- const blob = new Blob([text], { type: "text/javascript" });
69
- blobUrl = URL.createObjectURL(blob);
53
+ const nonce = Date.now().toString();
54
+ const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
55
+ const response = yield fetch(scriptUrl, { cache: "no-store" });
56
+ if (!response.ok) {
57
+ throw new Error("Failed to fetch the Deflect script");
70
58
  }
59
+ const sessionId = response.headers.get("session_id");
71
60
  if (sessionId) {
72
61
  window.Deflect.sessionId = sessionId;
73
62
  }
63
+ const text = yield response.text();
64
+ const blob = new Blob([text], { type: "text/javascript" });
65
+ const blobUrl = URL.createObjectURL(blob);
74
66
  const scriptEl = document.createElement("script");
75
67
  scriptEl.type = "module";
76
68
  scriptEl.src = blobUrl;
@@ -87,7 +79,6 @@ class Deflect {
87
79
  const token = yield window.Deflect.getToken();
88
80
  URL.revokeObjectURL(blobUrl);
89
81
  scriptEl.remove();
90
- this._prefetchedBlobUrl = null;
91
82
  delete window.Deflect.getToken;
92
83
  return token;
93
84
  });
@@ -5,11 +5,10 @@ interface DeflectConfig {
5
5
  };
6
6
  }
7
7
  declare class Deflect {
8
- private _prefetchedBlobUrl;
9
8
  constructor();
9
+ private _warmupScript;
10
10
  private setupReady;
11
11
  configure(params: DeflectConfig): void;
12
- prefetchScript(): void;
13
12
  solveChallenge(): Promise<string>;
14
13
  }
15
14
  declare const _default: Deflect;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deflectbot/deflect-sdk",
3
- "version": "1.1.9",
3
+ "version": "1.1.12",
4
4
  "description": "SDK for deflect.bot - Use it for seamless captcha integration on any website.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/types/index.d.ts",
package/src/index.ts CHANGED
@@ -4,11 +4,25 @@ interface DeflectConfig {
4
4
  }
5
5
 
6
6
  class Deflect {
7
- private _prefetchedBlobUrl: string | null = null;
8
-
9
7
  constructor() {
10
8
  window.Deflect = window.Deflect || {};
11
9
  window.Deflect.extraArgs = window.Deflect.extraArgs || {};
10
+
11
+ if (typeof window !== "undefined") {
12
+ window.addEventListener("load", () => {
13
+ setTimeout(() => {
14
+ this._warmupScript();
15
+ }, 500);
16
+ });
17
+ }
18
+ }
19
+
20
+ private _warmupScript(): void {
21
+ if (!window.Deflect.siteKey) return;
22
+
23
+ const nonce = Date.now().toString();
24
+ const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
25
+ fetch(scriptUrl, { cache: "no-store", method: "GET" }).catch(() => {});
12
26
  }
13
27
 
14
28
  private setupReady(): void {
@@ -27,22 +41,7 @@ class Deflect {
27
41
  throw new Error("siteKey is required in configuration");
28
42
  }
29
43
  window.Deflect.siteKey = params.siteKey;
30
- }
31
-
32
- prefetchScript(): void {
33
- if (!window.Deflect.siteKey || this._prefetchedBlobUrl) return;
34
-
35
- const nonce = Date.now().toString();
36
- const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
37
-
38
- fetch(scriptUrl, { cache: "no-store" })
39
- .then(async (res) => {
40
- if (!res.ok) return;
41
- const text = await res.text();
42
- const blob = new Blob([text], { type: "text/javascript" });
43
- this._prefetchedBlobUrl = URL.createObjectURL(blob);
44
- })
45
- .catch(() => {});
44
+ console.log("Deflect configured with siteKey");
46
45
  }
47
46
 
48
47
  async solveChallenge(): Promise<string> {
@@ -52,30 +51,23 @@ class Deflect {
52
51
 
53
52
  this.setupReady();
54
53
 
55
- let blobUrl: string;
56
- let sessionId: string | null = null;
57
-
58
- if (this._prefetchedBlobUrl) {
59
- blobUrl = this._prefetchedBlobUrl;
60
- } else {
61
- const nonce = Date.now().toString();
62
- const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
63
- const response = await fetch(scriptUrl, { cache: "no-store" });
64
-
65
- if (!response.ok) {
66
- throw new Error("Failed to fetch the Deflect script");
67
- }
54
+ const nonce = Date.now().toString();
55
+ const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}&_=${nonce}`;
56
+ const response = await fetch(scriptUrl, { cache: "no-store" });
68
57
 
69
- sessionId = response.headers.get("session_id");
70
- const text = await response.text();
71
- const blob = new Blob([text], { type: "text/javascript" });
72
- blobUrl = URL.createObjectURL(blob);
58
+ if (!response.ok) {
59
+ throw new Error("Failed to fetch the Deflect script");
73
60
  }
74
61
 
62
+ const sessionId = response.headers.get("session_id");
75
63
  if (sessionId) {
76
64
  window.Deflect.sessionId = sessionId;
77
65
  }
78
66
 
67
+ const text = await response.text();
68
+ const blob = new Blob([text], { type: "text/javascript" });
69
+ const blobUrl = URL.createObjectURL(blob);
70
+
79
71
  const scriptEl = document.createElement("script");
80
72
  scriptEl.type = "module";
81
73
  scriptEl.src = blobUrl;
@@ -100,7 +92,6 @@ class Deflect {
100
92
 
101
93
  URL.revokeObjectURL(blobUrl);
102
94
  scriptEl.remove();
103
- this._prefetchedBlobUrl = null;
104
95
 
105
96
  delete window.Deflect.getToken;
106
97