File size: 7,748 Bytes
f9df336
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

const $ = (id) => document.getElementById(id);

const urlInput = $("url");
const findBtn = $("find");
const statusBox = $("status");
const productSection = $("product");
const productInfo = $("productInfo");
const gallerySection = $("gallerySection");
const gallery = $("gallery");
const count = $("count");
const tableSection = $("tableSection");
const tableBody = $("tableBody");

function showStatus(message, kind="ok") {
  statusBox.className = `status ${kind}`;
  statusBox.textContent = message;
  statusBox.classList.remove("hidden");
}

function hideSections() {
  productSection.classList.add("hidden");
  gallerySection.classList.add("hidden");
  tableSection.classList.add("hidden");
  gallery.innerHTML = "";
  tableBody.innerHTML = "";
}

function normalizeOriginal(u) {
  if (!u) return "";
  try {
    const x = new URL(u);
    x.pathname = x.pathname
      .replace(/_(pico|icon|thumb|small|compact|medium|large|grande|master|\d+x\d*|x\d+)(_[a-z0-9_-]+)*(\.[a-z0-9]+)$/i, "$3")
      .replace(/_progressive(\.[a-z0-9]+)$/i, "$1");
    return x.toString();
  } catch {
    return u
      .replace(/_(pico|icon|thumb|small|compact|medium|large|grande|master|\d+x\d*)(_[a-z0-9_-]+)*(\.[a-z0-9]+)(?=\?|$)/i, "$3")
      .replace(/_progressive(\.[a-z0-9]+)(?=\?|$)/i, "$1");
  }
}

function addCandidate(map, url, type) {
  if (!url) return;
  try {
    url = new URL(url).href;
  } catch { return; }
  if (!/\.(jpg|jpeg|png|webp|avif)(\?.*)?$/i.test(url)) return;
  if (!map.has(url)) map.set(url, {url, type});
}

function collectImagesFromObject(obj, map, type="shopify-json") {
  if (!obj) return;
  if (typeof obj === "string") {
    if (/\.(jpg|jpeg|png|webp|avif)(\?.*)?$/i.test(obj)) addCandidate(map, obj, type);
    return;
  }
  if (Array.isArray(obj)) {
    obj.forEach(x => collectImagesFromObject(x, map, type));
    return;
  }
  if (typeof obj === "object") {
    for (const [key, value] of Object.entries(obj)) {
      const k = key.toLowerCase();
      if (["image","images","src","url","featured_image"].includes(k)) {
        collectImagesFromObject(value, map, `${type}:${key}`);
      } else if (typeof value === "object") {
        collectImagesFromObject(value, map, type);
      }
    }
  }
}

async function fetchJSON(url) {
  const r = await fetch(url, {headers: {"Accept":"application/json"}});
  if (!r.ok) throw new Error(`${r.status} ${r.statusText}`);
  return await r.json();
}

