-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
53 lines (42 loc) · 1.25 KB
/
Copy pathscript.js
File metadata and controls
53 lines (42 loc) · 1.25 KB
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
const page = document.getElementById("page")
const imageUpload = document.getElementById("imageUpload")
const backgroundImageKey = "onepictab.backgroundImage"
function applyBackground(imageDataUrl) {
if (imageDataUrl) {
page.style.backgroundImage = `url("${imageDataUrl}")`
page.classList.add("has-image")
return
}
page.style.backgroundImage = ""
page.classList.remove("has-image")
}
function saveImage(file) {
if (!file || !file.type.startsWith("image/")) {
return
}
const reader = new FileReader()
reader.addEventListener("load", () => {
const imageDataUrl = reader.result
if (typeof imageDataUrl !== "string") {
return
}
try {
localStorage.setItem(backgroundImageKey, imageDataUrl)
} catch (error) {
if (error instanceof DOMException && error.name === "QuotaExceededError") {
alert("The image is too large for storage.\nPlease compress or resize it and try again.")
} else {
throw error
}
return
}
applyBackground(imageDataUrl)
})
reader.readAsDataURL(file)
}
imageUpload.addEventListener("change", (event) => {
const [file] = event.target.files
saveImage(file)
imageUpload.value = ""
})
applyBackground(localStorage.getItem(backgroundImageKey))