@deflectbot/deflect-sdk 1.1.3 → 1.1.4
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/package.json +1 -1
- package/src/index.ts +16 -9
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -22,35 +22,42 @@ class Deflect {
|
|
|
22
22
|
if (!window.Deflect.siteKey) {
|
|
23
23
|
throw new Error("API key (siteKey) is missing in configuration");
|
|
24
24
|
}
|
|
25
|
-
|
|
25
|
+
|
|
26
26
|
const scriptUrl = `https://js.deflect.bot/main.js?sitekey=${window.Deflect.siteKey}`;
|
|
27
|
-
|
|
27
|
+
|
|
28
28
|
const response = await fetch(scriptUrl);
|
|
29
29
|
if (!response.ok) {
|
|
30
30
|
throw new Error("Failed to fetch the Deflect script");
|
|
31
31
|
}
|
|
32
|
-
|
|
33
32
|
const sessionId = response.headers.get("session_id");
|
|
34
33
|
if (sessionId) {
|
|
35
34
|
window.Deflect.sessionId = sessionId;
|
|
36
35
|
}
|
|
37
|
-
|
|
38
|
-
const scriptText = await response.text();
|
|
39
|
-
|
|
36
|
+
|
|
40
37
|
const scriptEl = document.createElement("script");
|
|
41
|
-
scriptEl.
|
|
38
|
+
scriptEl.type = "module";
|
|
39
|
+
const blob = new Blob([await response.text()], { type: "text/javascript" });
|
|
40
|
+
const blobUrl = URL.createObjectURL(blob);
|
|
41
|
+
scriptEl.src = blobUrl;
|
|
42
|
+
|
|
42
43
|
document.head.appendChild(scriptEl);
|
|
43
|
-
|
|
44
|
+
|
|
45
|
+
await new Promise<void>((resolve, reject) => {
|
|
46
|
+
scriptEl.onload = () => resolve();
|
|
47
|
+
scriptEl.onerror = () => reject("Failed to load the Deflect script");
|
|
48
|
+
});
|
|
49
|
+
|
|
44
50
|
if (
|
|
45
51
|
typeof window.Deflect === "undefined" ||
|
|
46
52
|
typeof window.Deflect.getToken !== "function"
|
|
47
53
|
) {
|
|
48
54
|
throw new Error("Deflect script did not load properly");
|
|
49
55
|
}
|
|
50
|
-
|
|
56
|
+
|
|
51
57
|
const token = await window.Deflect.getToken();
|
|
52
58
|
return token;
|
|
53
59
|
}
|
|
60
|
+
|
|
54
61
|
}
|
|
55
62
|
|
|
56
63
|
export default new Deflect();
|