forked from knauz/knauz.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parfumo.html
216 lines (174 loc) · 6.66 KB
/
parfumo.html
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8" />
<title>Parfumo Sammlungs-Vergleich</title>
</head>
<script>
async function fetchUrl(url) {
const response = await fetch(
"https://parfumo-corsanywhere.herokuapp.com/" + url,
{ credentials: "include", redirect: "manual" }
);
console.log("Fetched", { url, response });
return response;
}
async function fetchAndParse(url) {
const response = await fetchUrl(url);
const html = await response.text();
const parsed = new DOMParser().parseFromString(html, "text/html");
return parsed;
}
async function getSammlungMeta(sammlungUrl) {
const response = await fetchUrl(sammlungUrl);
const html = await response.text();
const parsed = new DOMParser().parseFromString(html, "text/html");
const params = /getWardrobe\((\d+),'([^']+)','([^']+)',/.exec(html);
return { id: params[1], nick: params[2], type: params[3], title: parsed.querySelector("h2.fl:not(.idcard-nav-arrow-toggle)").textContent };
}
function makeUrl(meta) {
return `https://www.parfumo.de/action/_get_wardrobe.php?id=${meta.id}&nick=${meta.nick}&type=${meta.type}&order=&order_effect=Nein&list_type=list`;
}
async function getSammlung(sammlungUrl) {
const meta = await getSammlungMeta(sammlungUrl);
const html = await fetchAndParse(makeUrl(meta));
const sammlung = html.querySelector("#wardrobeorder").childNodes;
let perfumes = {};
let brandName = null;
for (node of sammlung) {
if (node.nodeName == "H3") {
brandName = node.textContent;
perfumes[brandName] = [];
} else if (
node.nodeName == "DIV" &&
node.classList.contains("war_list_container")
) {
for (perfume of node.childNodes) {
perfumes[brandName].push(perfume.textContent);
}
}
}
return { nick: meta.nick, title: meta.title, perfumes };
}
async function changeListType(newType = "list") {
const response = await fetchUrl(
"https://www.parfumo.de/action/change_list_type.php?list_type=list&wardrobe=1"
);
const success = response.type == "opaqueredirect";
if (!success) {
console.warn("Could not change list type", response);
}
return success;
}
function getIntersection(sammlung1, sammlung2) {
const res = {};
for (brand in sammlung1) {
if (!(brand in sammlung2)) {
continue;
}
const perfumes1 = sammlung1[brand];
const perfumes2 = sammlung2[brand];
const intersection = perfumes1.filter(perfume =>
perfumes2.includes(perfume)
);
if (intersection.length < 1) {
continue;
}
res[brand] = intersection;
}
return res;
}
function validateSammlungUrl(elem) {
const sammlungRegex = new RegExp("https://www.parfumo.de/Benutzer/[^/]+/Sammlung(/[^/]*)?");
elem.style.backgroundColor = null;
const sammlung1Url = elem.value;
if (!elem.value.match(sammlungRegex)) {
elem.style.backgroundColor = "red";
return false;
}
return true;
}
async function compare() {
document.getElementById("userFirst").textContent = "...";
document.getElementById("userSecond").textContent = "...";
const list = document.getElementById("result");
list.innerHTML = "";
const sammlung1Elem = document.getElementById("sammlungFirst");
const sammlung2Elem = document.getElementById("sammlungSecond");
const sammlungUrlsValid = validateSammlungUrl(sammlung1Elem) & validateSammlungUrl(sammlung2Elem);
if (!sammlungUrlsValid) { return; }
const typeChanged = await changeListType("list");
if (typeChanged !== true) {
const list = document.getElementById("result");
list.innerHTML = "<li>Fehler beim Setzen des Listentyps :(</li>";
return;
}
const sammlung1 = await getSammlung(sammlung1Elem.value);
document.getElementById("userFirst").textContent = sammlung1.nick + "'s \"" + sammlung1.title + "\"";
const sammlung2 = await getSammlung(sammlung2Elem.value);
document.getElementById("userSecond").textContent = sammlung2.nick + "'s \"" + sammlung2.title + "\"";
const intersection = getIntersection(
sammlung1.perfumes,
sammlung2.perfumes
);
console.log({ sammlung1, sammlung2, intersection });
for (brandName in intersection) {
const brandElem = document.createElement("li");
brandElem.textContent = brandName;
brandElem.classList.add("brand");
list.appendChild(brandElem);
const perfumeListElem = document.createElement("ul");
const perfumes = intersection[brandName];
for (perfumeName of perfumes) {
const perfumeElem = document.createElement("li");
perfumeElem.textContent = perfumeName;
perfumeListElem.appendChild(perfumeElem);
}
list.appendChild(perfumeListElem);
}
if (Object.keys(intersection).length == 0) {
list.innerHTML = "<li>keine Übereinstimmungen :(</li>";
}
}
async function tryCompare() {
const button = document.getElementById("compare");
button.disabled = true;
try { await compare(); }
catch (e) {
const list = document.getElementById("result");
list.innerHTML = "<li>fehler :(</li>";
} finally {
button.disabled = false;
}
}
</script>
<style>
#userFirst {
color: #e3085a;
}
#userSecond {
color: #066baf;
}
#result .brand {
font-weight: bold;
}
h1 {
font-size: large;
}
ul+li {
padding-top: 1em;
}
</style>
<body>
<input type="text" id="sammlungFirst" placeholder="https://www.parfumo.de/Benutzer/Groxxda/Sammlung/Habe_ich"
size="55" />
<input type="text" id="sammlungSecond" placeholder="https://www.parfumo.de/Benutzer/Parfumo2/Sammlung/Merkliste"
size="55" />
<input type="button" id="compare" onclick="tryCompare()" value="Vergleichen" />
<h1 id="title">
Überschneidungen zw. <span id="userFirst">Sammlung 1</span> &
<span id="userSecond">Sammlung 2</span>
</h1>
<ul id="result"></ul>
</body>
</html>