-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchive-redirect.user.js
More file actions
214 lines (194 loc) · 7.28 KB
/
archive-redirect.user.js
File metadata and controls
214 lines (194 loc) · 7.28 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
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
// ==UserScript==
// @name Internet Archive Redirect
// @namespace http://srsutherland.dev
// @version 2024.11.20.2
// @author srsutherland
// @description Redirect error pages to the internet archive
// @match *://*/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=web.archive.org
// @grant none
// ==/UserScript==
(function () {
class ArchiveRedirect {
constructor() {
this.run();
}
async _fetch_latest_archive(url) {
const api = `https://archive.org/wayback/available?url=${encodeURIComponent(url)}`;
return fetch(api)
.then(response => response.json())
.then(
data => {
this._data = data;
const new_url = data.archived_snapshots?.closest?.url
if (!new_url) {
console.log('No Archive Found');
this._no_archive = true;
}
return new_url ? new_url : `https://web.archive.org/web/20250000000000*/${url}`
}
).then(
latest_archive => {
console.log('Latest Archive is ', latest_archive);
return latest_archive;
}
);
}
async latest_archive() {
const this_url = window.location.href;
if (this._latest_archive == undefined) {
this._latest_archive = await this._fetch_latest_archive(this_url);
}
return this._latest_archive;
}
async redirect_to_archive() {
const hourglass_emoji = '⏳';
console.log('Redirecting to Archive');
this.append_to_buttons(' ' + hourglass_emoji);
const archive_url = await this.latest_archive();
console.log('Redirecting to ', archive_url);
window.location = archive_url;
}
check_for_404() {
const status = document.querySelector('meta[name="robots"][content="noindex"]');
if (status) {
this.redirect_to_archive();
}
}
is_error_in_title() {
const title = document.title;
const possible_errors = [
'404',
//'Error',
'Not Found',
'Page Not Found',
'500',
'503',
'403',
];
// These sites might give false positives because they *discuss* errors
const whitelist_domains = [
'google.com',
'wikipedia.org',
'github.com',
'stackoverflow.com',
'serverfault.com',
'superuser.com',
'youtube.com',
'reddit.com',
];
if (whitelist_domains.some(domain => window.location.href.includes(domain))) {
return false;
}
return possible_errors.some(error => title.toLowerCase().includes(error.toLowerCase()));
}
cloudflare_error() {
const cf_error = document.querySelector('#cf-error-details');
return cf_error != null;
}
is_blocked() {
const title = document.title;
const possible_titles = [
"Internet Security by Zscaler"
];
return possible_titles.some(error => title.includes(error));
}
is_bad_state() {
const trigger =
this.is_error_in_title() ||
this.cloudflare_error() ||
this.is_blocked();
return trigger;
}
referrer_is_hugger() {
const referrer = document.referrer;
const huggers = [
'news.ycombinator.com',
'reddit.com',
];
return huggers.some(hugger => referrer.includes(hugger));
}
get_always_redirect() {
const value = localStorage.getItem('always_redirect');
if (value == 'true') {
return true;
}
if (value == 'false') {
return false;
}
return value;
}
set_always_redirect(value = true) {
localStorage.setItem('always_redirect', value);
}
run() {
if (!this.is_bad_state()) {
return;
}
this.latest_archive();
if (this.get_always_redirect() == true) {
console.log('"Always Redirect" is set');
this.redirect_to_archive();
}
if (this.referrer_is_hugger()) {
console.log(`Referrer ${document.referrer} is a known hugger`);
this.redirect_to_archive();
}
// else, create two buttons
this.draw_ui();
}
draw_ui() {
const head = document.querySelector('head');
const styles = document.createElement('style');
styles.id = 'archive-redirect-styles';
styles.innerHTML = `
.archive-redirect-container {
position: fixed;
top: 0;
left: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 100vw;
}
.archive-redirect-button {
padding: 1em;
margin: 1em;
border: none;
background-color: #f33; /* red */
color: white;
cursor: pointer;
}
`;
head.appendChild(styles);
const body = document.querySelector('body');
const container = document.createElement('div');
container.classList.add('archive-redirect-container');
const redirect_button = document.createElement('button');
redirect_button.innerText = 'Redirect to Archive';
redirect_button.classList.add('archive-redirect-button');
redirect_button.addEventListener('click', () => this.redirect_to_archive());
this.redirect_button = redirect_button;
const always_button = document.createElement('button');
always_button.innerText = 'Always Redirect to Archive';
always_button.classList.add('archive-redirect-button');
always_button.addEventListener('click', () => {
this.set_always_redirect();
this.redirect_to_archive();
});
this.always_button = always_button;
container.appendChild(redirect_button);
container.appendChild(always_button);
body.appendChild(container);
}
append_to_buttons(text) {
const buttons = document.querySelectorAll('.archive-redirect-button');
buttons.forEach(button => {
button.innerText += text;
});
}
}
const archiveRedirect = new ArchiveRedirect();
window.archive_redirect_extension = archiveRedirect;
})();