-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
167 lines (149 loc) · 4.43 KB
/
index.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const TIMEOUT = 10000; // ms
// Parse a list of URLs from the current URL
let urls = [];
{
let query = window.location.search;
if(query.length > 0 && query[0] === '?') {
query = query.substring(1);
}
if(query.length > 0) {
for(let q of query.split(',')) {
if(q.length > 0) {
urls.push(q);
}
}
}
}
let targetFound = false;
async function attempt(url) {
let response;
try {
response = await fetch(
url,
{
mode: 'no-cors',
method: 'HEAD'
}
);
} catch(e) {
console.log('failed:', url, e);
return;
}
if(response.status >= 400) {
console.log('failed:', url, 'status', response.status);
return;
}
if(targetFound) {
return;
}
targetFound = true;
// Start redirect
console.log('redirecting to', url);
document.getElementById('redirect-testing').style.display = 'none';
document.getElementById('redirect-found').style.display = '';
let link = document.getElementById('redirect-found').querySelector('a');
link.setAttribute('href', url);
link.innerText = url;
setTimeout(() => { window.location.href = url; }, 1000);
}
function timeoutReached() {
if(targetFound) {
return;
}
targetFound = true;
// Show error
console.log('timeout reached');
document.getElementById('redirect-testing').style.display = 'none';
document.getElementById('redirect-none').style.display = '';
}
if(urls.length > 0) {
// There are URLs present, redirect
console.log('urls:', urls);
// Show message
document.getElementById('placeholder').style.display = 'none';
document.getElementById('redirect').style.display = '';
Promise.all(
urls.map(attempt)
).then(() => {
if(targetFound) {
return;
}
targetFound = true;
// Show error
console.log('all failed');
document.getElementById('redirect-testing').style.display = 'none';
document.getElementById('redirect-none').style.display = '';
});
setTimeout(timeoutReached, 10000);
} else {
// No URLs present, show multi URL creation form
console.log('no urls');
document.getElementById('placeholder').style.display = 'none';
document.getElementById('new').style.display = '';
let inputs = [];
function onChange(e) {
// If an input changes to be empty
if(e.target.value !== '') {
return;
}
// Find the last of the empty inputs
let lastEmptyInput = null;
for(let idx = 0; idx < inputs.length; ++idx) {
if(inputs[idx].value === '') {
lastEmptyInput = idx;
}
}
// Remove all empty inputs except the last one
inputs = inputs.filter((input, idx) => {
if(idx != lastEmptyInput && input.value === '') {
input.removeEventListener('change', onChange);
input.removeEventListener('input', onInput);
input.parentNode.parentNode.removeChild(input.parentNode);
return false;
}
return true;
});
}
function onInput(e) {
// Update the result
let createUrls = [];
for(let input of inputs) {
if(input.value !== '') {
createUrls.push(input.value);
}
}
let resultUrl = 'https://remram44.github.io/multiurl/?' + createUrls.join(',');
let result = document.getElementById('create-result');
result.innerText = resultUrl;
result.setAttribute('href', resultUrl);
// If an input becomes not empty
if(e.target.value !== '') {
// Check whether there is an empty input
let hasEmptyInput = false;
for(let idx = 0; idx < inputs.length; ++idx) {
if(inputs[idx].value === '') {
hasEmptyInput = true;
}
}
// If none, add one
if(!hasEmptyInput) {
let randId = Math.floor(Math.random() * 100000000);
let newP = document.createElement('p');
newP.innerHTML = `<label for="url${randId}">URL:</label> <input type="text" id="url${randId}" class="url-input"></p>`;
document.getElementById('create').appendChild(newP);
let newInput = newP.querySelector('input.url-input');
inputs.push(newInput);
newInput.addEventListener('change', onChange);
newInput.addEventListener('input', onInput);
}
}
}
document.getElementById('create').querySelectorAll('input.url-input').forEach(input => {
inputs.push(input);
input.addEventListener('change', onChange);
input.addEventListener('input', onInput);
});
document.getElementById('create').addEventListener('submit', (e) => {
e.preventDefault();
});
}