-
Notifications
You must be signed in to change notification settings - Fork 0
/
favicon-link.html
86 lines (79 loc) · 2.41 KB
/
favicon-link.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Emoji to Favicon Link Generator</title>
<style>
body, html {
height: 100%;
margin: 0;
}
* {
box-sizing: border-box;
}
form {
padding: 1em;
background-image: linear-gradient(
hsl(240, 50%, 65.5%),
hsl(240, 100%, 25.5%)
);
display: grid;
grid-template: auto auto auto 1fr / 1fr auto;
gap: 1em;
}
textarea, output {
grid-column: span 2;
background: white;
}
</style>
</head>
<body>
<form>
<input
type="text"
id="emoji-input"
placeholder="Enter emoji or emoji name"
/>
<button type="button">Generate Link</button>
<output id="svg-source"></output>
<textarea id="encoded-html"></textarea>
</form>
<script type="module">
let emojis = []
try {
let response = await fetch("./emojis.json")
let data = await response.json()
emojis = data.emojis
} catch (e) {
alert(e)
}
let generateLink = () => {
const inputVal = document.querySelector("#emoji-input").value
let emoji
if (/\p{Emoji}/u.test(inputVal)) {
emoji = inputVal
} else {
emoji = emojis.find(({ translation }) =>
translation.toLowerCase().includes(inputVal.toLowerCase())
)?.emoji
}
if (emoji) {
const svgData =
`<svg xmlns='http://www.w3.org/2000/svg' width='48' height='48' viewBox='0 0 16 16'><text x='0' y='14'>${emoji}</text></svg>`
document.querySelector("#svg-source").textContent = svgData
const encodedData = encodeURIComponent(svgData)
const linkTag =
`<link rel="icon" href="data:image/svg+xml,${encodedData}" />`
document.head.insertAdjacentHTML("beforeend", linkTag)
document.querySelector("#encoded-html").value = linkTag
} else {
document.querySelector("#svg-source").textContent = "Emoji not found"
document.querySelector("#encoded-html").value = ""
}
}
document.querySelector("button").addEventListener("click", generateLink)
addEventListener("submit", (e) => e.preventDefault())
</script>
</body>
</html>