view surugaya-direct-image.user.js @ 119:34657b8835e2

Add script to adjust fastmail remote image message
author nanaya <me@nanaya.net>
date Tue, 17 Jan 2023 15:48:38 +0900
parents 8de2d53a4cb1
children
line wrap: on
line source

// ==UserScript==
// @name        suruga-ya direct image
// @namespace   https://nanaya.net
// @version     2.0.3
// @description skip loading image through php which seems to take forever
// @author      nanaya
// @match       https://www.suruga-ya.jp/*
// @grant       none
// @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/surugaya-direct-image.user.js
// ==/UserScript==

'use strict';

const itemImageRegexp = /photo\.php\?shinaban=([0-9A-Z]+)/;
function fix (image) {
  const origSrc = image.getAttribute('src');
  if (origSrc == null) return;
  const found = origSrc.match(itemImageRegexp);
  if (found == null) return;

  const src = `https://www.suruga-ya.jp/database/pics_light/game/${found[1].toLowerCase()}.jpg`;
  const setNotFound = () => {
    image.removeEventListener('error', setNotFound);
    image.setAttribute('src', 'https://www.suruga-ya.jp/database/images/no_photo.jpg');
  };
  image.addEventListener('error', setNotFound);
  image.setAttribute('src', src);
}

function run (node) {
  if (!(node instanceof window.HTMLElement)) return;

  fix(node);
  for (const image of node.querySelectorAll('img')) {
    fix(image);
  }
}

function onMutate (mutations) {
  for (const mutation of mutations) {
    for (const node of mutation.addedNodes) {
      run(node);
    }
  }
}

const observer = new window.MutationObserver(onMutate);
observer.observe(document, { childList: true, subtree: true });
run(document.body);