-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtm-uvpi.js
More file actions
205 lines (165 loc) · 5.61 KB
/
tm-uvpi.js
File metadata and controls
205 lines (165 loc) · 5.61 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// ==UserScript==
// @name Unified Verint Product Injector (UVPI)
// @description This is a tampermonkey script to inject Verint products to any website.
// @author Daniel Kahl
// @version 1.4
// @match https://*/*
// @namespace http://tampermonkey.net/
// @source https://github.com/foreseecode/tm-scripts/blob/main/tm-uvpi.js
// @downloadURL https://raw.githubusercontent.com/foreseecode/tm-scripts/refs/heads/main/tm-uvpi.js
// @updateURL https://raw.githubusercontent.com/foreseecode/tm-scripts/refs/heads/main/tm-uvpi.js
// @grant none
// ==/UserScript==
const injector = createInjector();
const isIframe = window.top != window.self;
// START RULES
// Example Unified WebSDK:
// injector.rule(/blank.org/, "unified-websdk", { snippet: "v2", siteKey: "default", container: "draft", moduleHost: ucm("us") });
// Example IVA:
// injector.rule(/blank.org/, "iva", { token: "[PASTE TOKEN HERE]" });
// END RULES
// START SCRIPTS
injector.script("unified-websdk", {
defaultOptions: {
varName: "unifiedSDK",
container: "draft",
moduleHost: ucm("us"),
configHost: null, // null => use same as "moduleHost"
version: null,
snippet: "v2"
},
inject({ version, ...siteConfig }) {
siteConfig.loadTime = Date.now();
if (siteConfig.configHost === null) {
siteConfig.configHost = siteConfig.moduleHost;
}
if (siteConfig.snippet === "v3") {
const script = document.createElement("script");
const target = document.head || document.body;
script.type = "module";
script.async = true;
script.src = `//${siteConfig.moduleHost}/projects/${siteConfig.siteKey}/${siteConfig.container}/uws.js`;
script.setAttribute("data-uws", "3.0");
target.appendChild(script);
return;
}
const readyCallbacks = [];
let sdkPath = siteConfig.sdkPath;
if (!sdkPath) {
if (siteConfig.modulePath) {
sdkPath = buildPath(siteConfig.modulePath, { ...siteConfig, file: "sdk.js" });
} else if (version) {
sdkPath = `//${siteConfig.moduleHost}/files/modules/unified-websdk/${version}/sdk.js`;
} else {
sdkPath = `//${siteConfig.moduleHost}/files/sites/${siteConfig.siteKey}/${siteConfig.container}/sdk.js`;
}
}
window._vrntSdkInit = siteConfig;
window.uwsReady = (callback) => readyCallbacks.push(callback);
import(sdkPath).then(() => window[siteConfig.varName].start(siteConfig, readyCallbacks));
}
});
injector.script("iva", {
defaultOptions: {
domain: "https://messenger.ivastudio.verint.live",
port: "443",
token: null,
branch: "current"
},
inject({ domain, port, token, ...other }) {
window.ivasMessengerSettings = { domain, port, token, ...other };
const script = document.createElement("script");
script.type = "text/javascript";
script.async = true;
script.src = `${domain}:${port}/loader`;
const firstScript = document.getElementsByTagName("script")[0];
if (firstScript) {
firstScript.parentNode.insertBefore(script, firstScript);
} else {
document.head.appendChild(script);
}
}
});
// END SCRIPTS
injector.run();
// FUNCTIONS
function createInjector() {
const rules = [];
const scripts = {};
const injector = {};
injector.rule = function (url, script, options) {
rules.push({ url, script, options });
};
injector.script = function (name, { defaultOptions = {}, inject }) {
if (typeof inject !== "function") {
error(`Script "${name}" has no valid inject function.`);
return;
}
scripts[name] = { defaultOptions, inject };
};
injector.run = function () {
if (!isIframe) {
info(`Run Injection...`);
}
for (const rule of rules) {
if (!isIframe) debug("Check rule:", rule);
if (!rule?.url?.test(location.href)) {
continue;
}
const script = scripts[rule.script];
if (!script) {
error(`Script "${rule.script}" not registered.`);
continue;
}
// generate options object from default and rule options
const options = {
blockIframe: true,
...script.defaultOptions,
...rule.options
};
// block iframe when configured or cleanup from option as its not needed by the inject function
if (options.blockIframe && isIframe) return;
delete options.blockIframe;
success(`✅ Injecting "${rule.script}"...`);
script.inject(options);
}
};
return injector;
}
function ucm(region = "us", env = "prod", cdn = false) {
if (region === "local") return "localhost:8080";
const domain = cdn ? "verint-cdn.com" : "verint-api.com";
let subdomain = `ucm-${region}`;
if (env !== "prod") {
subdomain += `-${env}`;
}
return `${subdomain}.${domain}`;
}
function buildPath(pathTemplate, params) {
return pathTemplate.replace(/\{([a-z]+)\}/gi, ($0, $1) => {
return params[$1];
});
}
function warn(...args) { logFactory("warn", "yellow", ...args); }
function error(...args) { logFactory("error", "red", ...args); }
function info(...args) { logFactory("info", "cyan", ...args); }
function debug(...args) { logFactory("log", "gray", ...args); }
function success(...args) { logFactory("log", "green", ...args); }
function logFactory(level, color, msg, ...rest) {
const args = [];
if (isIframe) {
args.push(`%c[UVPI - IFRAME]%c ${msg}`);
} else {
args.push(`%c[UVPI] %c${msg}`);
}
args.push(`color: ${color}; font-weight: bold;`);
args.push(`color: ${color};`);
console[level](...args, ...rest);
}
/**
Updates:
1.3
- added support bor branch and other new IVA injection options
1.4
- adding support for Unified WebSDK Embed Snippet v3
*/