-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathauto_redirectLargestImageSrc.js
452 lines (421 loc) · 12.5 KB
/
auto_redirectLargestImageSrc.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import { UfsGlobal } from "./content-scripts/ufs_global.js";
export default {
icon: '<i class="fa-solid fa-up-right-and-down-left-from-center"></i>',
name: {
en: "Auto - view largest image",
vi: "Tự động - xem ảnh lớn nhất",
},
description: {
en: `<ul>
<li>When viewing an image in new tab.</li>
<li>This script will auto find and redirect to largest image.</li>
<li>Support hundred of websites.</li>
</ul>`,
vi: `<ul>
<li>Khi bạn mở xem ảnh trong tab mới.</li>
<li>Chức năng này sẽ tự động tìm và chuyển trang sang ảnh chất lượng cao nhất.</li>
<li>Hỗ trợ hàng trăm trang web.</li>
</ul>`,
img: "/scripts/auto_redirectLargestImageSrc.jpg",
},
buttons: [
{
icon: '<i class="fa-regular fa-circle-question"></i>',
name: {
vi: "Danh sách trang web",
en: "Supported websites",
},
onClick: () => {
window.open(
"/pages/viewScriptSource/index.html?file=auto_redirectLargestImageSrc_rules"
);
},
},
],
changeLogs: {
"2024-04-16": "init",
},
contentScript: {
onDocumentStart: async () => {
let oldHref = location.href;
check(oldHref);
window.onload = () =>
UfsGlobal.DOM.onHrefChanged((oldHref, newHref) => check(newHref));
async function check(href) {
let url = await getLargestImageSrc(href, href);
if (url && url != href) {
if (
confirm(
"Found bigger image. Redirect to that now?\n\nTìm thấy ảnh lớn hơn. Chuyển trang ngay?\n\n" +
url
)
) {
location.href = url;
}
}
}
},
},
};
const CACHED = {};
export async function getLargestImageSrc(imgSrc, webUrl) {
if (/^data:/i.test(imgSrc)) {
return null;
}
// bypass redirect
imgSrc = UfsGlobal.Utils.makeUrlValid(imgSrc);
let redirectedUrl = await UfsGlobal.Utils.getRedirectedUrl(imgSrc);
if (redirectedUrl) {
imgSrc = redirectedUrl;
}
function try1() {
const url = new URL(imgSrc);
switch (url.hostname) {
// https://atlassiansuite.mservice.com.vn:8443/secure/useravatar?size=small&ownerId=JIRAUSER14656&avatarId=11605
case "atlassiansuite.mservice.com.vn":
case "atlassiantool.mservice.com.vn":
if (url.href.includes("avatar")) {
if (url.searchParams.get("size")) {
url.searchParams.set("size", "256");
} else {
url.searchParams.append("size", "256");
}
}
if (url.href.includes("/thumbnail/")) {
return url.href.replace("/thumbnail/", "/attachments/");
}
if (url.href.includes("/thumbnails/")) {
return url.href.replace("/thumbnails/", "/attachments/");
}
return url.toString();
}
return null;
}
async function try2() {
if (!CACHED.largeImgSiteRules) {
let s = await import("./auto_redirectLargestImageSrc_rules.js");
CACHED.largeImgSiteRules = s.default;
}
for (let rule of CACHED.largeImgSiteRules) {
if (rule.url && !testRegex(webUrl, rule.url)) continue;
if (rule.src && !testRegex(imgSrc, rule.src)) continue;
if (rule.exclude && testRegex(imgSrc, rule.exclude)) continue;
if (rule.r) {
let newSrc = replaceUsingRegex(imgSrc, rule.r, rule.s);
if (newSrc?.length) {
return newSrc;
}
}
}
return null;
}
// https://greasyfork.org/en/scripts/2312-resize-image-on-open-image-in-new-tab
function try3() {
return new Promise((resolve) => {
let m = null;
//google
if (
(m = imgSrc.match(
/^(https?:\/\/lh\d+\.googleusercontent\.com\/.+\/)([^\/]+)(\/[^\/]+(\.(jpg|jpeg|gif|png|bmp|webp))?)(?:\?.+)?$/i
))
) {
if (m[2] != "s0") {
resolve(m[1] + "s0" + m[3]);
}
} else if (
(m = imgSrc.match(
/^(https?:\/\/lh\d+\.googleusercontent\.com\/.+=)(.+)(?:\?.+)?$/i
))
) {
if (m[2] != "s0") {
resolve(m[1] + "s0");
}
} else if (
(m = imgSrc.match(
/^(https?:\/\/\w+\.ggpht\.com\/.+\/)([^\/]+)(\/[^\/]+(\.(jpg|jpeg|gif|png|bmp|webp))?)(?:\?.+)?$/i
))
) {
if (m[2] != "s0") {
resolve(m[1] + "s0" + m[3]);
}
}
//blogspot
else if (
(m = imgSrc.match(
/^(https?:\/\/\w+\.bp\.blogspot\.com\/.+\/)([^\/]+)(\/[^\/]+(\.(jpg|jpeg|gif|png|bmp|webp))?)(?:\?.+)?$/i
))
) {
if (m[2] != "s0") {
resolve(m[1] + "s0" + m[3]);
}
}
//tumblr
else if (
(m = imgSrc.match(
/^(https?:\/\/\d+\.media\.tumblr\.com\/.*tumblr_\w+_)(\d+)(\.(jpg|jpeg|gif|png|bmp|webp))(?:\?.+)?$/i
))
) {
if (m[2] < 1280) {
let ajax = new XMLHttpRequest();
ajax.onreadystatechange = function () {
if (ajax.status == 200) {
resolve(m[1] + "1280" + m[3]);
}
};
ajax.open("HEAD", m[1] + "1280" + m[3], true);
ajax.send();
}
}
//twitter
else if (
(m = imgSrc.match(
/^(https?:\/\/\w+\.twimg\.com\/media\/[^\/:]+)\.(jpg|jpeg|gif|png|bmp|webp)(:\w+)?$/i
))
) {
let format = m[2];
if (m[2] == "jpeg") format = "jpg";
resolve(m[1] + "?format=" + format + "&name=orig");
} else if (
(m = imgSrc.match(/^(https?:\/\/\w+\.twimg\.com\/.+)(\?.+)$/i))
) {
let url = new URL(webUrl);
let pars = url.searchParams;
if (!pars.format || !pars.name) return;
if (pars.name == "orig") return;
resolve(m[1] + "?format=" + pars.format + "&name=orig");
}
//Steam (Only user content)
else if (
(m = imgSrc.match(
/^(https?:\/\/(images\.akamai\.steamusercontent\.com|steamuserimages-a\.akamaihd\.net)\/[^\?]+)\?.+$/i
))
) {
resolve(m[1]);
}
//性浪微博
else if (
(m = imgSrc.match(
/^(https?:\/\/(?:(?:ww|wx|ws|tvax|tva)\d+|wxt|wt)\.sinaimg\.(?:cn|com)\/)([\w\.]+)(\/.+)(?:\?.+)?$/i
))
) {
if (m[2] != "large") {
resolve(m[1] + "large" + m[3]);
}
}
//zhihu
else if (
(m = imgSrc.match(
/^(https?:\/\/.+\.zhimg\.com\/)(?:\d+\/)?([\w\-]+_)(\w+)(\.(jpg|jpeg|gif|png|bmp|webp))(?:\?.+)?$/i
))
) {
if (m[3] != "r") {
resolve(m[1] + m[2] + "r" + m[4]);
}
}
//pinimg
else if (
(m = imgSrc.match(/^(https?:\/\/i\.pinimg\.com\/)(\w+)(\/.+)$/i))
) {
if (m[2] != "originals") {
resolve(m[1] + "originals" + m[3]);
}
} else if (
(m = imgSrc.match(
/^(https?:\/\/s-media[\w-]+\.pinimg\.com\/)(\w+)(\/.+)$/i
))
) {
//need delete?
if (m[2] != "originals") {
resolve(m[1] + "originals" + m[3]);
}
}
//bilibili
else if (
(m = imgSrc.match(
/^(https?:\/\/\w+\.hdslb\.com\/.+\.(jpg|jpeg|gif|png|bmp|webp))(@|_).+$/i
))
) {
resolve(m[1]);
}
//taobao(tmall)
else if (
(m = imgSrc.match(
/^(https?:\/\/(?:.+?)\.alicdn\.com\/.+\.(jpg|jpeg|gif|png|bmp|webp))_.+$/i
))
) {
resolve(m[1]);
}
//jd
else if (
(m = imgSrc.match(
/^(https?:\/\/(?:img\d+)\.360buyimg\.com\/)((?:.+?)\/(?:.+?))(\/(?:.+?))(\!.+)?$/i
))
) {
if (m[2] != "sku/jfs") {
resolve(m[1] + "sku/jfs" + m[3]);
}
}
// https://s01.riotpixels.net/data/2a/b2/2ab23684-6cec-41da-9bce-f72c5264353a.jpg.240p.jpg
else if (
(m = imgSrc.match(
/^(https?:\/\/(?:.+?)\.riotpixels\.net\/.+\.(jpg|jpeg|gif|png|bmp|webp))\..+?$/i
))
) {
resolve(m[1]);
}
// reddit NEED TEST
else if (
(m = imgSrc.match(
/^https?:\/\/preview\.redd\.it\/(.+\.(jpg|jpeg|gif|png|bmp|webp))\?.+?$/i
))
) {
resolve("https://i.redd.it/" + m[1]);
}
// akamaized.net/imagecache NEED TEST
else if (
(m = imgSrc.match(
/^(https:\/\/.+\.akamaized\.net\/imagecache\/\d+\/\d+\/\d+\/\d+\/)(\d+)(\/.+)$/i
))
) {
if (m[2] < 1920) resolve(m[1] + "1920" + m[3]);
}
// 微信公众号 by sbdx
else if (
(m = imgSrc.match(
/^(https:\/\/mmbiz\.qpic\.cn\/mmbiz_jpg\/.+?\/)(\d+)(\?wx_fmt=jpeg)/i
))
) {
if (m[2] != 0) resolve(m[1] + "0" + m[3]);
}
//百度贴吧(然而对于画质提升什么的并没有什么卵用...)
else if (
(m = imgSrc.match(
/^https?:\/\/imgsrc\.baidu\.com\/forum\/pic\/item\/.+/i
))
) {
if (
(m = imgSrc.match(
/^(https?):\/\/(?:imgsrc|imgsa|\w+\.hiphotos)\.(?:bdimg|baidu)\.com\/(?:forum|album)\/.+\/(\w+\.(?:jpg|jpeg|gif|png|bmp|webp))(?:\?.+)?$/i
))
) {
resolve(m[1] + "://imgsrc.baidu.com/forum/pic/item/" + m[2]);
}
//if( (m = imgSrc.match(/^(https?)(:\/\/(?:imgsrc|imgsa|\w+\.hiphotos|tiebapic)\.(?:bdimg|baidu)\.com\/)(?:forum|album)\/.+\/(\w+\.(?:jpg|jpeg|gif|png|bmp|webp))(?:\?.+)?$/i)) ){
// resolve(m[1] + m[2] + "forum/pic/item/" + m[3])
//}
} else {
resolve(null);
}
});
}
for (let fn of [try1, try2, try3]) {
try {
let res = await timeoutPromise(fn(), 5000);
if (res && res != imgSrc) {
if (!Array.isArray(res)) res = [res];
if (res.length) {
let finalSrc = await findWorkingSrc(res, true);
if (finalSrc?.length) return finalSrc;
}
}
} catch (e) {
console.log("ERROR getLargestImageSrc: " + fn.name + " -> ", e);
}
}
return null;
}
function findWorkingSrc(srcs, inOrder = true) {
return new Promise((resolve, reject) => {
if (!srcs || !Array.isArray(srcs) || srcs.length === 0) {
reject("srcs is falsy, not an array, or empty");
} else {
const checkImage = (src) =>
// prevent Error: Content Security Policy directive: "connect-src 'self'
isImageSrc(src).then((value) => {
if (inOrder) return value;
if (value) resolve(src);
return value;
});
const promises = srcs.map(checkImage);
Promise.all(promises).then((res) => {
let trueIndex = res.indexOf(true);
if (trueIndex > -1) {
resolve(srcs[trueIndex]);
} else {
reject("none of the URLs are valid images");
}
});
}
});
}
async function isImageSrc(src) {
try {
const res = await UfsGlobal.Extension.fetchByPassOrigin(src, {
method: "HEAD",
});
if (res?.ok) {
// const type = res.headers.get("content-type");
const type = res.headers?.["content-type"];
if (type?.startsWith?.("image/")) {
return true;
}
}
} catch (error) {
console.log("ERROR isImageSrc: " + src + " -> ", error);
}
return new Promise((resolve) => {
let img = new Image();
img.src = src;
img.onload = () => resolve(true);
img.onerror = () => resolve(false);
});
}
function timeoutPromise(prom, time) {
return Promise.race([
prom,
new Promise((_r, rej) => setTimeout(() => rej("time out " + time), time)),
]);
}
function uniqueArray(array) {
return Array.from(new Set(array));
}
function replaceUsingRegex(str, r, s) {
let results = [];
if (!Array.isArray(r) && !Array.isArray(s)) {
if (r?.test?.(str)) {
results.push(str.replace(r, s));
}
} else if (!Array.isArray(r) && Array.isArray(s)) {
if (r?.test?.(str)) {
for (const si of s) {
results.push(str.replace(r, si));
}
}
} else if (Array.isArray(r) && !Array.isArray(s)) {
for (const ri of r) {
if (ri?.test?.(str)) {
results.push(str.replace(ri, s));
}
}
} else if (Array.isArray(r) && Array.isArray(s)) {
for (let ri = 0; ri < r.length; ri++) {
let _r = r[ri];
if (_r?.test?.(str)) {
let _s = Array.isArray(s[ri]) ? s[ri] : [s[ri]];
for (const si of _s) {
results.push(str.replace(_r, si));
}
}
}
}
return uniqueArray(results);
}
function testRegex(str, regexs) {
if (!Array.isArray(regexs)) regexs = [regexs];
for (let regex of regexs) {
if (regex?.test?.(str)) {
return true;
}
}
return false;
}