comparison surugaya-fixes.user.js @ 105:6b354277f2d1

[surugaya-fixes] faster image load
author nanaya <me@nanaya.net>
date Sun, 08 Jan 2023 00:02:28 +0900
parents 86da34e62d29
children 42fbeb68c0e5
comparison
equal deleted inserted replaced
104:93e21738b588 105:6b354277f2d1
1 // ==UserScript== 1 // ==UserScript==
2 // @name suruga-ya fixes 2 // @name suruga-ya fixes
3 // @namespace https://myconan.net 3 // @namespace https://myconan.net
4 // @version 1.0.2 4 // @version 2.0.0
5 // @description Show all products 5 // @description Show all products with fast image
6 // @author nanaya 6 // @author nanaya
7 // @match https://www.suruga-ya.jp/* 7 // @match https://www.suruga-ya.jp/*
8 // @grant none 8 // @grant none
9 // @downloadURL https://hg.myconan.net/ec-userscripts/raw-file/tip/surugaya-fixes.user.js 9 // @downloadURL https://hg.myconan.net/ec-userscripts/raw-file/tip/surugaya-fixes.user.js
10 // ==/UserScript== 10 // ==/UserScript==
11 11
12 'use strict'
13
14 // always inject adult consent cookie
12 ;(function () { 15 ;(function () {
13 'use strict' 16 const hasAdultCookie = document.cookie.split('; ').includes('adult=1')
17 if (!hasAdultCookie) {
18 const d = new Date()
19 d.setTime(d.getTime() + (100 * 24 * 60 * 60 * 1000))
20 document.cookie = `adult=1; expires=${d.toGMTString()}; path=/`
21 window.location.reload()
22 }
23 })()
14 24
15 const hasAdultCookie = document.cookie.split('; ').includes('adult=1') 25 // skip loading image through php which seems to take forever
26 ;(function () {
27 const itemImageRegexp = /photo\.php\?shinaban=(\d+)/
28 function fix (image) {
29 const origSrc = image.getAttribute('src')
30 if (origSrc == null) return
31 const found = origSrc.match(itemImageRegexp)
32 if (found == null) return
16 33
17 if (hasAdultCookie) { 34 const src = `https://www.suruga-ya.jp/database/pics_light/game/${found[1]}.jpg`
18 return 35 const setNotFound = () => {
36 image.removeEventListener('error', setNotFound)
37 image.setAttribute('src', 'https://www.suruga-ya.jp/database/images/no_photo.jpg')
38 }
39 image.addEventListener('error', setNotFound)
40 image.setAttribute('src', src)
19 } 41 }
20 42
21 const d = new Date() 43 function run (node) {
22 d.setTime(d.getTime() + (100 * 24 * 60 * 60 * 1000)) 44 if (!(node instanceof window.HTMLElement)) return
23 document.cookie = `adult=1; expires=${d.toGMTString()}; path=/` 45
24 window.location.reload() 46 fix(node)
25 }).call() 47 for (const image of node.querySelectorAll('img')) {
48 fix(image)
49 }
50 }
51
52 function onMutate (mutations) {
53 for (const mutation of mutations) {
54 for (const node of mutation.addedNodes) {
55 run(node)
56 }
57 }
58 }
59
60 const observer = new window.MutationObserver(onMutate)
61 observer.observe(document, { childList: true, subtree: true })
62 run(document.body)
63 })()