-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpixiv_reverse_proxy.user.js
68 lines (62 loc) · 1.72 KB
/
pixiv_reverse_proxy.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
// ==UserScript==
// @name pixiv reverse proxy
// @namespace https://github.com/x94fujo6rpg/SomeTampermonkeyScripts
// @version 0.0.2
// @description Reverse proxy all images. If you are experiencing 56k speeds on pixiv, try this. (I can't even load my own post. WTF)
// @author x94fujo6
// @match https://www.pixiv.net/*
// @match https://i.pximg.net/*
// @grant none
// @run-at document-start
// ==/UserScript==
/*jshint esversion: 6 */
(function () {
setInterval(() => {
main();
}, 1000);
function main() {
let url = window.location.href;
if (url.includes("//i.pximg.net")) {
window.location.href = url.replace("//i.pximg.net", "//i.pixiv.cat");
} else {
replaceLink();
replaceImg();
}
}
function replaceLink() {
let images = document.querySelectorAll("a[href*='i.pximg.net']");
if (!images) return;
images.forEach(a => {
if (!(a.getAttribute("reverse_proxy"))) {
let link = getProxy(a.href);
if (link) {
a.href = link;
a.setAttribute("reverse_proxy", true);
console.log(`reverse proxy [link]: ${link}`);
}
}
});
}
function replaceImg() {
let targets = document.querySelectorAll("img[src*='i.pximg.net']");
if (!targets) return;
targets.forEach(img => {
if (!(img.getAttribute("reverse_proxy"))) {
let link = getProxy(img.src);
if (link) {
img.src = link;
img.setAttribute("reverse_proxy", true);
console.log(`reverse proxy [img]: ${link}`);
}
}
});
}
function getProxy(link) {
let reg = /i\.pximg\.net\/(.*)/,
proxy = "https://i.pixiv.cat/",
extract;
if (!link) throw Error("empty link");
extract = reg.exec(link);
return extract ? `${proxy}${extract[1]}` : false;
}
})();