-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtampermonkey.js
234 lines (208 loc) · 6.18 KB
/
tampermonkey.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// ==UserScript==
// @name Jsdelivr Auto Fallback
// @namespace https://github.com/PipecraftNet/jsdelivr-auto-fallback
// @version 0.2.4
// @author PipecraftNet&DreamOfIce
// @description 修复 cdn.jsdelivr.net 无法访问的问题
// @homepage https://github.com/PipecraftNet/jsdelivr-auto-fallback
// @downloadURL https://greasyfork.org/scripts/445701-jsdelivr-auto-fallback/code/Jsdelivr%20Auto%20Fallback.user.js
// @updateURL https://greasyfork.org/scripts/445701-jsdelivr-auto-fallback/code/Jsdelivr%20Auto%20Fallback.user.js
// @supportURL https://github.com/PipecraftNet/jsdelivr-auto-fallback/issues
// @license MIT
// @match *://*/*
// @run-at document-start
// @grant GM.setValue
// @grant GM.getValue
// @grant GM_addElement
// ==/UserScript==
(async (document) => {
'use strict';
let fastNode;
let failed;
let isRunning;
const DEST_LIST = [
'cdn.jsdelivr.net',
'fastly.jsdelivr.net',
'gcore.jsdelivr.net',
'cdn.zenless.top',
'testingcf.jsdelivr.net',
'test1.jsdelivr.net'
];
const PREFIX = '//';
const SOURCE = DEST_LIST[0];
const starTime = Date.now();
const TIMEOUT = 2000;
const STORE_KEY = 'jsdelivr-auto-fallback';
const TEST_PATH = '/gh/PipecraftNet/jsdelivr-auto-fallback@main/empty.css?';
const shouldReplace = (text) => text && text.includes(PREFIX + SOURCE);
const replace = (text) => text.replace(PREFIX + SOURCE, PREFIX + fastNode);
const $ = document.querySelectorAll.bind(document);
const setAttributes = (element, attributes) => {
if (element && attributes) {
for (const name in attributes) {
if (Object.hasOwn(attributes, name)) {
const value = attributes[name];
if (value === undefined) {
continue;
}
element.setAttribute(name, value);
}
}
}
return element;
};
const createElement = (tagName, attributes) =>
setAttributes(document.createElement(tagName), attributes);
const addElement =
typeof GM_addElement === 'function'
? GM_addElement
: (parentNode, tagName, attributes) => {
if (!parentNode) {
return;
}
if (typeof parentNode === 'string') {
attributes = tagName;
tagName = parentNode;
parentNode = document.head;
}
const element = createElement(tagName, attributes);
parentNode.append(element);
return element;
};
const replaceElementSrc = () => {
let element;
let value;
for (element of $('link[rel="stylesheet"]')) {
value = element.href;
if (shouldReplace(value) && !value.includes(TEST_PATH)) {
element.href = replace(value);
}
}
for (element of $('script')) {
value = element.src;
if (shouldReplace(value)) {
addElement(element.parentNode, 'script', {
src: replace(value)
});
element.defer = true;
element.src = '';
element.remove();
}
}
for (element of $('img')) {
value = element.src;
if (shouldReplace(value)) {
// Used to cancel loading. Without this line it will remain pending status.
element.src = '';
element.src = replace(value);
}
}
// All elements that have a style attribute
for (element of $('*[style]')) {
value = element.getAttribute('style');
if (shouldReplace(value)) {
element.setAttribute('style', replace(value));
}
}
for (element of $('style')) {
value = element.innerHTML;
if (shouldReplace(value)) {
element.innerHTML = replace(value);
}
}
};
const tryReplace = () => {
if (!isRunning && failed && fastNode) {
console.warn(SOURCE + ' is not available. Use ' + fastNode);
isRunning = true;
setTimeout(replaceElementSrc, 0);
// Some need to wait for a while
setTimeout(replaceElementSrc, 20);
// Replace dynamically added elements
setInterval(replaceElementSrc, 500);
}
};
const checkAvailable = (url, callback) => {
let timeoutId;
const newNode = addElement(document.head, 'link', {
rel: 'stylesheet',
text: 'text/css',
href: url + TEST_PATH + starTime
});
const handleResult = (isSuccess) => {
if (!timeoutId) {
return;
}
clearTimeout(timeoutId);
timeoutId = 0;
// Used to cancel loading. Without this line it will remain pending status.
if (!isSuccess) newNode.href = 'data:text/css;base64,';
newNode.remove();
callback(isSuccess);
};
timeoutId = setTimeout(handleResult, TIMEOUT);
newNode.addEventListener('error', () => handleResult(false));
newNode.addEventListener('load', () => handleResult(true));
};
const cached = await (async () => {
try {
return Object.assign({}, await GM.getValue(STORE_KEY));
} catch {
return {};
}
})();
const main = () => {
cached.time = starTime;
cached.failed = false;
cached.fastNode = null;
for (const url of DEST_LIST) {
checkAvailable('https://' + url, (isAvailable) => {
// console.log(url, Date.now() - starTime, Boolean(isAvailable));
if (!isAvailable && url === SOURCE) {
failed = true;
cached.failed = true;
}
if (isAvailable && !fastNode) {
fastNode = url;
}
if (isAvailable && !cached.fastNode) {
cached.fastNode = url;
}
tryReplace();
});
}
setTimeout(() => {
// If all domains are timeout
if (failed && !fastNode) {
fastNode = DEST_LIST[1];
tryReplace();
}
GM.setValue(STORE_KEY, cached);
}, TIMEOUT + 100);
};
if (
cached.time &&
starTime - cached.time < 60 * 60 * 1000 &&
cached.failed &&
cached.fastNode
) {
failed = true;
fastNode = cached.fastNode;
tryReplace();
setTimeout(main, 1000);
} else if (document.head) {
main();
} else {
const observer = new MutationObserver(() => {
if (document.head) {
observer.disconnect();
main();
}
});
const observerOptions = {
childList: true,
subtree: true
};
observer.observe(document, observerOptions);
}
})(document);