ReverseFaceSearch commited on
Commit
71aaa8a
·
verified ·
1 Parent(s): d86126d

Create script.js

Browse files
Files changed (1) hide show
  1. script.js +118 -0
script.js ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (() => {
2
+ "use strict";
3
+
4
+ const destination = "https://www.socialsleuth.xyz/face-search";
5
+ const uploadCard = document.getElementById("upload-card");
6
+ const photoInput = document.getElementById("photo-input");
7
+ const uploadEmpty = document.getElementById("upload-empty");
8
+ const uploadPreview = document.getElementById("upload-preview");
9
+ const previewImage = document.getElementById("preview-image");
10
+ const fileName = document.getElementById("file-name");
11
+ const changePhoto = document.getElementById("change-photo");
12
+ const uploadError = document.getElementById("upload-error");
13
+ const startSearch = document.getElementById("start-search");
14
+
15
+ let previewUrl = "";
16
+
17
+ const isSupportedImage = (file) => {
18
+ if (!file) return false;
19
+ const acceptedTypes = ["image/jpeg", "image/jpg", "image/png"];
20
+ if (acceptedTypes.includes(file.type.toLowerCase())) return true;
21
+ return /\.(jpe?g|png)$/i.test(file.name);
22
+ };
23
+
24
+ const openFilePicker = () => {
25
+ photoInput.click();
26
+ };
27
+
28
+ const showError = () => {
29
+ uploadError.hidden = false;
30
+ };
31
+
32
+ const clearError = () => {
33
+ uploadError.hidden = true;
34
+ };
35
+
36
+ const displayFile = (file) => {
37
+ if (!isSupportedImage(file)) {
38
+ showError();
39
+ return;
40
+ }
41
+
42
+ clearError();
43
+ if (previewUrl) URL.revokeObjectURL(previewUrl);
44
+ previewUrl = URL.createObjectURL(file);
45
+
46
+ previewImage.src = previewUrl;
47
+ previewImage.alt = `Preview of selected photo: ${file.name}`;
48
+ fileName.textContent = file.name;
49
+ uploadEmpty.hidden = true;
50
+ uploadPreview.hidden = false;
51
+ startSearch.disabled = false;
52
+ };
53
+
54
+ photoInput.addEventListener("change", () => {
55
+ displayFile(photoInput.files && photoInput.files[0]);
56
+ photoInput.value = "";
57
+ });
58
+
59
+ uploadCard.addEventListener("click", openFilePicker);
60
+ uploadCard.addEventListener("keydown", (event) => {
61
+ if (event.key === "Enter" || event.key === " ") {
62
+ event.preventDefault();
63
+ openFilePicker();
64
+ }
65
+ });
66
+
67
+ ["dragenter", "dragover"].forEach((eventName) => {
68
+ uploadCard.addEventListener(eventName, (event) => {
69
+ event.preventDefault();
70
+ uploadCard.classList.add("is-dragging");
71
+ });
72
+ });
73
+
74
+ ["dragleave", "drop"].forEach((eventName) => {
75
+ uploadCard.addEventListener(eventName, (event) => {
76
+ event.preventDefault();
77
+ uploadCard.classList.remove("is-dragging");
78
+ });
79
+ });
80
+
81
+ uploadCard.addEventListener("drop", (event) => {
82
+ displayFile(event.dataTransfer.files && event.dataTransfer.files[0]);
83
+ });
84
+
85
+ changePhoto.addEventListener("click", (event) => {
86
+ event.stopPropagation();
87
+ openFilePicker();
88
+ });
89
+
90
+ startSearch.addEventListener("click", () => {
91
+ if (!startSearch.disabled) window.location.href = destination;
92
+ });
93
+
94
+ document.querySelectorAll(".faq-question").forEach((button) => {
95
+ const answer = document.getElementById(button.getAttribute("aria-controls"));
96
+ const item = button.closest(".faq-item");
97
+
98
+ button.addEventListener("click", () => {
99
+ const willOpen = button.getAttribute("aria-expanded") !== "true";
100
+ button.setAttribute("aria-expanded", String(willOpen));
101
+ item.classList.toggle("is-open", willOpen);
102
+
103
+ if (willOpen) {
104
+ answer.hidden = false;
105
+ requestAnimationFrame(() => answer.classList.add("is-open"));
106
+ } else {
107
+ answer.classList.remove("is-open");
108
+ window.setTimeout(() => {
109
+ if (button.getAttribute("aria-expanded") === "false") answer.hidden = true;
110
+ }, 230);
111
+ }
112
+ });
113
+ });
114
+
115
+ window.addEventListener("beforeunload", () => {
116
+ if (previewUrl) URL.revokeObjectURL(previewUrl);
117
+ });
118
+ })();