@eeaas/core
Version:
Easter eggs as a service
235 lines (234 loc) • 8.12 kB
JavaScript
var b = Object.defineProperty;
var w = (n, e, s) => e in n ? b(n, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : n[e] = s;
var u = (n, e, s) => w(n, typeof e != "symbol" ? e + "" : e, s);
class L {
constructor(e) {
u(this, "patterns");
u(this, "buffer", []);
u(this, "isListening", !1);
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 k = {
info: "#2196f3",
success: "#26c281",
warn: "#ff9800",
error: "#d32f2f"
};
class R {
constructor(e = !1) {
u(this, "debug");
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")
return;
const f = `color: white; background-color: ${k[e]}; padding: 2px 5px; border-radius: 2px`;
(e === "error" ? console.error : e === "warn" ? console.warn : console.info)(`%c${s}`, f, ...a);
}
info(e, ...s) {
this.log("info", e, ...s);
}
success(e, ...s) {
this.log("success", e, ...s);
}
warn(e, ...s) {
this.log("warn", e, ...s);
}
error(e, ...s) {
this.log("error", e, ...s);
}
}
const E = ({ 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
};
}, m = ({ url: n, resource: e }) => new Promise((s, a) => {
const i = E({ 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);
}), T = (n, e, s) => ({
...e,
id: `eeaas_${n.name}_${e.type}_${s}`
}), S = (n) => !!(n.url && !n.content) || !!(!n.url && n.content), I = (n) => document.getElementById(n) !== null, $ = async (n) => {
if (n.url)
return await m({ 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;
}, v = async (n) => {
if (n.url)
return await m({ 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;
}, A = async (n, e) => {
if (!e.length)
return [];
const s = e.filter((r) => S(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"), f = [
...a.map((r) => $(r)),
...i.map((r) => v(r))
];
return (await Promise.all(f)).filter((r) => r !== null);
}, K = (n) => {
n.length && n.forEach((e) => {
const s = document.getElementById(e.id);
s && s.remove();
});
}, C = ({ debug: n = !1 } = {}) => {
const e = new R(n), s = {}, a = {};
return {
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) => T(r, o, d)) : [],
loadedResources: []
};
let c = null;
const p = /* @__PURE__ */ new Set(), g = () => {
for (const o of p)
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 L(o), c.start();
}
t.isEnabled = !0, g(), t.trigger.type === "auto" && l.start();
},
disable() {
c && (c.stop(), c = null), t.isActivated && l.stop(), t.isEnabled = !1, g();
},
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((y) => !I(y.id));
o = await A(e, d);
}
typeof t.onStart == "function" && await t.onStart(o), t.loadedResources = o, t.isActivated = !0, g();
} 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 && K(t.loadedResources), t.loadedResources = [], t.isActivated = !1, g();
} catch (o) {
e.error("eeaas", `Error stopping egg "${t.name}":`, o);
}
},
subscribe(o) {
return p.add(o), () => p.delete(o);
},
unsubscribe(o) {
p.delete(o);
}
};
s[r.name] = t, a[r.name] = l, t.isEnabled && l.enable(), e.info("eeaas", `Registered egg "${r.name}"`);
},
get: (r) => {
const t = a[r];
if (!t) {
e.warn("eeaas", `Egg "${r}" not found!`);
return;
}
return t;
},
getAll: () => Object.values(a)
};
};
export {
C as initializeEeaas
};
//# sourceMappingURL=index.js.map