-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
136 lines (121 loc) · 4.42 KB
/
script.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
function selectService(service) {
document.getElementById('service-buttons').style.display = 'none';
document.getElementById('shorten-url-container').style.display = 'block';
localStorage.setItem('selectedService', service);
}
function shortenUrl() {
const longUrl = document.getElementById('long-url').value;
const selectedService = localStorage.getItem('selectedService');
if (selectedService === 'ulvis') {
shortenWithUlvis(longUrl);
} else if (selectedService === '1pt') {
shortenWith1pt(longUrl);
} else if (selectedService === 'cleanuri') {
shortenWithCleanURI(longUrl);
} else if (selectedService === 'owo') {
shortenWithOwo(longUrl);
}
}
function copyToClipboard() {
const shortUrlInput = document.getElementById('shortened-url');
shortUrlInput.select();
document.execCommand('copy');
alert('Copied to clipboard');
}
function reset() {
document.getElementById('long-url').value = '';
document.getElementById('service-buttons').style.display = 'block';
document.getElementById('shorten-url-container').style.display = 'none';
document.getElementById('shortened-url-container').style.display = 'none';
}
function shortenWithUlvis(longUrl) {
const customText = generateRandomString(8);
const apiUrl = `https://corsproxy.io/?https://ulvis.net/api.php?url=${encodeURIComponent(longUrl)}&custom=${customText}&private=1`;
fetch(apiUrl)
.then(response => response.text())
.then(shortUrl => {
displayShortUrl(shortUrl);
})
.catch(error => console.error('Error:', error));
}
function shortenWith1pt(longUrl) {
const apiUrl = 'https://csclub.uwaterloo.ca/~phthakka/1pt/addURL.php';
const customUrlInput = document.getElementById('custom-url');
const customUrl = customUrlInput ? remove(customUrlInput.value, ['1pt.co/', '/', '\\?']) : '';
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 201) {
const data = JSON.parse(this.responseText);
const returnedShortUrl = `1pt.co/${data.short}`;
const showWarning = customUrl !== '' && data.short !== customUrl;
displayShortUrl(returnedShortUrl);
}
};
const requestUrl = `${apiUrl}?url=${encodeURIComponent(longUrl)}&cu=${encodeURI(customUrl)}`;
xhttp.open('GET', requestUrl, true);
xhttp.send();
}
function shortenWithCleanURI(longUrl) {
const apiUrl = 'https://corsproxy.io/?https://cleanuri.com/api/v1/shorten';
const data = { url: longUrl };
fetch(apiUrl, {
method: 'POST',
body: new URLSearchParams(data),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(response => response.json())
.then(data => {
if (data.result_url) {
displayShortUrl(data.result_url);
} else {
console.error('Error:', data);
}
})
.catch(error => console.error('Error:', error));
}
function shortenWithOwo(longUrl) {
const apiUrl = 'https://corsproxy.io/?https://owo.vc/api/v2/link';
const data = {
link: longUrl,
generator: 'owo',
metadata: 'IGNORE'
};
fetch(apiUrl, {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
'User-Agent': 'ShorterURLs'
}
})
.then(response => {
if (!response.ok) {
return response.json().then(data => { throw new Error(JSON.stringify(data)) });
}
return response.json();
})
.then(data => {
if (data.id) {
const shortUrl = `https://${data.id}`;
displayShortUrl(shortUrl);
} else {
console.error('Error:', data);
}
})
.catch(error => console.error('Error:', error));
}
function displayShortUrl(shortUrl) {
document.getElementById('shortened-url').value = shortUrl;
document.getElementById('shorten-url-container').style.display = 'none';
document.getElementById('shortened-url-container').style.display = 'block';
}
function generateRandomString(length) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
}