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