-
Notifications
You must be signed in to change notification settings - Fork 1
/
fantastic-fiction-high-res-images.user.js
54 lines (46 loc) · 1.65 KB
/
fantastic-fiction-high-res-images.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
// ==UserScript==
// @name High res images on fantasticfiction.com
// @namespace https://github.com/Alistair1231/my-userscripts/
// @version 0.3.0
// @description replaces low res images on hideoutshowcase with high res ones
// @author Alistair1231
// @match https://www.fantasticfiction.com/*
// @icon https://icons.duckduckgo.com/ip2/fantasticfiction.com.ico
// @license MIT
// ==/UserScript==
// https://github.com/Alistair1231/my-userscripts/raw/master/fantastic-fiction-high-res-images.user.js
(function () {
// From: https://m.media-amazon.com/images/I/516s5YwK2HL.SX316.SY480._SL500_.jpg
// To: https://m.media-amazon.com/images/I/516s5YwK2HL.jpg
// https://regex101.com/r/yoj8UD/1
const regex = /(?:[\._]+?S[XLY]\d+_?){3}(?=\.jpg)/gm;
const isAlreadyProcessed = (src) => !regex.test(src);
const replaceSrc = (img) => {
const newSrc = img.src.replace(regex, "");
if (img.src !== newSrc) {
img.src = newSrc;
}
};
const replaceAllImages = () => {
document.querySelectorAll("img").forEach(replaceSrc);
};
const replaceMutation = (mutationList) => {
mutationList.forEach((mutation) => {
if (mutation.type === "attributes" && mutation.attributeName === "src") {
const img = mutation.target;
if (!isAlreadyProcessed(img.src)) {
replaceSrc(img);
}
}
});
};
const observer = new MutationObserver(replaceMutation);
// Observe all src attribute changes
observer.observe(document.body, {
attributes: true,
subtree: true,
attributeFilter: ["src"],
});
// Run the function on initial load
replaceAllImages();
})();