forked from jeffalo/is.wasteof.money
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
84 lines (73 loc) · 2.97 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta property="og:url" content="https://is.wasteof.money" />
<meta property="og:type" content="website" />
<meta property="og:title" content="Is it a waste of money?" />
<meta property="og:description" content="Find out if a product is a waste of money." />
<meta property="og:image" content="/assets/logo.png" />
<meta property="og:site_name" content="is.wasteof.money" />
<link rel="icon" href="/assets/logo.png">
<title>is.wasteof.money</title>
<link rel="stylesheet" href="style.css">
<script src="https://cdn.jsdelivr.net/npm/fuse.js@v6.4.0"></script>
<script src="items.js"></script>
</head>
<body>
<header>
<a class="noline"href="/"><h1>is.wasteof.money</h1></a>
<a href="/about">about</a> | <a href="https://github.com/JeffaloBob/is.wasteof.money/">github</a>
<hr>
</header>
<main>
<div class="incontainer"><strong class="thething">Is <input oninput="check()" spellcheck="false" id="input" type="text" autofocus> a waste of money?</strong></div>
<div id="result" class="result">Try to type something!</div>
<div id="explanation" class="result"></div>
</main>
</body>
<script>
const explanation = document.getElementById('explanation');
const result = document.getElementById('result');
const input = document.getElementById('input');
// 2. Set up the Fuse instance
const fuse = new Fuse(items, {
keys: ['name'],
threshold: 0.3,
minMatchCharLength: 2
})
const generateFuseMessage = term => {
term = term.toLowerCase();
if (term.length < 3)
return "Not found yet... keep trying?";
const fuseResults = fuse.search(term);
getHtml = item => `<strong><a href=# onclick="fill('${item}')">${item}</strong></a>`;
let matched = fuseResults.map(x => getHtml(x.item.name)).join("<br>");
return matched? `Did you mean:<br>${matched}`: "Not found yet... keep trying?";
}
const check = () => {
let found = items.find(item => item.name == input.value.toLowerCase());
let isWaste = String(found && found.isWaste);
let custom = found && found.custom;
if (found)
explanation.innerHTML = found.explanation || "";
else
explanation.innerHTML = "";
result.innerHTML = {"free": "is free! can something free even be a waste of money?", true: "yes", false: "no", "custom": custom, "undefined": generateFuseMessage(input.value)}[isWaste];
}
const fill = item => {
input.value = item;
input.focus();
check();
}
function hash() {
if(location.hash){
input.value = decodeURIComponent(location.hash.replace('#',''))
check()
}
}
window.addEventListener("popstate",hash);
hash();
</script>
</html>