-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
shortenURL.js
262 lines (245 loc) · 7.7 KB
/
shortenURL.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { showLoading } from "./helpers/utils.js";
export default {
icon: "https://lh3.googleusercontent.com/jm2UEmhuC3c6L5zWae0zuefgl-Azjz541WpEwvNtXwaJl7H8I9U0zRRmDRdnbvmdZ6UYhTGQJ0QVCrZXvkGIy14HFA=w128-h128-e365-rj-sc0x00ffffff",
name: {
en: "Shorten URL",
vi: "Rút gọn link",
},
description: {
en: "Support tinyurl, tnyim, cuttly, bitly, j2team, ...",
vi: "Hỗ trợ tinyurl, tnyim, cuttly, bitly, j2team, ...",
},
onClickExtension: function () {
// https://hyperhost.ua/tools/en/surli
// https://www.shorturl.at/shortener.php
// https://tinyurl.com/app
// https://cutt.ly/
// https://bom.so/
// Source code extracted from https://chrome.google.com/webstore/detail/url-shortener/godoifjoiadanijplaghmhgfeffnblib/related?hl=vi
const urlShorten = [
{
name: "tinyurl",
func: async function (url) {
let resp = await fetch(
"https://tinyurl.com/api-create.php?url=" + encodeURIComponent(url)
);
let text = await resp.text();
if (text.length < 50) {
const shorturl = text.replace("http://", "https://");
return shorturl;
}
throw "Unable to find url";
},
},
{
name: "is.gd",
func: async function (url) {
url = encodeURIComponent(url);
let resp = await fetch(
"https://is.gd/create.php?format=json&url=" + url + "&logstats=1"
);
if (resp.status !== 200) throw "Unable to find url";
let json = await resp.json();
return json.shorturl;
},
},
{
name: "v.gd",
func: async function (url) {
url = encodeURIComponent(url);
let resp = await fetch(
"https://v.gd/create.php?format=json&url=" + url + "&logstats=1"
);
if (resp.status !== 200) throw "Unable to find url";
let json = await resp.json();
return json.shorturl;
},
},
{
name: "tny.im",
func: async function (url) {
let resp = await fetch(
"https://tny.im/yourls-api.php?format=json&action=shorturl&url=" +
encodeURIComponent(url)
);
let data = await resp.text();
let shorturl = JSON.parse(data).shorturl.replace(
"http://",
"https://"
);
return shorturl;
},
},
{
name: "bom.so",
func: async function (url) {
let formData = new FormData();
formData.append("url", url);
let resp = await fetch("https://bom.so/shorten", {
method: "POST",
body: formData,
});
let json = await resp.json();
if (json.error) throw json.msg;
return json.short;
},
},
// {
// name: "a.priv.sh",
// func: async function (url) {
// let resp = await fetch("https://a.priv.sh", {
// method: "POST",
// headers: { "Content-Type": "application/json" },
// body: JSON.stringify({ url: url }),
// });
// let json = await resp.json();
// if (json.message !== "success")
// throw "That URL doesn't quite look right...";
// return json.url;
// },
// },
{
name: "cuttly",
note: "cần API key",
func: function (url) {
return new Promise((resolve, reject) => {
chrome.storage.local.get(["cuttlyApiKey"], async function (res) {
let apiKey = res.cuttlyApiKey || "";
apiKey = prompt("Enter cuttly API key:", apiKey);
if (apiKey) {
chrome.storage.local.set({ cuttlyApiKey: apiKey });
try {
let longurl = encodeURIComponent(url);
let resp = await fetch(
"https://ifsc-code.in/urlShorten?longUrl=" +
longurl +
"&api=" +
apiKey
);
let text = await resp.text();
let json = JSON.parse(text);
if (json.url.status === 4) {
reject("Unable to find url");
}
resolve(json.url.shortLink);
} catch (e) {
reject("ERROR: " + e);
}
}
});
});
},
},
{
name: "bitly",
note: "cần API key",
func: function (url) {
return new Promise((resolve, reject) => {
chrome.storage.local.get(["bitlyApiKey"], async function (res) {
let apiKey = res.bitlyApiKey || "";
apiKey = prompt("Enter bitly API key:", apiKey);
if (!apiKey) {
reject("Invalid API key");
}
chrome.storage.local.set({ bitlyApiKey: apiKey });
try {
let longurl = encodeURIComponent(url);
let resp = await fetch(
"https://api-ssl.bitly.com/v3/shorten?access_token=" +
apiKey +
"&longUrl=" +
longurl +
"&domain=bit.ly&"
);
let text = await resp.text();
let json = JSON.parse(text);
if (json.status_txt === "INVALID_ARG_ACCESS_TOKEN") {
reject("Invalid API key");
}
resolve(json.data.url);
} catch (e) {
reject("ERROR: " + e);
}
});
});
},
},
{
name: "j2team",
note: "mở tab mới",
func: function (url) {
let longUrl = encodeURIComponent(url);
window.open(
`https://j2team.dev/home/?prefill_url=${longUrl}&utm_source=useful-scripts-extension`
);
},
},
];
(async () => {
let index = prompt(
"Shorten URL / Rút gọn link\n\n" +
urlShorten
.map(
(_, i) => ` ${i}: ${_.name} ${_.note ? "(" + _.note + ")" : ""}`
)
.join("\n"),
0
);
if (index !== null && index >= 0 && index < urlShorten.length) {
let shortener = urlShorten[index];
let url = prompt(
`Enter URL want to shorten:\nNhập link muốn rút gọn: (${shortener.name})`,
""
);
if (url) {
const { closeLoading } = showLoading("Đang tạo link rút gọn...");
try {
let shortUrl = await shortener?.func?.(url);
if (shortUrl)
prompt(
`Shorten URL / Link rút gọn (${shortener.name}):`,
shortUrl
);
else
alert("Unable to get short link / Không lấy được link rút gọn");
} catch (e) {
alert("ERROR " + e);
} finally {
closeLoading();
}
}
}
})();
},
};
function backup() {
let div = /*html*/ `<div class="container">
<div class="inner-container">
<button id="close-btn">Close</button>
</div>
</div>
<style>
.container {
position: fixed;
top:0;left:0;right:0;bottom:0;
background: #333e;
display: flex;
justify-content: center;
}
.inner-container {
position: relative;
background: #aaa;
}
#close-btn {
position: absolute;
top: 0; right: 0;
padding: 5px 10px;
}
</style>`;
let child = document.createElement("div");
child.innerHTML = div;
document.body.appendChild(child);
document.querySelector("#close-btn")?.addEventListener("click", function () {
child.remove();
});
}