PageSourceSearch

https://spacewaves2.net/template/default/js/surprise-me.js

js spacewaves2.net collected 2026-09-26 03:39:19 UTC 2,200 bytes, 84 lines download raw bytes

1(function () {
2  'use strict';
3
4  var GAMES_JSON = '/data/games.json';
5  var gamesCache = null;
6  var loading = false;
7
8  function getCurrentSlug() {
9    var parts = window.location.pathname.replace(/\/+$/, '').split('/').filter(Boolean);
10    if (!parts.length) return null;
11    var dirs = ['wave-games', 'geometry-games', 'arcade-games'];
12    var i;
13    for (i = 0; i < parts.length - 1; i++) {
14      if (dirs.indexOf(parts[i]) !== -1) {
15        return decodeURIComponent(parts[i + 1]);
16      }
17    }
18    return null;
19  }
20
21  function normalizeList(data) {
22    if (Array.isArray(data)) return data;
23    if (data && Array.isArray(data.games)) return data.games;
24    return [];
25  }
26
27  function pickRandom(list) {
28    if (!list.length) return null;
29    var slug = getCurrentSlug();
30    var pool = slug
31      ? list.filter(function (g) { return g.slug !== slug; })
32      : list.slice();
33    if (!pool.length) pool = list;
34    return pool[Math.floor(Math.random() * pool.length)];
35  }
36
37  function goToGame(game) {
38    if (game && game.url) window.location.href = game.url;
39  }
40
41  function loadGames(cb) {
42    if (gamesCache) {
43      cb(gamesCache);
44      return;
45    }
46    fetch(GAMES_JSON + '?_=' + Date.now(), { cache: 'no-store' })
47      .then(function (r) {
48        if (!r.ok) throw new Error('HTTP ' + r.status);
49        return r.json();
50      })
51      .then(function (data) {
52        gamesCache = normalizeList(data);
53        cb(gamesCache);
54      })
55      .catch(function () {
56        cb([]);
57      });
58  }
59
60  function initSurpriseMe() {
61    var btn = document.getElementById('surprise-me-btn');
62    if (!btn) return;
63    btn.addEventListener('mouseenter', function () {
64      loadGames(function () {});
65    });
66    btn.addEventListener('click', function () {
67      if (loading) return;
68      loading = true;
69      btn.classList.add('is-loading');
70      loadGames(function (list) {
71        loading = false;
72        btn.classList.remove('is-loading');
73        var game = pickRandom(list);
74        if (game) goToGame(game);
75      });
76    });
77  }
78
79  if (document.readyState === 'loading') {
80    document.addEventListener('DOMContentLoaded', initSurpriseMe);
81  } else {
82    initSurpriseMe();
83  }
84})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.