-
Notifications
You must be signed in to change notification settings - Fork 1
/
Imgur_Direct.user.js
98 lines (84 loc) · 2.76 KB
/
Imgur_Direct.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
// ==UserScript==
//
// @name Imgur Direct
// @version 1.3
// @namespace https://greasyfork.org/en/users/667743-catspinner
// @description See Imgur images & videos directly.
// @icon https://imgur.com/favicon.ico
// @license MIT
//
// @updateURL https://greasyfork.org/scripts/413400-imgur-direct/code/Imgur%20Direct.meta.js
// @downloadURL https://greasyfork.org/scripts/413400-imgur-direct/code/Imgur%20Direct.user.js
//
// @include /^https://imgur\.com/[a-zA-Z0-9]{5,8}$/
// @exclude https://imgur.com/gallery
// @exclude https://imgur.com/upload
//
// @run-at document-start
//
// ==/UserScript==
//
/*========================= Version History ==================================
1.00 - First public release.
1.1 - Fixed Imgur's jpg/jpeg shenanigans.
Shows images faster.
Strict @include rule to define where script can run.
1.2 - Fixed a typo in @include regex.
1.3 - Just some meta changes.
==============================================================================*/
function runImgurDirect() {
const imgname = location.href.replace('https://imgur.com/', 'https://i.imgur.com/'),
imgname_jpeg = imgname + '.jpeg',
imgname_jpg = imgname + '.jpg',
imgname_png = imgname + '.png',
imgname_gif = imgname + '.gif',
imgname_mp4 = imgname + '.mp4',
imgname_gifv = imgname + '.gifv';
const url = location.href;
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'text';
xhr.send();
xhr.onload = function() {
const page = xhr.response;
var video = true;
var trueimage = location.href;
if (page.indexOf("og:video") == -1) {
video = false;
} if (page.indexOf(imgname_png) > -1) {
trueimage = imgname_png;
} else if (page.indexOf(imgname_jpeg) > -1) {
trueimage = imgname_jpeg;
} else if (page.indexOf(imgname_jpg) > -1) {
trueimage = imgname_jpg;
}
if (video) {
if (countOccurrence(page, imgname_gif) > 2) {
trueimage = imgname_gif;
} else if (countOccurrence(page, imgname_mp4) > 2) {
trueimage = imgname_mp4;
} else if (countOccurrence(page, imgname_gifv) > 2) {
trueimage = imgname_gifv;
}
}
if (url !== trueimage) {
window.location.replace(trueimage);
} else {
return;
}
}
}
function countOccurrence(string, subString) {
var n = 0,
pos = 0,
step = subString.length;
while (true) {
pos = string.indexOf(subString, pos);
if (pos >= 0) {
++n;
pos += step;
} else break;
}
return n;
}
runImgurDirect();