-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (51 loc) · 1.52 KB
/
Copy pathscript.js
File metadata and controls
61 lines (51 loc) · 1.52 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
function redirectIfQueryIsLink() {
const urlParams = new URLSearchParams(window.location.search);
const paramsToCheck = ['redirect', 'url', 'link', 'target'];
for (const paramName of paramsToCheck) {
const value = urlParams.get(paramName);
if (value) {
const processedUrl = processUrl(value);
if (processedUrl) {
window.location.href = processedUrl;
return true;
}
}
}
return false;
}
function processUrl(input) {
// Remove any whitespace
input = input.trim();
// Check if it's already a valid URL
try {
new URL(input);
return input;
} catch {
// Not a valid URL, let's process it
}
// Check if it matches a domain pattern
const domainPattern = /^([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.[a-zA-Z]{2,}$/;
if (domainPattern.test(input)) {
return `https://${input}`;
}
// If it starts with www., add https://
if (input.startsWith('www.')) {
return `https://${input}`;
}
// Check if it might be a domain without protocol
if (input.includes('.') && !input.includes(' ') && !input.includes('http')) {
return `https://${input}`;
}
return null;
}
function manualRedirect() {
const input = document.getElementById('urlInput').value;
const processedUrl = processUrl(input);
if (processedUrl) {
window.location.href = processedUrl;
} else {
alert('Please enter a valid domain or URL');
}
}
// Check for redirect on page load
window.addEventListener('load', redirectIfQueryIsLink);