comparison surugaya-direct-image.user.js @ 116:0e108a9dc6d7

Split suruga-ya scripts
author nanaya <me@nanaya.net>
date Sun, 15 Jan 2023 23:46:56 +0900
parents surugaya-fixes.user.js@ef21ef445fc6
children 8de2d53a4cb1
comparison
equal deleted inserted replaced
115:7774174022af 116:0e108a9dc6d7
1 // ==UserScript==
2 // @name suruga-ya direct image
3 // @namespace https://nanaya.net
4 // @version 2.0.3
5 // @description skip loading image through php which seems to take forever
6 // @author nanaya
7 // @match https://www.suruga-ya.jp/*
8 // @grant none
9 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/surugaya-fixes.user.js
10 // ==/UserScript==
11
12 'use strict';
13
14 const itemImageRegexp = /photo\.php\?shinaban=([0-9A-Z]+)/;
15 function fix (image) {
16 const origSrc = image.getAttribute('src');
17 if (origSrc == null) return;
18 const found = origSrc.match(itemImageRegexp);
19 if (found == null) return;
20
21 const src = `https://www.suruga-ya.jp/database/pics_light/game/${found[1].toLowerCase()}.jpg`;
22 const setNotFound = () => {
23 image.removeEventListener('error', setNotFound);
24 image.setAttribute('src', 'https://www.suruga-ya.jp/database/images/no_photo.jpg');
25 };
26 image.addEventListener('error', setNotFound);
27 image.setAttribute('src', src);
28 }
29
30 function run (node) {
31 if (!(node instanceof window.HTMLElement)) return;
32
33 fix(node);
34 for (const image of node.querySelectorAll('img')) {
35 fix(image);
36 }
37 }
38
39 function onMutate (mutations) {
40 for (const mutation of mutations) {
41 for (const node of mutation.addedNodes) {
42 run(node);
43 }
44 }
45 }
46
47 const observer = new window.MutationObserver(onMutate);
48 observer.observe(document, { childList: true, subtree: true });
49 run(document.body);