-
Notifications
You must be signed in to change notification settings - Fork 11
/
redirect.js
150 lines (129 loc) · 4.06 KB
/
redirect.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
var currentURL = "";
var isHTTPS = true;
var index = 0;
var numberOfRedirects = 0;
var redirecting = false;
var cacheURL = [
"http://webcache.googleusercontent.com/search?q=cache:",
"http://web.archive.org/web/*/",
".nyud.net"
];
var cacheNames = [
"google-cache-sortable",
"wayback-machine-sortable",
"coral-cdn-sortable"
];
var URL_HASH = [
{ name: "google-cache-sortable", URL: "http://webcache.googleusercontent.com/search?q=cache:" },
{ name: "wayback-machine-sortable", URL: "http://web.archive.org/web/*/" },
{ name: "coral-cdn-sortable", URL: ".nyud.net" }
];
function getURL() {
++numberOfRedirects;
if(cacheURL[index] == cacheURL.length) {
index = 0;
}
if(cacheURL[index] != ".nyud.net") { // Google and Wayback Machine
return cacheURL[index] + (isHTTPS ? currentURL.substr(8) : currentURL.substr(7));
} else { // Coral CDN
if(currentURL.slice(-1) == "/") {
return currentURL.substring(0, currentURL.length -1) + ".nyud.net";
} else {
return currentURL + ".nyud.net";
}
}
}
function handler(details) {
//console.log("StatusLine is: " + details.statusLine);
if(numberOfRedirects > cacheURL.length) { // There is no cache available
chrome.webRequest.onHeadersReceived.removeListener(handler);
redirecting = false;
return { cancel: true };
}
if(~details.statusLine.search("404")) { // Not found
++index;
return { redirectUrl:getURL() };
} else if(~details.statusLine.search("403")) { // Forbidden
++index;
return { redirectUrl:getURL() };
} else { // Success
chrome.webRequest.onHeadersReceived.removeListener(handler);
redirecting = false;
return { cancel: false };
}
}
function openPage(currentTab) {
chrome.storage.sync.get("cacheOrder4", function(result) {
var cacheOrder = result["cacheOrder4"] || cacheNames;
URL_HASH.sort(function(a, b) {
return cacheOrder.indexOf(a.name) - cacheOrder.indexOf(b.name);
});
for(var i = 0; i < 3; i++) {
cacheURL[i] = URL_HASH[i].URL;
cacheNames[i] = URL_HASH[i].name;
}
// console.log(cacheURL);
index = 0;
numberOfRedirects = 0;
redirecting = true;
chrome.webRequest.onHeadersReceived.addListener(
handler,
{
urls: ["<all_urls>"],
types: ["main_frame"]
},
["blocking"]
);
currentURL = currentTab.url;
isHTTPS = (currentURL.substring(0, 6) == "https:");
//console.log("the tab id for the redirect is " + currentTab.id + ": The website is: " + currentURL);
chrome.tabs.update(currentTab.id, { url: getURL(index) });
});
}
function autoRedirect(details) {
chrome.tabs.query({}, function(tabs) {
// Make sure it is the current tab
var tab = null;
tabs.forEach(function(t) {
if(t.url === details.url) { // What if multiple tabs open with same url?
tab = t;
}
});
// Return if no tab with url or redirecting
if(tab == null || redirecting)
return;
if(~details.statusLine.indexOf("408")) {
console.log("408 redirect");
} else if(~details.statusLine.indexOf("503")) {
console.log("503 redirect");
openPage(tab);
} else if(~details.statusLine.indexOf("521")) {
console.log("521 redirect");
openPage(tab);
} else if(~details.statusLine.indexOf("522")) {
console.log("522 redirect");
openPage(tab);
}
});
}
chrome.storage.sync.get("auto-detect", function(result) {
if(result["auto-detect"] == "on") {
chrome.webRequest.onHeadersReceived.addListener(
autoRedirect,
{ urls: ["<all_urls>"] }
);
}
});
// Set defaults on install or update
chrome.runtime.onInstalled.addListener(function(details) {
chrome.storage.sync.get("cacheOrder", function(result) {
if(Object.keys(result).length == 0) {
var saveObject = {
"cacheOrder4": cacheNames,
"auto-detect": "off"
};
chrome.storage.sync.set(saveObject);
}
});
});
chrome.browserAction.onClicked.addListener(openPage);