UNPKG

blitzllama-js

Version:

Blitzllama JS Library

46 lines (45 loc) 2.24 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; const hasWindow = () => typeof window !== 'undefined'; export const checkReadyState = () => { if (hasWindow() && window.blitz) { return true; } return false; }; export const blitzCommand = (command, ...args) => __awaiter(void 0, void 0, void 0, function* () { if (hasWindow() && window.blitz) { const result = yield window.blitz(command, ...args); return result; } throw Error('Blitz is not available, make sure init function has been called.'); }); const appendScript = (scriptText, scriptId) => { try { const existingScript = document.getElementById(scriptId); const script = existingScript || document.createElement('script'); script.id = scriptId; script.innerText = scriptText; script.crossOrigin = 'anonymous'; document.head.appendChild(script); return true; } catch (_a) { return false; } }; export const init = (appKey) => { const scriptCode = `var exports = { __esModule: true };window._blitzQueue = window._blitzQueue || [];function blitz() {_blitzQueue.push(arguments);}(function () {var a = document.createElement('script');a.type = 'text/javascript';a.async = !0;a.src = 'https://cdn.blitzllama.com/js/blitz.js';var b = document.getElementsByTagName('script')[0];b.parentNode.insertBefore(a, b);})();blitz('init', "${appKey}");`; const isAppended = appendScript(scriptCode, 'blitz-script'); if (isAppended && checkReadyState()) { return; } throw Error('Failed to initialize Blitz'); };