@deflectbot/deflect-sdk 1.1.11 → 1.1.13

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