UNPKG

@eeaas/core

Version:

Easter eggs as a service

232 lines (231 loc) 7.92 kB
class y { patterns; buffer = []; isListening = !1; constructor(e) { this.patterns = e, this.handleKeyDown = this.handleKeyDown.bind(this); } isInputElement(e) { return e instanceof HTMLInputElement || e instanceof HTMLTextAreaElement || e instanceof HTMLSelectElement || e instanceof HTMLElement && e.isContentEditable; } handleKeyDown(e) { this.buffer.push(e.key.toLowerCase()); const s = Math.max(...this.patterns.map((i) => i.keystrokes.length)); this.buffer.length > s && (this.buffer = this.buffer.slice(-s)); const a = this.isInputElement(e.target); for (const i of this.patterns) if (!(!i.captureOnInputs && a) && (typeof i.onKeydown == "function" && i.onKeydown(e), this.checkMatch(i.keystrokes))) { i.callback(), this.buffer = []; break; } } checkMatch(e) { return this.buffer.length < e.length ? !1 : e.every((s, a) => this.buffer[this.buffer.length - e.length + a] === s.toLowerCase()); } start() { this.isListening || (document.addEventListener("keydown", this.handleKeyDown), this.isListening = !0); } stop() { document.removeEventListener("keydown", this.handleKeyDown), this.buffer = [], this.isListening = !1; } } const b = { force: "#2196f3", info: "#2196f3", warn: "#ff9800", error: "#d32f2f" }; class w { debug; constructor(e = !1) { this.debug = e; } shouldLog(e) { return typeof this.debug == "boolean" ? this.debug : Array.isArray(this.debug) ? this.debug.includes(e) : typeof this.debug == "string" ? this.debug === e : !1; } log(e, s, ...a) { if (!this.shouldLog(e) && e !== "error" && e !== "force") return; const u = `color: white; background-color: ${b[e]}; padding: 2px 5px; border-radius: 2px`; (e === "error" ? console.error : e === "warn" ? console.warn : console.info)(`%c${s}`, u, ...a); } force(e, ...s) { this.log("force", e, ...s); } info(e, ...s) { this.log("info", e, ...s); } warn(e, ...s) { this.log("warn", e, ...s); } error(e, ...s) { this.log("error", e, ...s); } } const L = ({ url: n, resource: e }) => { if (e.type === "css") { const a = Object.assign(document.createElement("link"), { href: n, rel: "stylesheet", id: e.id }); return { ...e, element: a }; } const s = Object.assign(document.createElement("script"), { src: n, id: e.id }); return { ...e, element: s }; }, h = ({ url: n, resource: e }) => new Promise((s, a) => { const i = L({ url: n, resource: e }); i.element.onload = () => { s(i); }, i.element.onerror = () => { i.element.remove(), a(new Error(`[eeaas] Failed to load resource (${e.type}: ${e.url})`)); }, document.head.appendChild(i.element); }), k = (n, e, s) => ({ ...e, id: `eeaas_${n.name}_${e.type}_${s}` }), R = (n) => !!(n.url && !n.content) || !!(!n.url && n.content), E = (n) => document.getElementById(n) !== null, T = async (n) => { if (n.url) return await h({ resource: n, url: n.url }); if (n.content) { const e = document.createElement("style"); return e.id = n.id, e.textContent = n.content, document.head.appendChild(e), { ...n, id: e.id, element: e }; } return null; }, S = async (n) => { if (n.url) return await h({ resource: n, url: n.url }); if (n.content) { const e = document.createElement("script"); return e.id = n.id, e.textContent = n.content, document.body.appendChild(e), { ...n, id: e.id, element: e }; } return null; }, I = async (n, e) => { if (!e.length) return []; const s = e.filter((r) => R(r) ? !0 : (n.error("eeaas", "Invalid resource! Must have either url or content."), !1)), a = s.filter((r) => r.type === "css"), i = s.filter((r) => r.type === "script"), u = [ ...a.map((r) => T(r)), ...i.map((r) => S(r)) ]; return (await Promise.all(u)).filter((r) => r !== null); }, $ = (n) => { n.length && n.forEach((e) => { const s = document.getElementById(e.id); s && s.remove(); }); }, A = ({ debug: n = !1 } = {}) => { const e = new w(n), s = {}, a = {}; return n && e.force("eeaas", "Debug mode enabled"), { register: (r) => { s[r.name] && (e.warn("eeaas", `Egg "${r.name}" is already registered, re-registering...`), a[r.name].disable()); const t = { name: r.name, trigger: r.trigger || { type: "manual" }, stopTrigger: r.stopTrigger || { type: "manual" }, onStart: r.onStart, onStop: r.onStop, allowMultipleInstances: r.allowMultipleInstances ?? !1, isEnabled: r.enabled ?? !0, isActivated: !1, resourcesToLoad: r.resources ? r.resources.map((o, d) => k(r, o, d)) : [], loadedResources: [] }; let c = null; const f = /* @__PURE__ */ new Set(), p = () => { for (const o of f) try { o(); } catch (d) { e.error("eeaas", `Error in listener for egg "${r.name}":`, d); } }, l = { name: t.name, get isEnabled() { return t.isEnabled; }, get isActivated() { return t.isActivated; }, get loadedResources() { return t.loadedResources; }, enable() { if (t.trigger.type === "keys" || t.stopTrigger.type === "keys") { const o = []; t.trigger.type === "keys" && o.push({ keystrokes: t.trigger.keystrokes, callback: () => l.start(), captureOnInputs: t.trigger.captureOnInputs ?? !0, onKeydown: t.trigger.onKeydown }), t.stopTrigger.type === "keys" && o.push({ keystrokes: t.stopTrigger.keystrokes, callback: () => l.stop(), captureOnInputs: t.stopTrigger.captureOnInputs ?? !0, onKeydown: t.stopTrigger.onKeydown }), c = new y(o), c.start(); } t.isEnabled = !0, p(), t.trigger.type === "auto" && l.start(); }, disable() { c && (c.stop(), c = null), t.isActivated && l.stop(), t.isEnabled = !1, p(); }, async start() { if (!t.isEnabled) { e.warn("eeaas", `Failed to start! Egg "${t.name}" is not enabled.`); return; } if (t.isActivated) { typeof t.onStop == "function" && !t.allowMultipleInstances && await t.onStop(t.loadedResources), typeof t.onStart == "function" && await t.onStart(t.loadedResources); return; } try { let o = []; if (t.resourcesToLoad && t.resourcesToLoad.length) { const d = t.resourcesToLoad.filter((m) => !E(m.id)); o = await I(e, d); } typeof t.onStart == "function" && await t.onStart(o), t.loadedResources = o, t.isActivated = !0, p(); } catch (o) { e.error("eeaas", `Error starting egg "${t.name}":`, o); } }, async stop() { if (t.isActivated) try { typeof t.onStop == "function" && await t.onStop(t.loadedResources), t.resourcesToLoad && t.resourcesToLoad.length && $(t.loadedResources), t.loadedResources = [], t.isActivated = !1, p(); } catch (o) { e.error("eeaas", `Error stopping egg "${t.name}":`, o); } }, subscribe(o) { return f.add(o), () => f.delete(o); }, unsubscribe(o) { f.delete(o); } }; return s[r.name] = t, a[r.name] = l, t.isEnabled && l.enable(), e.info("eeaas", `Registered egg "${r.name}"`), l; }, get: (r) => { const t = a[r]; if (!t) { e.warn("eeaas", `Egg "${r}" not found!`); return; } return t; }, getAll: () => Object.values(a) }; }; export { A as initializeEeaas }; //# sourceMappingURL=index.js.map