This repository has been archived by the owner on Oct 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
eggs.js
88 lines (73 loc) · 1.58 KB
/
eggs.js
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
const matchers = {
// Returns true if the seed includes all terms
all(terms, seed) {
for (const term of terms) {
if (!seed.includes(term)) {
return false;
}
}
return true;
},
// Returns true if the seed includes any term
any(terms, seed) {
for (const term of terms) {
if (seed.includes(term)) {
return true;
}
}
},
// Returns true if the seed is an exact match to any term
exact(terms, seed) {
for (const term of terms) {
if (term === seed) {
return true;
}
}
}
}
const mutations = {
// Sets the background image of the page
background(value) {
document.body.style.background = value;
},
// Adds a style element
css(value) {
const element = document.createElement('style');
element.innerText = value;
document.head.appendChild(element);
}
}
function findEgg(eggs) {
const seed = urlParams.get("seed").toLowerCase();
if (seed) {
for (const egg of eggs) {
if (!egg.match) continue;
for (const key of Object.keys(egg.match)) {
if (matchers.hasOwnProperty(key) && typeof matchers[key] === 'function') {
if (matchers[key](egg.match[key], seed) === true) {
handleEgg(egg);
}
}
}
}
}
}
function handleEgg(egg) {
if (egg.mutate) {
for (const key of Object.keys(egg.mutate)) {
if (mutations.hasOwnProperty(key) && typeof mutations[key] === 'function') {
mutations[key](egg.mutate[key]);
}
}
}
}
(async () => {
const response = await fetch('./eggs.json');
if (response.ok) {
const eggs = await response.json();
const egg = findEgg(eggs);
if (egg) {
handleEgg(egg);
}
}
})();