# HG changeset patch # User nanaya # Date 1673103748 -32400 # Node ID 6b354277f2d1211821785a3217181c41bbdae932 # Parent 93e21738b588d7fbeec623212a0be60b0613bebe [surugaya-fixes] faster image load diff -r 93e21738b588 -r 6b354277f2d1 surugaya-fixes.user.js --- a/surugaya-fixes.user.js Sun Dec 25 11:52:30 2022 +0900 +++ b/surugaya-fixes.user.js Sun Jan 08 00:02:28 2023 +0900 @@ -1,25 +1,63 @@ // ==UserScript== // @name suruga-ya fixes // @namespace https://myconan.net -// @version 1.0.2 -// @description Show all products +// @version 2.0.0 +// @description Show all products with fast image // @author nanaya // @match https://www.suruga-ya.jp/* // @grant none // @downloadURL https://hg.myconan.net/ec-userscripts/raw-file/tip/surugaya-fixes.user.js // ==/UserScript== +'use strict' + +// always inject adult consent cookie ;(function () { - 'use strict' - const hasAdultCookie = document.cookie.split('; ').includes('adult=1') + if (!hasAdultCookie) { + const d = new Date() + d.setTime(d.getTime() + (100 * 24 * 60 * 60 * 1000)) + document.cookie = `adult=1; expires=${d.toGMTString()}; path=/` + window.location.reload() + } +})() - if (hasAdultCookie) { - return +// skip loading image through php which seems to take forever +;(function () { + const itemImageRegexp = /photo\.php\?shinaban=(\d+)/ + 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]}.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) } - const d = new Date() - d.setTime(d.getTime() + (100 * 24 * 60 * 60 * 1000)) - document.cookie = `adult=1; expires=${d.toGMTString()}; path=/` - window.location.reload() -}).call() + 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) +})()