forked from xzhhbx/jsbox_script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
URLShorten.js
54 lines (40 loc) · 937 Bytes
/
URLShorten.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
/*
用途:识别剪贴板文本中所有链接,并将其转换为短链接
bypassDomain:
定义忽略转换的完整域名
*/
async function main() {
let content = $clipboard.text
let bypassDomain = ['t.me']
let urls = $detector.link(content).filter(url => {
return !bypassDomain.some(s => url.includes(`://${s}`))
})
let pms = urls.map(url => {
return shorten(url)
})
let shorts = await Promise.all(pms)
let result = urls.reduce((acc, cur, curIdx) => {
return acc.replace(cur, shorts[curIdx])
}, content)
$ui.alert({
title: `共计识别出${urls.length}个链接`,
message: result,
actions: [{
title: 'Copy',
handler: () => {
$clipboard.text = result
}
}, {
title: 'Cancel'
}]
})
}
await main()
async function shorten(url) {
let short = url
try {
short = await $http.shorten(url)
} catch(e) {
}
return short
}