aboutsummaryrefslogtreecommitdiffstats
path: root/tiermaker.user.js
blob: cabba50744c6415f1209d75400cee33d4048f89d (plain) (blame)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// ==UserScript==
// @name         Tiermaker Liberator
// @namespace    interactive-tierlist
// @version      1.0.0
// @description  Export tierlist as JSON with rows + untiered; convert remote image URLs to data: URLs
// @match        *://tiermaker.com/*
// @match        file://*/*
// @run-at       document-idle
// @grant        GM_xmlhttpRequest
// @connect      *
// ==/UserScript==

(function () {
  'use strict';

  const UNTIERED_RE = /untiered|unranked|not\s*tiered|remaining/i;
  const dataUrlCache = new Map();

  function textOf(el) {
    return (el?.textContent || '').replace(/\s+/g, ' ').trim();
  }

  function firstNonEmpty(values) {
    for (const v of values) {
      if (typeof v === 'string' && v.trim() !== '') return v.trim();
    }
    return '';
  }

  function normalizeUrl(src) {
    if (!src) return '';
    if (src.startsWith('data:')) return src;
    try {
      return new URL(src, location.href).href;
    } catch {
      return src;
    }
  }

  function isLikelyRowName(name) {
    if (!name) return false;
    if (name.length > 48) return false;
    if (/share|download|copy|login|sign in|facebook|twitter|settings/i.test(name)) return false;
    return true;
  }

  function uniqueElements(arr) {
    return [...new Set(arr)];
  }

  function getRowImageElements(rowEl) {
    const explicit = Array.from(
      rowEl.querySelectorAll('.character img, .draggable img, img.character, img.draggable')
    );

    const imgs = explicit.length ? explicit : Array.from(rowEl.querySelectorAll('img'));

    return uniqueElements(
      imgs.filter((img) => {
        const src = img.currentSrc || img.getAttribute('src') || '';
        if (!src) return false;
        const w = img.naturalWidth || img.width || 0;
        const h = img.naturalHeight || img.height || 0;
        if ((w > 0 && w <= 2) || (h > 0 && h <= 2)) return false;

        return true;
      })
    );
  }

  function extractRowName(rowEl) {
    const candidates = [];
    candidates.push(
      ...rowEl.querySelectorAll(
        '[class*="label"], [class*="tier"], [class*="rank"], span, h2, h3, h4, p'
      )
    );

    for (const el of candidates) {
      const t = textOf(el);
      if (isLikelyRowName(t)) return t;
    }

    return '';
  }

  function collectRowsFromParent(parentEl) {
    const rows = [];

    for (const child of Array.from(parentEl.children)) {
      const imgs = getRowImageElements(child);
      if (!imgs.length || imgs.length > 400) continue;

      const name = extractRowName(child);
      if (!isLikelyRowName(name)) continue;

      rows.push({ name, imgEls: imgs, rowEl: child });
    }

    return rows;
  }

  function findBestRowSet() {
    const main = document.querySelector('#main-container') || document.body;

    const seedSelectors = [
      '#char-tier-container-scroll',
      '#char-tier-outer-container-scroll',
      '#char-tier-container',
      '[id*="char-tier"]',
      '[id*="tier-container"]',
      '[class*="tier-container"]',
      '[class*="tier-list"]',
    ];

    const seeds = new Set();
    for (const sel of seedSelectors) {
      for (const el of document.querySelectorAll(sel)) seeds.add(el);
    }

    // Fallback seeds
    for (const el of Array.from(main.querySelectorAll('[id*="tier"], [class*="tier"]')).slice(0, 200)) {
      seeds.add(el);
    }
    seeds.add(main);

    let bestRows = [];
    let bestParent = null;

    for (const seed of seeds) {
      const parentCandidates = [seed, ...Array.from(seed.children)];
      for (const parent of parentCandidates) {
        const rows = collectRowsFromParent(parent);
        if (rows.length > bestRows.length) {
          bestRows = rows;
          bestParent = parent;
        }
      }
    }

    return { rows: bestRows, parent: bestParent };
  }

  function findUntieredElements(searchRoot, usedRowEls) {
    const selectors = [
      '[id*="untiered"]',
      '[class*="untiered"]',
      '[id*="unranked"]',
      '[class*="unranked"]',
    ];

    for (const sel of selectors) {
      for (const el of searchRoot.querySelectorAll(sel)) {
        if (usedRowEls.has(el)) continue;
        const imgs = getRowImageElements(el);
        if (imgs.length) return imgs;
      }
    }

    return [];
  }

  function detectTitle() {
    const h1 = textOf(document.querySelector('h1'));
    const og = document.querySelector('meta[property="og:title"]')?.getAttribute('content') || '';
    let title = firstNonEmpty([h1, og, document.title, 'Untitled Tierlist']);
    title = title
      .replace(/\|\s*TierMaker.*$/i, '')
      .replace(/\s*Tier List Maker.*$/i, '')
      .trim();
    return title || 'Untitled Tierlist';
  }

  function getImageTitle(img) {
    return firstNonEmpty([
      img.getAttribute('data-title'),
      img.getAttribute('title'),
      img.getAttribute('alt'),
      img.closest('[data-title]')?.getAttribute('data-title'),
      img.closest('[title]')?.getAttribute('title'),
      '',
    ]);
  }

  function getImageDescription(img) {
    return firstNonEmpty([
      img.getAttribute('data-description'),
      img.getAttribute('data-desc'),
      img.closest('[data-description]')?.getAttribute('data-description'),
      img.closest('[data-desc]')?.getAttribute('data-desc'),
      '',
    ]);
  }

  function blobToDataUrl(blob) {
    return new Promise((resolve, reject) => {
      const reader = new FileReader();
      reader.onerror = () => reject(new Error('Failed converting blob to data URL'));
      reader.onload = () => resolve(String(reader.result));
      reader.readAsDataURL(blob);
    });
  }

  function gmFetchBlob(url) {
    return new Promise((resolve, reject) => {
      if (typeof GM_xmlhttpRequest !== 'function') {
        reject(new Error('GM_xmlhttpRequest unavailable'));
        return;
      }

      GM_xmlhttpRequest({
        method: 'GET',
        url,
        responseType: 'arraybuffer',
        onload: (res) => {
          if (res.status < 200 || res.status >= 400 || !res.response) {
            reject(new Error(`GM request failed (${res.status})`));
            return;
          }

          const ctMatch = /content-type:\s*([^\r\n;]+)/i.exec(res.responseHeaders || '');
          const mime = ctMatch ? ctMatch[1].trim() : 'application/octet-stream';
          resolve(new Blob([res.response], { type: mime }));
        },
        onerror: () => reject(new Error('GM request network error')),
      });
    });
  }

  async function toDataUrl(src) {
    const normalized = normalizeUrl(src);
    if (!normalized) return '';
    if (normalized.startsWith('data:')) return normalized;

    if (dataUrlCache.has(normalized)) return dataUrlCache.get(normalized);

    const p = (async () => {
      try {
        const res = await fetch(normalized);
        if (!res.ok) throw new Error(`fetch failed (${res.status})`);
        const blob = await res.blob();
        return await blobToDataUrl(blob);
      } catch {
        const blob = await gmFetchBlob(normalized);
        return await blobToDataUrl(blob);
      }
    })().catch(() => normalized); // final fallback: keep original URL

    dataUrlCache.set(normalized, p);
    return p;
  }

  async function imageElToTierImage(imgEl) {
    const rawSrc = imgEl.currentSrc || imgEl.getAttribute('src') || '';
    return {
      src: await toDataUrl(rawSrc),
      title: getImageTitle(imgEl),
      description: getImageDescription(imgEl),
    };
  }

  async function buildTierlistFile() {
    const { rows: rawRows, parent } = findBestRowSet();
    if (!rawRows.length) {
      throw new Error('Could not find tier rows in the page.');
    }

    const result = {
      title: detectTitle(),
      rows: [],
    };

    const usedRowEls = new Set();
    let untiered = null;

    for (const row of rawRows) {
      usedRowEls.add(row.rowEl);
      const imgs = await Promise.all(row.imgEls.map(imageElToTierImage));

      if (UNTIERED_RE.test(row.name)) {
        untiered = imgs;
      } else {
        result.rows.push({ name: row.name, imgs });
      }
    }

    if (!untiered) {
      const root = parent || document.body;
      const untieredEls = findUntieredElements(root, usedRowEls);
      if (untieredEls.length) {
        untiered = await Promise.all(untieredEls.map(imageElToTierImage));
      }
    }

    if (untiered && untiered.length) {
      result.untiered = untiered;
    }

    return result;
  }

  function downloadJson(filename, text) {
    const blob = new Blob([text], { type: 'application/json' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = filename;
    document.body.appendChild(a);
    a.click();
    a.remove();
    URL.revokeObjectURL(url);
  }

  function slugify(name) {
    return (name || 'tierlist')
      .toLowerCase()
      .replace(/[^a-z0-9]+/g, '-')
      .replace(/(^-|-$)/g, '') || 'tierlist';
  }

  async function exportNow() {
    const data = await buildTierlistFile();
    const json = JSON.stringify(data, null, 2);

    console.log('Tierlist JSON:', data);
    downloadJson(`${slugify(data.title)}.json`, json);

    if (navigator.clipboard?.writeText) {
      navigator.clipboard.writeText(json).catch(() => {});
    }

    return data;
  }

  // Expose manual API for console use:
  window.exportTierlistJson = exportNow;

  // Small on-page button:
  const btn = document.createElement('button');
  btn.textContent = 'Export Tierlist JSON';
  btn.style.position = 'fixed';
  btn.style.right = '16px';
  btn.style.bottom = '16px';
  btn.style.zIndex = '999999';
  btn.style.padding = '8px 12px';
  btn.style.border = '1px solid #444';
  btn.style.borderRadius = '8px';
  btn.style.background = '#111';
  btn.style.color = '#fff';
  btn.style.cursor = 'pointer';

  btn.addEventListener('click', async () => {
    const original = btn.textContent;
    btn.disabled = true;
    btn.textContent = 'Exporting...';
    try {
      await exportNow();
      btn.textContent = 'Exported ✔';
    } catch (err) {
      console.error(err);
      btn.textContent = 'Export failed';
    } finally {
      setTimeout(() => {
        btn.disabled = false;
        btn.textContent = original;
      }, 1800);
    }
  });

  document.body.appendChild(btn);
})();
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage