-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimgur-proxy.js
More file actions
89 lines (80 loc) · 2.93 KB
/
Copy pathimgur-proxy.js
File metadata and controls
89 lines (80 loc) · 2.93 KB
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
// @name imgur-proxy
// @description Use DuckDuckGo's proxy to load Imgur images for people in the UK
// @author bud3699
// @version 1.1.1
(function imgurProxyExtension() {
const duckDuckGoProxy = "https://proxy.duckduckgo.com/iu/?u=";
function proxyImgurImages() {
const images = document.querySelectorAll("img");
images.forEach(img => {
const src = img.src;
if (
/imgur\.com/.test(src) &&
!src.startsWith(duckDuckGoProxy) &&
!decodeURIComponent(src).includes(duckDuckGoProxy)
) {
img.src = duckDuckGoProxy + encodeURIComponent(src);
}
});
}
function proxyImgurCSS() {
const duckDuckGoProxy = "https://proxy.duckduckgo.com/iu/?u=";
const allElements = document.querySelectorAll("*");
allElements.forEach(el => {
const computedBg = getComputedStyle(el).backgroundImage;
if (
computedBg &&
/imgur\.com/.test(computedBg) &&
!computedBg.includes(duckDuckGoProxy) &&
!decodeURIComponent(computedBg).includes(duckDuckGoProxy)
) {
const urlMatch = computedBg.match(/url\(["']?(https?:\/\/[^"')]+)["']?\)/);
if (urlMatch && urlMatch[1]) {
const proxiedUrl = duckDuckGoProxy + encodeURIComponent(urlMatch[1]);
el.style.backgroundImage = `url("${proxiedUrl}")`;
}
}
const inlineBg = el.style.backgroundImage;
if (
inlineBg &&
/imgur\.com/.test(inlineBg) &&
!inlineBg.includes(duckDuckGoProxy) &&
!decodeURIComponent(inlineBg).includes(duckDuckGoProxy)
) {
const urlMatch = inlineBg.match(/url\(["']?(https?:\/\/[^"')]+)["']?\)/);
if (urlMatch && urlMatch[1]) {
const proxiedUrl = duckDuckGoProxy + encodeURIComponent(urlMatch[1]);
el.style.backgroundImage = `url("${proxiedUrl}")`;
}
}
});
for (const sheet of document.styleSheets) {
try {
for (const rule of sheet.cssRules) {
if (
rule.style &&
rule.style.backgroundImage &&
/imgur\.com/.test(rule.style.backgroundImage) &&
!rule.style.backgroundImage.includes(duckDuckGoProxy) &&
!decodeURIComponent(rule.style.backgroundImage).includes(duckDuckGoProxy)
) {
const urlMatch = rule.style.backgroundImage.match(/url\(["']?(https?:\/\/[^"')]+)["']?\)/);
if (urlMatch && urlMatch[1]) {
const proxiedUrl = duckDuckGoProxy + encodeURIComponent(urlMatch[1]);
rule.style.backgroundImage = `url("${proxiedUrl}")`;
}
}
}
} catch (e) {
}
}
}
function proxyAllImgurContent() {
proxyImgurImages();
proxyImgurCSS();
}
proxyAllImgurContent();
const observer = new MutationObserver(() => proxyAllImgurContent());
observer.observe(document.body, { childList: true, subtree: true });
console.log("Imgur Proxy Extension loaded.");
})();