forked from GzhiYi/Scriptables
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloader.github.js
More file actions
Latest commit
161 lines (157 loc) · 4.8 KB
/
Copy pathloader.github.js
File metadata and controls
161 lines (157 loc) · 4.8 KB
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
//
// scriptable 加载器
// 用于加载远程 scriptable 桌面组件插件
// author@im3x
// 公众号@古人云
// https://github.com/im3x/Scriptables
//
classIm3xLoader{
constructor(git='github'){
// 仓库源
this.git=git
this.ver=202010131700
// 解析参数
this.opt={
name: 'welcome',
args: '',
version: 'latest',
developer: 'im3x'
}
letarg=args.widgetParameter||args['queryParameters']['__widget__']
// widget@version:params
// 第三方开发者源:user-name/widget@version:params
if(arg){
let_args=arg.split(":")
let_plug=_args[0].split("@")
if(_plug.length===2){
this.opt['version']=_plug[1]
}
let_name=_plug[0].split('/')
if(_name.length===2){
this.opt['name']=_name[1]
this.opt['developer']=_name[0]
}else{
this.opt['name']=_name[0]
}
if(_args.length===2)this.opt['args']=_args[1]
}
// 缓存路径
this.filename=`${this.opt['developer']}_${this.opt['name']}@${this.opt['version']}.js.im3x`
this.filepath=FileManager.local().documentsDirectory()+'/'+this.filename
this.notify()
this.update()
}
asyncinit(){
// 判断文件是否存在
letrendered=false
letwidget
if(FileManager.local().fileExists(this.filepath)){
try{
rendered=true
widget=awaitthis.render()
}catch(e){
rendered=false
}
}
// 加载代码,存储
try{
letreq=newRequest(`https://${this.git}.com/${this.opt['developer']}/Scriptables/raw/main/${encodeURIComponent(this.opt['name'])}/${encodeURIComponent(this.opt['version'])}.js?_=${+newDate}`)
letdata=awaitreq.loadString()
// 如果404
if(req.response['statusCode']===404){
returnawaitthis.renderFail('插件不存在')
}
awaitFileManager.local().writeString(this.filepath,data)
if(!rendered){
widget=awaitthis.render()
}
}catch(e){
// 网络加载失败,返回错误提示
// 如果已经渲染了(有本地缓存,直接返回本地代码)
if(rendered)returnwidget
returnawaitthis.renderFail(e.message)
}
returnwidget
}
// 加载失败提示
asyncrenderFail(err){
letw=newListWidget()
lett1=w.addText("⚠️")
t1.centerAlignText()
w.addSpacer(10)
lett2=w.addText(err)
t2.textColor=Color.red()
t2.font=Font.lightSystemFont(14)
t2.centerAlignText()
w.url=`https://github.com/${this.opt['developer']}/Scriptables`
returnw
}
// 初始化组件并渲染
asyncrender(){
letM=importModule(this.filename)
letm=newM(this.opt['args'],this)
// 执行组件自定义方法操作
if(!config.runsInWidget&&typeofm['runActions']==='function'){
try{
letfunc=m.runActions.bind(m)
awaitfunc()
}catch(e){
letalert=newAlert()
alert.title="执行失败"
alert.message=e.message
alert.presentAlert()
}
returnfalse
}
letw=awaitm.render()
returnw
}
// 通知
asyncnotify(){
letreq=newRequest(`https://${this.git}.com/im3x/Scriptables/raw/main/update.notify.json?_=${+newDate}`)
letres=awaitreq.loadJSON()
if(!res||!res['id'])return
// 判断是否已经通知过
letkey='im3x_loader_notify'
if(Keychain.contains(key)){
letcache=Keychain.get(key)
if(cache===res['id'])return
}
// 通知
letn=newNotification()
n=Object.assign(n,res)
n.schedule()
// 设置已通知
Keychain.set(key,res['id'])
}
// 更新加载器
asyncupdate(){
letreq=newRequest(`https://gitee.com/api/v5/repos/im3x/Scriptables/commits?path=loader.${this.git}.js&page=1&per_page=1`)
letres=awaitreq.loadJSON()
letcommit=res[0]
letkey='im3x_loader_update'
if(Keychain.contains(key)){
letcache=Keychain.get(key)
if(cache===commit['sha'])return
}
// 加载远程代码内容
letreq1=newRequest(`https://gitee.com/im3x/Scriptables/raw/main/loader.${this.git}.js`)
letres1=awaitreq1.loadString()
// 当前脚本的路径
letself=module.filename
// 读取前三行代码(包含图标信息)
letselfContent=FileManager.local().readString(self)
lettmp=selfContent.split("\n")
// 放到前三行
letnew_code=`${tmp[0]}\n${tmp[1]}\n${tmp[2]}\n${res1}`
// 写入文件
FileManager.local().writeString(self,new_code)
Keychain.set(key,commit['sha'])
}
}
constLoader=newIm3xLoader()
constwidget=awaitLoader.init()
if(config.runsInWidget&&widget){
Script.setWidget(widget)
}
Script.complete()