-
Notifications
You must be signed in to change notification settings - Fork 1
/
h.js
131 lines (124 loc) · 4.46 KB
/
h.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
(() => {
const e = "bp-web-widget",
t = {};
function n(t) {
return t ? `${t}-container` : e
}
function o(e) {
return e || "bp-widget"
}
function i(e, t, n = {}) {
const o = document.createElement(e);
Object.entries(n).forEach((([e, t]) => o[e] = t));
const i = document.querySelector(t);
if (!i) throw new Error(`No element correspond to ${t}`);
return i.appendChild(o), o
}
function r(e, t) {
const n = `bp-chat-key-${t.clientId}`;
let i = localStorage.getItem(n);
i || (i = function (e) {
const t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let n = "";
for (let o = 0; o < e; o++) n += t.charAt(Math.floor(Math.random() * t.length));
return n
}(32), localStorage.setItem(n, i));
const r = encodeURIComponent(JSON.stringify({
config: Object.assign(Object.assign({}, t), {
encryptionKey: i
})
})),
a = encodeURIComponent(t.botConversationDescription || t.botName || "Chatbot"),
c = e + "/index.html?options=" + r;
return `<iframe id="${o(t.chatId)}" title="${a}" frameborder="0" src="${c}" class="bp-widget-web"/>`
}
function a(e, t) {
return new Proxy(t, {
get: (t, n) => t[n] ? t[n] : "iframeWindow" === n ? () => {
console.warn(`No webchat with id ${e} has been initialized. \n Please use window.botpressWebChat.init first.`)
} : "eventListener" === n ? {
handler: () => {},
types: []
} : void 0,
set: (e, t, n) => (e[t] = n, !0)
})
}
function c(n) {
return t[n = n || e]
}
function s(e, t) {
return document.querySelector(`#${e} #${t}`)
}
window.addEventListener("message", (function ({
data: e
}) {
var t, i, r;
if (! function (e) {
return e && "string" == typeof e.type && "string" == typeof e.chatId
}(e)) return;
if ("UI.RESIZE" === e.type) {
const t = "number" == typeof e.value ? e.value + "px" : e.value;
s(n(e.chatId), o(e.chatId)).style.width = t
}
if ("UI.SET-CLASS" === e.type) {
s(n(e.chatId), o(e.chatId)).setAttribute("class", e.value)
}
const a = c(e.chatId);
a && (null === (t = a.eventListener.topics) || void 0 === t ? void 0 : t.some((t => "*" === t || t === e.type))) && (null === (r = (i = a.eventListener).handler) || void 0 === r || r.call(i, e))
})), window.botpressWebChat = {
init: function (c, d) {
d = d || "body", c.chatId = c.chatId || e;
const f = c.hostUrl || "";
i("link", "head", {
rel: "stylesheet",
href: `${f}/inject.css`
});
const l = r(f, c),
u = n(c.chatId),
h = o(c.chatId);
i("div", d, {
id: u,
innerHTML: l
});
const p = {
iframeWindow: s(u, h).contentWindow
};
t[c.chatId] ? Object.assign(t[c.chatId], p) : t[c.chatId] = a(c.chatId, p)
},
configure: function (e, t) {
c(t).iframeWindow.postMessage({
action: "configure",
payload: e
}, "*")
},
sendEvent: function (e, t) {
c(t).iframeWindow.postMessage({
action: "event",
payload: e
}, "*")
},
mergeConfig: function (e, t) {
c(t).iframeWindow.postMessage({
action: "mergeConfig",
payload: e
}, "*")
},
sendPayload: function (e, t) {
c(t).iframeWindow.postMessage({
action: "sendPayload",
payload: e
}, "*")
},
onEvent: function (n, o = [], i) {
if ("function" != typeof n) throw new Error("EventHandler is not a function, please provide a function");
if (!Array.isArray(o)) throw new Error("Topics should be an array of supported event types");
const r = {
eventListener: {
handler: n,
topics: o
}
};
t[i = i || e] ? Object.assign(t[i], r) : t[i] = a(i, r)
}
}
})();