Empornium thumbnail preload

Preloads thumbnail images in the torrent table.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name             Empornium thumbnail preload
// @version          1.4.0
// @description      Preloads thumbnail images in the torrent table.
// @namespace        https://greasyfork.org/users/241444
// @author           salad: https://greasyfork.org/en/users/241444-salad
// @license          GPL-3.0-only
// @match            https://www.empornium.is/torrents.php*
// @match            https://www.empornium.is/top10.php*
// @match            https://www.empornium.is/bookmarks.php*
// @match            https://www.empornium.sx/torrents.php*
// @match            https://www.empornium.sx/top10.php*
// @match            https://www.empornium.sx/bookmarks.php*
// @icon             https://www.google.com/s2/favicons?domain=empornium.is
// ==/UserScript==

(async () => {

  if (document.querySelectorAll('#torrent_table, .torrent_table').length === 0) {
    return;
  }

  const torrentLinks = document.querySelectorAll('#torrent_table td a[onmouseover], .torrent_table td a[onmouseover]');

  const torrentUrls = Array.from(torrentLinks)
    .map(el => new URL(el.href));
  const torrentIds = torrentUrls.map(url => url.searchParams.get('id'));

  const domParser = new DOMParser();
  const previewHtmls = torrentIds.map(id => unsafeWindow[`overlay${id}`])
    .map(html => domParser.parseFromString(html, 'text/html'));
  const thumbnailUrls = previewHtmls.map(html => html.querySelector('img').src);

  console.info('Preloading %s thumbnails...', thumbnailUrls.length);
  console.time('Preloading thumbnails');

  const thumbnailElements = thumbnailUrls.map(src => {
    return new Promise(resolve => {
      const img = new Image();
      img.onload = () => resolve(img);
      img.src = src;
    });
  });

  await Promise.all(thumbnailElements);
  console.timeEnd('Preloading thumbnails');

})();