File size: 8,425 Bytes
71aaa8a
 
 
 
f24eb4d
 
 
 
 
 
71aaa8a
 
 
 
 
 
 
 
 
 
 
f24eb4d
 
953b496
71aaa8a
bbe684c
b378fe5
bbe684c
 
 
 
 
 
 
 
 
 
 
71aaa8a
 
 
 
 
 
 
 
f24eb4d
71aaa8a
 
 
f24eb4d
 
71aaa8a
 
 
 
 
 
 
 
 
 
 
 
f24eb4d
 
 
 
71aaa8a
 
f24eb4d
953b496
71aaa8a
 
 
 
 
 
 
 
 
953b496
71aaa8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f24eb4d
71aaa8a
 
 
 
 
f24eb4d
71aaa8a
 
 
f24eb4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
953b496
 
 
 
 
 
bbe684c
f24eb4d
 
953b496
f24eb4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
953b496
 
 
 
f24eb4d
953b496
f24eb4d
 
 
 
 
71aaa8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
953b496
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
(() => {
  "use strict";

  const destination = "https://www.socialsleuth.xyz/face-search";
  const handoffEndpoint = "https://www.socialsleuth.xyz/api/face-search/handoff";
  const maxDimensionPx = 1600;
  const targetMaxBytes = 1.5 * 1024 * 1024;
  const backendMaxBytes = 2 * 1024 * 1024;
  const uploadHardLimitBytes = 50 * 1024 * 1024;
  const jpegQualitySteps = [0.82, 0.75, 0.68, 0.6, 0.5, 0.42];
  const uploadCard = document.getElementById("upload-card");
  const photoInput = document.getElementById("photo-input");
  const uploadEmpty = document.getElementById("upload-empty");
  const uploadPreview = document.getElementById("upload-preview");
  const previewImage = document.getElementById("preview-image");
  const fileName = document.getElementById("file-name");
  const changePhoto = document.getElementById("change-photo");
  const uploadError = document.getElementById("upload-error");
  const startSearch = document.getElementById("start-search");

  let previewUrl = "";
  let selectedFile = null;
  let isSubmitting = false;
  let finalHandoffUrl = "";

  const openTopLevelWindow = (url = "about:blank") => {
    return window.open(url, "_blank");
  };

  document
    .querySelectorAll(".how-it-works .section-button, .final-cta .primary-button")
    .forEach((link) => {
      link.addEventListener("click", (event) => {
        event.preventDefault();
        openTopLevelWindow(destination);
      });
    });

  const isSupportedImage = (file) => {
    if (!file) return false;
    const acceptedTypes = ["image/jpeg", "image/jpg", "image/png"];
    if (acceptedTypes.includes(file.type.toLowerCase())) return true;
    return /\.(jpe?g|png)$/i.test(file.name);
  };

  const openFilePicker = () => {
    if (isSubmitting) return;
    photoInput.click();
  };

  const showError = (message = "Please choose a JPG or PNG image.") => {
    uploadError.textContent = message;
    uploadError.hidden = false;
  };

  const clearError = () => {
    uploadError.hidden = true;
  };

  const displayFile = (file) => {
    if (!isSupportedImage(file)) {
      showError();
      return;
    }
    if (file.size > uploadHardLimitBytes) {
      showError("That photo is too large to process. Please choose another image.");
      return;
    }

    clearError();
    selectedFile = file;
    finalHandoffUrl = "";
    if (previewUrl) URL.revokeObjectURL(previewUrl);
    previewUrl = URL.createObjectURL(file);

    previewImage.src = previewUrl;
    previewImage.alt = `Preview of selected photo: ${file.name}`;
    fileName.textContent = file.name;
    uploadEmpty.hidden = true;
    uploadPreview.hidden = false;
    startSearch.disabled = false;
    startSearch.textContent = "START SEARCH";
  };

  photoInput.addEventListener("change", () => {
    displayFile(photoInput.files && photoInput.files[0]);
    photoInput.value = "";
  });

  uploadCard.addEventListener("click", openFilePicker);
  uploadCard.addEventListener("keydown", (event) => {
    if (event.key === "Enter" || event.key === " ") {
      event.preventDefault();
      openFilePicker();
    }
  });

  ["dragenter", "dragover"].forEach((eventName) => {
    uploadCard.addEventListener(eventName, (event) => {
      event.preventDefault();
      uploadCard.classList.add("is-dragging");
    });
  });

  ["dragleave", "drop"].forEach((eventName) => {
    uploadCard.addEventListener(eventName, (event) => {
      event.preventDefault();
      uploadCard.classList.remove("is-dragging");
    });
  });

  uploadCard.addEventListener("drop", (event) => {
    if (isSubmitting) return;
    displayFile(event.dataTransfer.files && event.dataTransfer.files[0]);
  });

  changePhoto.addEventListener("click", (event) => {
    event.stopPropagation();
    if (isSubmitting) return;
    openFilePicker();
  });

  const loadImage = (file) =>
    new Promise((resolve, reject) => {
      const url = URL.createObjectURL(file);
      const image = new Image();
      image.onload = () => resolve({ image, url });
      image.onerror = () => {
        URL.revokeObjectURL(url);
        reject(new Error("IMAGE_DECODE_FAILED"));
      };
      image.src = url;
    });

  const canvasToBlob = (canvas, quality) =>
    new Promise((resolve, reject) => {
      canvas.toBlob(
        (blob) => (blob ? resolve(blob) : reject(new Error("CANVAS_ENCODE_FAILED"))),
        "image/jpeg",
        quality
      );
    });

  // Converted from production's validated imageCompression.js settings so
  // common phone photos fit the existing 2MB temporary-session limit.
  const prepareImageForHandoff = async (file) => {
    if (file.size <= 1024 * 1024) return file;

    let sourceUrl = "";
    try {
      const loaded = await loadImage(file);
      const image = loaded.image;
      sourceUrl = loaded.url;
      const longEdge = Math.max(image.naturalWidth || 0, image.naturalHeight || 0);
      const scale = longEdge > maxDimensionPx ? maxDimensionPx / longEdge : 1;
      const width = Math.max(1, Math.round((image.naturalWidth || maxDimensionPx) * scale));
      const height = Math.max(1, Math.round((image.naturalHeight || maxDimensionPx) * scale));
      const canvas = document.createElement("canvas");
      canvas.width = width;
      canvas.height = height;
      const context = canvas.getContext("2d");
      if (!context) return file;
      context.imageSmoothingEnabled = true;
      context.imageSmoothingQuality = "high";
      context.drawImage(image, 0, 0, width, height);

      let blob = null;
      for (const quality of jpegQualitySteps) {
        blob = await canvasToBlob(canvas, quality);
        if (blob.size <= targetMaxBytes) break;
      }
      if (!blob || blob.size >= file.size) return file;

      const baseName = String(file.name || "photo").replace(/\.[^./]+$/, "") || "photo";
      return new File([blob], `${baseName}.jpg`, {
        type: "image/jpeg",
        lastModified: Date.now(),
      });
    } finally {
      if (sourceUrl) URL.revokeObjectURL(sourceUrl);
    }
  };

  startSearch.addEventListener("click", async () => {
    if (startSearch.disabled || isSubmitting) return;
    if (finalHandoffUrl) {
      window.open(finalHandoffUrl, "_blank");
      return;
    }
    if (!selectedFile) return;

    isSubmitting = true;
    startSearch.disabled = true;
    startSearch.textContent = "PREPARING SEARCH…";
    clearError();

    try {
      const preparedFile = await prepareImageForHandoff(selectedFile);
      if (!isSupportedImage(preparedFile) || preparedFile.size > backendMaxBytes) {
        throw new Error("IMAGE_TOO_LARGE");
      }

      startSearch.textContent = "TRANSFERRING PHOTO…";
      const form = new FormData();
      form.append("image", preparedFile, preparedFile.name || "photo.jpg");
      const response = await fetch(handoffEndpoint, {
        method: "POST",
        body: form,
        credentials: "omit",
      });
      const data = await response.json().catch(() => null);
      if (!response.ok || !data?.ok || typeof data.handoffToken !== "string") {
        throw new Error(data?.error || "HANDOFF_FAILED");
      }

      finalHandoffUrl = `${destination}?handoff=${encodeURIComponent(data.handoffToken)}`;
      isSubmitting = false;
      startSearch.disabled = false;
      startSearch.textContent = "CONTINUE TO SEARCH";
    } catch (_) {
      finalHandoffUrl = "";
      isSubmitting = false;
      startSearch.disabled = false;
      startSearch.textContent = "START SEARCH";
      showError("We couldn't transfer that photo. Please try again.");
    }
  });

  document.querySelectorAll(".faq-question").forEach((button) => {
    const answer = document.getElementById(button.getAttribute("aria-controls"));
    const item = button.closest(".faq-item");

    button.addEventListener("click", () => {
      const willOpen = button.getAttribute("aria-expanded") !== "true";
      button.setAttribute("aria-expanded", String(willOpen));
      item.classList.toggle("is-open", willOpen);

      if (willOpen) {
        answer.hidden = false;
        requestAnimationFrame(() => answer.classList.add("is-open"));
      } else {
        answer.classList.remove("is-open");
        window.setTimeout(() => {
          if (button.getAttribute("aria-expanded") === "false") answer.hidden = true;
        }, 230);
      }
    });
  });

  window.addEventListener("beforeunload", () => {
    if (previewUrl) URL.revokeObjectURL(previewUrl);
  });
})();