async function getShopifyProduct(productUrl) {
  const u = new URL(productUrl);
  const match = u.pathname.match(/\/products\/([^/?#]+)/i);
  if (!match) throw new Error("This does not look like a Shopify /products/... URL.");
  const handle = match[1];

  // Shopify Ajax product endpoint. This is lightweight and does not need a server.
  const endpoint = `${u.origin}/products/${handle}.js`;
  return await fetchJSON(endpoint);
}

function renderProduct(product) {
  const title = product.title || "Unknown product";
  const vendor = product.vendor || "";
  const productId = product.id || "";
  productInfo.innerHTML = `
    <div><strong>${escapeHtml(title)}</strong></div>
    ${vendor ? `<div>Vendor: ${escapeHtml(vendor)}</div>` : ""}
    ${productId ? `<div>Product ID: ${escapeHtml(String(productId))}</div>` : ""}
  `;
  productSection.classList.remove("hidden");
}

function escapeHtml(s) {
  return String(s).replace(/[&<>"']/g, c => ({
    "&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"
  }[c]));
}

function rank(items) {
  return items.sort((a,b) => score(b) - score(a));
}

function score(x) {
  let s = 0;
  const u = x.url.toLowerCase();
  const t = x.type.toLowerCase();
  if (t.includes("original")) s += 100;
  if (!/_\d+x\d*(?:_|\.|\?)/i.test(u) && !/_progressive\./i.test(u)) s += 25;
  if (t.includes("shopify")) s += 10;
  return s;
}

function renderResults(items) {
  const ranked = rank(items);
  count.textContent = `${ranked.length} candidate(s)`;
  gallery.innerHTML = "";
  tableBody.innerHTML = "";

  ranked.forEach((item, i) => {
    const original = normalizeOriginal(item.url);
    const card = document.createElement("div");
    card.className = "item";

    const img = document.createElement("img");
    img.loading = "lazy";
    img.src = item.url;
    img.alt = `Candidate ${i+1}`;
    img.onerror = () => {
      card.querySelector(".imageState").textContent = "Image could not be loaded by the browser";
      img.style.opacity = ".25";
    };

    const body = document.createElement("div");
    body.className = "itemBody";
    body.innerHTML = `
      <strong>${escapeHtml(item.type)}</strong>
      <div class="imageState">Candidate ${i+1}</div>
      <a href="${escapeHtml(item.url)}" target="_blank" rel="noopener">Open image</a>
      <div class="actions">
        <a class="smallBtn" href="${escapeHtml(original)}" target="_blank" rel="noopener">Open original candidate</a>
      </div>
    `;
    card.appendChild(img);
    card.appendChild(body);
    gallery.appendChild(card);

    const tr = document.createElement("tr");
    tr.innerHTML = `
      <td>${i+1}</td>
      <td>${escapeHtml(item.type)}</td>
      <td class="url"><a href="${escapeHtml(item.url)}" target="_blank" rel="noopener">${escapeHtml(item.url)}</a></td>
      <td class="url"><a href="${escapeHtml(original)}" target="_blank" rel="noopener">${escapeHtml(original)}</a></td>
    `;
    tableBody.appendChild(tr);
  });

  gallerySection.classList.remove("hidden");
  tableSection.classList.remove("hidden");
}

async function findImages() {
  const productUrl = urlInput.value.trim();
  hideSections();

  if (!productUrl) {
    showStatus("Enter an Al Diplomacy product URL.", "warn");
    return;
  }

  findBtn.disabled = true;
  findBtn.textContent = "Searching...";
  showStatus("Reading Shopify product data...", "ok");

  try {
    const product = await getShopifyProduct(productUrl);
    renderProduct(product);

    const map = new Map();

    // Shopify product.js normally exposes the complete image array.
    if (product.images) collectImagesFromObject(product.images, map, "shopify:images");
    if (product.featured_image) collectImagesFromObject(product.featured_image, map, "shopify:featured_image");

    // Some themes expose additional media.
    if (product.media) collectImagesFromObject(product.media, map, "shopify:media");

    const items = [...map.values()];

    // Add original variants.
    const originals = [];
    items.forEach(x => {
      const o = normalizeOriginal(x.url);
      if (o && o !== x.url && !map.has(o)) {
        originals.push({url:o, type:x.type + " -> original"});
      }
    });
    originals.forEach(x => map.set(x.url, x));

    const finalItems = [...map.values()];

    if (!finalItems.length) {
      showStatus(
        "The Shopify endpoint worked, but no image assets were returned. " +
        "This product may use a theme/media structure that needs a different extractor.",
        "warn"
      );
    } else {
      showStatus(
        `Success. Shopify returned ${finalItems.length} image candidate(s). ` +
        "The app is showing the original/high-resolution candidates first where possible.",
        "ok"
      );
      renderResults(finalItems);
    }
  } catch (e) {
    showStatus(
      "The browser could not read the Shopify product endpoint.\n\n" +
      `Reason: ${e.message}\n\n` +
      "This is usually a CORS/site-access restriction. If Al Diplomacy blocks " +
      "browser requests, a tiny CPU-only backend proxy will be needed; no GPU is required.",
      "err"
    );
  } finally {
    findBtn.disabled = false;
    findBtn.textContent = "Find Images";
  }
}

findBtn.addEventListener("click", findImages);
urlInput.addEventListener("keydown", e => {
  if (e.key === "Enter") findImages();
});