messaggera
Version:
This script allows you to easily integrate a chat widget into your website. It dynamically loads the necessary resources and initializes the chat widget for your website. ## Installation
90 lines (89 loc) • 3.52 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Messaggera = void 0;
// @ts-nocheck
var scriptElementId = 'messaggera-script';
var Messaggera = function (websiteId, userData) {
console.log('messaggera script called');
if (!websiteId || typeof websiteId !== "string" || !/^[a-zA-Z0-9-_]+$/.test(websiteId)) {
console.error("Invalid Website ID.");
return;
}
if (userData) {
if (typeof userData !== "object" || Array.isArray(userData) || Object.keys(userData).length === 0) {
throw new Error("User data must be a non-empty object.");
}
var id = userData.id, email = userData.email, optionalData = __rest(userData, ["id", "email"]);
if (!id || typeof id !== "string") {
throw new Error("The 'id' field is required and must be a string.");
}
if (!email || typeof email !== "string" || !/\S+@\S+\.\S+/.test(email)) {
throw new Error("The 'email' field is required and must be a valid email address.");
}
window.$takiChat = [];
window.$takiChat.push(__assign({ id: id, email: email }, optionalData));
}
if (typeof window !== 'undefined') {
window.WEBSITE_ID = websiteId;
init();
}
};
exports.Messaggera = Messaggera;
var init = function () {
if (isDocumentReady()) {
addScriptToPage();
}
else {
document.addEventListener('readystatechange', handleReadyStateChange);
window.addEventListener('load', addScriptToPage, { once: true });
}
};
var handleReadyStateChange = function () {
if (isDocumentReady()) {
document.removeEventListener('readystatechange', handleReadyStateChange);
addScriptToPage();
}
};
var addScriptToPage = function () {
if (document.getElementById(scriptElementId))
return;
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.id = scriptElementId;
script.src = "https://api.messaggera.com/api/owner/websites/".concat(window.WEBSITE_ID, "/check?q=").concat(new Date().getTime());
var firstScript = document.getElementsByTagName('script')[0];
if (firstScript && firstScript.parentNode) {
firstScript.parentNode.insertBefore(script, firstScript);
}
};
var isDocumentReady = function () {
return document.readyState === 'complete' || document.readyState === 'interactive';
};
// Attach Messaggera to the window object for browser usage
if (typeof window !== 'undefined') {
window.Messaggera = exports.Messaggera;
}
exports.default = exports.Messaggera;