-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathJS-File-Installer.js
90 lines (86 loc) · 1.84 KB
/
JS-File-Installer.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
$app.strings = {
"en": {
"title1": "Running Error",
"title2": "OK",
"title3": "Same File Name",
"title4": "Cancel",
"title5": "Replace",
"msg1": "Please run through a shared JS-file.",
"msg2": "Sure to REPLACE the existed file?",
"toast1": "Installed"
},
"zh-Hans": {
"title1": "运行有误",
"title2": "好的",
"title3": "文件名重复",
"title4": "取消",
"title5": "覆盖",
"msg1": "请从 JS 文件分享并以此扩展运行。",
"msg2": "确定要覆盖已有扩展文件?",
"toast1": "已安装"
}
}
function error() {
$ui.alert({
title: $l10n("title1"),
message: $l10n("msg1"),
actions: [{
title: $l10n("title2"),
style: "Cancel",
handler: function() {
$context.close()
$app.close()
}
}]
})
}
function warning(name, data) {
$ui.alert({
title: $l10n("title3"),
message: $l10n("msg2"),
actions: [{
title: $l10n("title4"),
style: "Cancel",
handler: function() {
$context.close()
$app.close()
}
},
{
title: $l10n("title5"),
handler: function() {
install(name, data)
}
}
]
})
}
function install(name, data) {
$addin.save({
name: fileName,
data: data
})
$ui.toast($l10n("toast1"), 1)
$delay(1.5, function() {
$context.close()
$app.close()
})
}
if (typeof($context.data) == "undefined") {
error()
} else {
var data = $context.data
var fileName = data.fileName
var addins = $file.extensions.join("|")
// Exclude file of wrong MIME type
if (fileName.indexOf(".js") == -1) {
error()
} else {
// Warn if file name exists
if (addins.indexOf(fileName.substr(0, fileName.length - 3)) > -1) {
warning(fileName, data)
} else {
install(fileName, data)
}
}
}