-
Notifications
You must be signed in to change notification settings - Fork 6
/
background.js
59 lines (51 loc) · 2.07 KB
/
background.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
var reloadCount = 0;
// Redirect imgur.com requests to Duckduckgo proxy
chrome.webRequest.onBeforeRequest.addListener(function (details) {
var redirectUrl = "https://proxy.duckduckgo.com/iu/?u=" + details.url.replace("https://", "http://");
redirectUrl = redirectUrl.replace(/ref=.*&|ref=.*$/, "");
return {redirectUrl: redirectUrl};
}, {
urls: ["*://*.imgur.com/*"]
}, ["blocking"]);
chrome.webRequest.onHeadersReceived.addListener(function (details) {
// If HTTP status is not OK, try again
if (details.frameId === 0 && details.statusCode !== 200 && reloadCount < 2) {
var code = 'window.location.reload();';
chrome.tabs.executeScript(
details.tabId,
{code: code},
function (results) {
var e = chrome.runtime.lastError;
}
);
reloadCount++;
}
var contentType = '';
var lastIndex = 0;
$.each(details.responseHeaders, function (index, item) {
// Modify valid JS sources when imgur page loads from Duckduckgo proxy
if (item.name === "content-security-policy" && item.value.indexOf("script-src") >= 0) {
details.responseHeaders[index].value = "script-src self http: https: about: 'unsafe-inline' 'unsafe-eval'";
}
// Get content type
if (item.name === "content-type") {
contentType = item.value;
}
lastIndex = index;
});
// if this is an image or video, set a proper filename
if (contentType.startsWith("image/") || contentType.startsWith("video/")) {
details.responseHeaders[lastIndex + 1] = {
name: "content-disposition",
value: "filename=" + details.url.split('/').pop()
};
}
return {responseHeaders: details.responseHeaders};
}, {
urls: [
"*://proxy.duckduckgo.com/iu/?u=http://imgur.com*",
"*://proxy.duckduckgo.com/iu/?u=https://imgur.com*",
"*://proxy.duckduckgo.com/iu/?u=http://*.imgur.com*",
"*://proxy.duckduckgo.com/iu/?u=https://*.imgur.com*",
]
}, ["responseHeaders", "blocking"]);