-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDownloader.js
148 lines (142 loc) · 3.34 KB
/
Downloader.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
/*
文件下载工具
1.通过 share extension 运行,直接下载链接内容
2.主程序内直接运行,自动填充剪贴板链接
3.通过 url scheme 运行,可指定 auto 自动开始下载
jsbox://run?name=Downloader&downloadUrl=https://domain.com/A.exe&auto=true
作者联系:https://t.me/axel_burks
*/
var file = null
var context_content = $context.query.downloadUrl || $context.link || $context.text
var content = context_content || ($clipboard.text ? $clipboard.text : null)
var auto = $context.query.auto || (context_content ? true : false)
if ($app.env == $env.safari) {
content = $context.safari.items.baseURI
auto = true
}
$ui.render({
props: {
title: "Downloader"
},
views: [
{
type: "label",
props: {
text: "Input URL:",
align: $align.left
},
layout: function(make, view) {
make.left.inset(25)
make.top.right.inset(20)
make.size.equalTo($size(64, 32))
}
},
{
type: "button",
props: {
id: "downloadButton",
title: "Download"
},
layout: function(make) {
make.top.inset(265)
make.right.inset(25)
make.size.equalTo($size(111, 32))
},
events: {
tapped: function(sender) {
download()
}
}
},
{
type: "button",
props: {
id: "shareButton",
title: "Share"
},
layout: function(make) {
make.top.inset(265)
make.right.equalTo($("downloadButton").left).inset(10)
make.size.equalTo($size(0, 0))
},
events: {
tapped: function(sender) {
share(file)
}
}
},
{
type: "text",
props: {
id: "inputUrl",
type: $kbType.url,
bgcolor: $rgba(100, 100, 100, 0.2),
radius: 12
},
layout: function(make) {
make.top.equalTo($("label").bottom).offset(5)
make.right.left.inset(20)
make.size.equalTo($size(64, 192))
},
events: {
ready: function(sender) {
if (content != null) {
sender.text = content
}
if (auto) {
download()
} else {
sender.focus()
}
}
}
}
]
})
function download() {
if ($("inputUrl").text.match(/^(https?|ftp):\/\/[^\s]+\/[^\s]+\.\w+/i)) {
var url = $("inputUrl").text.match(/^https?:\/\/[^\s]+\/[^\s]+\.\w+/i)[0]
$ui.toast("Downloading...")
$ui.loading(true)
$http.download({
url: url,
progress: function (bytesWritten, totalBytes) {
var percentage = bytesWritten * 1.0 / totalBytes
},
handler: function(resp) {
$ui.loading(false)
file = resp.data
$("shareButton").updateLayout(function(make) {
make.size.equalTo($size(100, 32))
})
share(file)
}
})
} else {
$ui.error("Please Input Correct URL!", 1)
$("inputUrl").focus()
}
}
function share(data) {
$share.sheet({
item: [data.fileName, data],
handler: function(success) {
if (success) {
$cache.clear()
delayClose(0.5)
}
}
})
}
function delayClose(time) {
$thread.main({
delay: time,
handler: function() {
if ($app.env == $env.action || $app.env == $env.safari) {
$context.close()
} else {
$app.close()
}
}
})
}