-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathScriptableToolKit.js
More file actions
955 lines (872 loc) · 35.9 KB
/
ScriptableToolKit.js
File metadata and controls
955 lines (872 loc) · 35.9 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
/**
* 根据自己的习惯整合各个开发者而形成的工具包
* 并且加入一些好用的方法
* 方法如下:
* isEmpty: 判断字符串是否是空(undefined,null,空串)
* randomString: 生成随机字符串
* autoComplete: 自动补齐字符串
* customReplace: 自定义替换
* formatDate: 日期格式化
* formatTimeDuring: 毫秒数转换成n天m小时
* fileLengthFormat: 文件大小单位转换
*
* 基于Scriptable的api封装的方法(用法可以参考该目录下/example中的demo):
* require({scriptName, url = '', reload = false}): 引入第三方js库
* generateInputAlert: 生成带文本框的弹窗
* generateAlert: 生成弹窗
* widgetCutBg: 设置widget背景
*
* ⚠请在执行失败的地方执行execFail()
*
* @param scriptName 脚本名,用于通知时候的标题
* @param scriptId 每个脚本唯一的id,用于存储持久化的时候加入key
* @param options 传入一些参数,目前参数如下;
* lkIsSaveLog{scriptId} boolean : 保存日志到iCloud(目录:scriptable/lklogs/{scriptId}/)
* lkIsEnableLog{scriptId} boolean
* @constructor
*/
function ScriptableToolKit(scriptName, scriptId, options) {
return new (class {
constructor(scriptName, scriptId, options) {
//scriptable公共组件
this.local = FileManager.local()
this.icloud = FileManager.iCloud()
this.curDateCache = this.local.joinPath(this.local.documentsDirectory(), "curDateCache")
//一些扩展参数
this.options = options
this.tgEscapeCharMapping = {'&': '&'}
this.userAgent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.2 Safari/605.1.15`
this.prefix = `lk`
this.name = scriptName
this.id = scriptId
this.data = null
this.dataFile = `${this.prefix}${this.id}.json`
this.bgImgPath = `${this.prefix}${this.id}Bg.jpg`
this.bgImgPath = this.local.joinPath(this.local.documentsDirectory(), this.bgImgPath)
//i18n
this.lang = Device.language()
this.msg = {
"zh": {
s0:"在开始之前,先进入主屏幕,进入图标排列模式。滑到最右边的空白页,并进行截图。",
s1:"看起来你选择的图片不是iPhone的截图,或者你的iPhone不支持。请换一张图片再试一次。",
s2:"你想创建什么尺寸的widget?",
s3:"你想把widget放在哪里?",
s4:" (请注意,您的设备只支持两行小部件,所以中间和底部的选项是一样的)。",
s5:"widget的背景图已裁切完成,想在Scriptable内部使用还是导出到相册?",
s6:"已经截图,继续",
s7:"退出去截图",
s8:"小",
s9:"中",
s10:"大",
s11:"顶部左边",
s12:"顶部右边",
s13:"中间左边",
s14:"中间右边",
s15:"底部左边",
s16:"底部右边",
s17:"顶部",
s18:"中间",
s19:"底部",
s20:"在Scriptable内部使用",
s21:"导出到相册",
s22:"填写遮罩层颜色。(格式:#000000)",
s23:"颜色(格式:#000000)",
s24:"填写遮罩层不透明度(0-1之间)",
s25:"0-1之间",
s26:"确定",
s27:"取消",
s28:"预览widget",
s29:"设置widget背景",
s30:"入口",
s31:"你用的是哪个型号?",
s32:"退出",
s33:"清除缓存",
s34:"开始清除缓存",
s35:"清除缓存完成"
},
"en": {
s0:"Before you start, go to your home screen and enter wiggle mode. Scroll to the empty page on the far right and take a screenshot.",
s1:"It looks like you selected an image that isn't an iPhone screenshot, or your iPhone is not supported. Try again with a different image.",
s2:"What size of widget are you creating?",
s3:"What position will it be in?",
s4:" (Note that your device only supports two rows of widgets, so the middle and bottom options are the same.)",
s5:"Your widget background is ready. Would you like to use it in a Scriptable widget or export the image?",
s6:"Continue",
s7:"Exit to Take Screenshot",
s8:"Small",
s9:"Medium",
s10:"Large",
s11:"Top left",
s12:"Top right",
s13:"Middle left",
s14:"Middle right",
s15:"Bottom left",
s16:"Bottom right",
s17:"Top",
s18:"Middle",
s19:"Bottom",
s20:"Use in Scriptable",
s21:"Export to Photos",
s22:"Fill in the mask layer color. (Format: #000000)",
s23:"Color.(Format: #000000)",
s24:"Fill in the mask layer opacity (between 0-1)",
s25:"between 0-1",
s26:"Confirm",
s27:"Cancel",
s28:"Preview widget",
s29:"Setting widget background",
s30:"ENTER",
s31:"What type of iPhone do you have?",
s32:"Exit",
s33:"Clean cache",
s34:"Clean cache started",
s35:"Clean cache finished"
}
}
this.curLang = this.msg[this.lang] || this.msg.en
//默认脚本开关
this.isSaveLog = this.getResultByKey(`${this.prefix}IsSaveLog${this.id}`, false)
this.isEnableLog = this.getResultByKey(`${this.prefix}IsEnableLog${this.id}`, true)
this.logDir = this.icloud.documentsDirectory() + '/lklogs/' + this.id
this.logSeparator = '\n██'
this.now = new Date()
this.execStatus = true
this.notifyInfo = []
this.operations = []
//脚本执行限制
this.isLimited = false
this.checkLimit()
}
async checkLimit() {
const lastRunningTime = await this.getVal(`${this.prefix}LastRunningTime${this.id}`, 'local', 0)
const runLimitNum = this.getResultByKey(`${this.prefix}RunLimitNum${this.id}`, 300000)
this.log(`上次运行时间:${this.formatDate(new Date(lastRunningTime), "yyyy-MM-dd HH:mm:ss.S")} ${lastRunningTime},运行频率限制:${runLimitNum}`)
if (lastRunningTime >= 0) {
if (this.now.getTime() - lastRunningTime <= runLimitNum) {
this.appendNotifyInfo('限制运行')
this.isLimited = true
} else {
await this.setVal(`${this.prefix}LastRunningTime${this.id}`, this.now.getTime(), 'local')
}
}
return this.isLimited
}
getResultByKey(key, defaultValue) {
if (!this.options) {
return defaultValue
}
const val = this.options[key]
if (this.isEmpty(val)) {
return defaultValue
} else {
return val
}
}
appendNotifyInfo(info, type) {
if (type == 1) {
this.notifyInfo = info
} else {
this.notifyInfo.push(`${this.logSeparator}${this.formatDate(new Date(), 'yyyy-MM-dd HH:mm:ss.S')}█${info}`)
}
}
saveLog() {
if (this.isSaveLog) {
let message
if (Array.isArray(this.notifyInfo)) {
message = this.notifyInfo.join("")
} else {
message = this.notifyInfo
}
// 校验lklog目录是否存在
if (!this.icloud.isDirectory(this.logDir)) {
// create dir
this.icloud.createDirectory(this.logDir, true)
}
// write log
this.icloud.writeString(`${this.logDir}/${this.formatDate(this.now, 'yyyyMMddHHmmss')}.log`, message)
}
}
prependNotifyInfo(info) {
this.notifyInfo.splice(0, 0, info)
}
execFail() {
this.execStatus = false
}
sleep(time) {
return new Promise((resolve) => setTimeout(resolve, time))
}
log(message) {
if (this.isEnableLog) console.log(`${this.logSeparator}${JSON.stringify(message)}`)
this.appendNotifyInfo(message)
}
logErr(message) {
this.execStatus = false
if (this.isEnableLog) {
console.warn(`${this.logSeparator}${this.name}执行异常:`)
console.warn(message)
console.warn(`\n${message.message}`)
}
}
getContainer(key) {
return key == 'local' ? this.local : this.icloud
}
/**
* get value from container
* @param key
* @param container local or icloud
*/
async getVal(key, container, defaultValue) {
let containerInstance = this.getContainer(container)
let data = ''
try {
let realDataFile = containerInstance.joinPath(containerInstance.documentsDirectory(), this.dataFile)
if (!containerInstance.fileExists(realDataFile)) {
await this.setVal(key, defaultValue, container)
return defaultValue
}
data = await containerInstance.readString(realDataFile)
data = JSON.parse(data)
} catch (e) {
throw e
}
if (data.hasOwnProperty(key)) {
return data[key]
} else {
await this.setVal(key, defaultValue, container)
return defaultValue
}
}
/**
* get dataFile content
* @param container
*/
async getDataFile(container) {
let containerInstance = this.getContainer(container)
let data = ''
try {
let realDataFile = containerInstance.joinPath(containerInstance.documentsDirectory(), this.dataFile)
if (!containerInstance.fileExists(realDataFile)) {
return Promise.resolve('')
}
data = await containerInstance.readString(realDataFile)
} catch (e) {
throw e
}
return Promise.resolve(data)
}
async saveImage(fileName, image, container) {
let containerInstance = this.getContainer(container)
let imagePath = containerInstance.joinPath(containerInstance.documentsDirectory(), `${this.prefix}${this.id}/${fileName}`)
let imageDir = imagePath.substring(0, imagePath.lastIndexOf("/") + 1)
if (!containerInstance.isDirectory(imageDir)) {
containerInstance.createDirectory(imageDir, true)
}
containerInstance.writeImage(imagePath, image)
}
async getImage(fileName, container) {
let containerInstance = this.getContainer(container)
let imagePath = containerInstance.joinPath(containerInstance.documentsDirectory(), `${this.prefix}${this.id}/${fileName}`)
if (!containerInstance.fileExists(imagePath)) {
this.logErr(`file not exist: ${imagePath}`)
return false
}
return await containerInstance.readImage(imagePath)
}
/**
* set value in container
* @param key
* @param val
* @param container this.local or this.icloud
*/
async setVal(key, val, container) {
let containerInstance = this.getContainer(container)
let data
let realDataFile = containerInstance.joinPath(containerInstance.documentsDirectory(), this.dataFile)
try {
if (!containerInstance.fileExists(realDataFile)) {
data = {}
} else {
data = await containerInstance.readString(realDataFile)
data = JSON.parse(data)
}
} catch (e) {
data = {}
}
data[key] = val
await containerInstance.writeString(realDataFile, JSON.stringify(data))
}
async get(options, callback = () => {}) {
let request = new Request('')
request.url = options.url
request.method = 'GET'
request.headers = options.headers
try {
const result = await request.loadString()
callback(request.response, result)
return result
} catch (e) {
this.logErr(e)
callback(undefined, undefined)
}
}
async post(options, callback = () => {}) {
let request = new Request('')
request.url = options.url
request.body = options.body
request.method = 'POST'
request.headers = options.headers
request.timeout = 5000
try {
const result = await request.loadString()
callback(request.response, result)
return result
} catch (e) {
this.logErr(e)
callback(undefined, undefined)
}
}
async loadScript ({scriptName, url}) {
this.log(`获取脚本【${scriptName}】`)
const content = await this.get({url})
this.icloud.writeString(`${this.icloud.documentsDirectory()}/${scriptName}.js`, content)
this.log(`获取脚本【${scriptName}】完成🎉`)
}
require({scriptName, url = '', reload = false}) {
if (this.icloud.fileExists(this.icloud.joinPath(this.icloud.documentsDirectory(), `${scriptName}.js`)) && !reload) {
this.log(`引用脚本【${scriptName}】`)
return importModule(scriptName)
} else {
this.loadScript({ scriptName, url })
this.log(`引用脚本【${scriptName}】`)
return importModule(scriptName)
}
}
async generateInputAlert(message, field, defaultValue) {
let result = []
let alert = new Alert()
alert.message = message
alert.addTextField(field, defaultValue);
alert.addCancelAction(this.curLang.s27)
alert.addAction(this.curLang.s26)
result[0] = await alert.presentAlert()
result[1] = alert.textFieldValue(0)
return result
}
async generateAlert(message, options) {
let alert = new Alert()
alert.message = message
for (const option of options) {
alert.addAction(option)
}
return await alert.presentAlert()
}
isEmpty(obj) {
return typeof obj == "undefined" || obj == null || obj == "" || obj == "null"
}
isWorkingDays(now, workingDaysFlag = 'curlybraces', holidayFlag = 'gamecontroller') {
return new Promise(async (resolve, reject) => {
let sp = "≈"
const d = this.formatDate(now, 'yyyy-MM-dd')
// 0工作日 1休息日 2节假日 3补班
let resultStr = 0
try {
let curDate = await this.getVal('curDateCache', 'local', 'fff')
//判断上一次是否请求错误
let curDateErrorTime = await this.getVal('curDateCacheErrorTime', 'local', this.now.getTime())
let isPreError = !this.isEmpty(curDateErrorTime) && Number(curDateErrorTime) + (5 * 60 * 1000) < this.now.getTime()
if (!isPreError && d == curDate.split(sp)[0] && curDate.split(sp)[1] != "❌") {
//日期相同说明当天请求过,直接使用上次请求的值
resultStr = curDate.split(sp)[1]
this.log('already request')
// this.setVal('curDateCache', '', 'local')
} else {
this.log('send request')
const url = {
url: 'http://timor.tech/api/holiday/info/' + d
}
await this.get(url, (resp, data) => {
if (data.indexOf("<") == 0) {
resultStr = "❌"
} else {
resultStr = JSON.parse(data)
if (resultStr.code == -1) {
// 接口错误
resultStr = "❌"
} else {
resultStr = resultStr.type.type
}
}
})
}
} catch (e) {
resultStr = "❌"
this.logErr(e)
} finally {
// 写入文件系统
await this.setVal('curDateCache', `${d}${sp}${resultStr}`, 'local')
if (resultStr == "❌") {
resolve(resultStr)
// 写入错误时间,便于5分钟后重新请求
this.log('写入运行错误时间,5分钟后重新请求!')
this.setVal('curDateCache', '', 'local')
this.setVal('curDateCacheErrorTime', `${this.now.getTime()}`, 'local')
} else {
this.setVal('curDateCacheErrorTime', '', 'local')
this.setVal('curDateCache', `${d}${sp}${resultStr}`, 'local')
resolve(resultStr == 0 || resultStr == 3 ? workingDaysFlag : holidayFlag)
}
}
})
}
randomString(len) {
len = len || 32
var $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'
var maxPos = $chars.length
var pwd = ''
for (let i = 0; i < len; i++) {
pwd += $chars.charAt(Math.floor(Math.random() * maxPos))
}
return pwd
}
/**
*
* 示例:$.time('yyyy-MM-dd qq HH:mm:ss.S')
* :$.time('yyyyMMddHHmmssS')
* y:年 M:月 d:日 q:季 H:时 m:分 s:秒 S:毫秒
* 其中y可选0-4位占位符、S可选0-1位占位符,其余可选0-2位占位符
* @param {*} format 格式化参数
*
*/
formatDate(date, format) {
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'H+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds(),
'q+': Math.floor((date.getMonth() + 3) / 3),
'S': date.getMilliseconds()
}
if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
for (let k in o)
if (new RegExp('(' + k + ')').test(format))
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
return format
}
/**
* 自动补齐字符串
* @param str 原始字符串
* @param prefix 前缀
* @param suffix 后缀
* @param fill 补齐用字符
* @param len 目标补齐长度,不包含前后缀
* @param direction 方向:0往后补齐
* @param ifCode 是否打码
* @param clen 打码长度
* @param startIndex 起始坐标
* @param cstr 打码字符
* @returns {*}
*/
autoComplete(str, prefix, suffix, fill, len, direction, ifCode, clen, startIndex, cstr) {
str += ``
if (str.length < len) {
while (str.length < len) {
if (direction == 0) {
str += fill
} else {
str = fill + str
}
}
}
if (ifCode) {
let temp = ``
for (var i = 0; i < clen; i++) {
temp += cstr
}
str = str.substring(0, startIndex) + temp + str.substring(clen + startIndex)
}
str = prefix + str + suffix
return this.toDBC(str)
}
/**
* @param str 源字符串 "#{code}, #{value}"
* @param param 用于替换的数据,结构如下
* @param prefix 前缀 "#{"
* @param suffix 后缀 "}"
* {
* "code": 1,
* "value": 2
* }
* 按上面的传入,输出为"1, 2"
* 对应的#{code}用param里面code的值替换,#{value}也是
* @returns {*|void|string}
*/
customReplace(str, param, prefix, suffix) {
try {
if (this.isEmpty(prefix)) {
prefix = "#{"
}
if (this.isEmpty(suffix)) {
suffix = "}"
}
for (let i in param) {
str = str.replace(`${prefix}${i}${suffix}`, param[i])
}
} catch (e) {
this.logErr(e)
}
return str
}
toDBC(txtstring) {
var tmp = ""
for (var i = 0; i < txtstring.length; i++) {
if (txtstring.charCodeAt(i) == 32) {
tmp = tmp + String.fromCharCode(12288)
} else if (txtstring.charCodeAt(i) < 127) {
tmp = tmp + String.fromCharCode(txtstring.charCodeAt(i) + 65248)
}
}
return tmp
}
getWidgetBg() {
return this.local.readImage(this.bgImgPath)
}
phoneSizes() {
return {
// 16 Pro Max
"2868": {
small: 510,
medium: 1092,
large: 1146,
left: 114,
right: 696,
top: 276,
middle: 912,
bottom: 1548,
},
// 14 Pro Max
"2796": {
small: 510,
medium: 1092,
large: 1146,
left: 99,
right: 681,
top: 282,
middle: 918,
bottom: 1554
},
// 14 Pro
"2556": {
small: 474,
medium: 1014,
large: 1062,
left: 82,
right: 622,
top: 270,
middle: 858,
bottom: 1446
},
// 12 Pro Max
"2778": {
small: 510,
medium: 1092,
large: 1146,
left: 96,
right: 678,
top: 246,
middle: 882,
bottom: 1518
},
// 12 and 12 Pro
"2532": {
small: 474,
medium: 1014,
large: 1062,
left: 78,
right: 618,
top: 231,
middle: 819,
bottom: 1407
},
// 11 Pro Max, XS Max
"2688": {
small: 507,
medium: 1080,
large: 1137,
left: 81,
right: 654,
top: 228,
middle: 858,
bottom: 1488
},
// 11, XR
"1792": {
small: 338,
medium: 720,
large: 758,
left: 54,
right: 436,
top: 160,
middle: 580,
bottom: 1000
},
// 11 Pro, XS, X, 12 mini
"2436": {
x: {
small: 465,
medium: 987,
large: 1035,
left: 69,
right: 591,
top: 213,
middle: 783,
bottom: 1353,
},
mini: {
small: 465,
medium: 987,
large: 1035,
left: 69,
right: 591,
top: 231,
middle: 801,
bottom: 1371,
}
},
// Plus phones
"2208": {
small: 471,
medium: 1044,
large: 1071,
left: 99,
right: 672,
top: 114,
middle: 696,
bottom: 1278
},
// SE2 and 6/6S/7/8
"1334": {
small: 296,
medium: 642,
large: 648,
left: 54,
right: 400,
top: 60,
middle: 412,
bottom: 764
},
// SE1
"1136": {
small: 282,
medium: 584,
large: 622,
left: 30,
right: 332,
top: 59,
middle: 399,
bottom: 399
},
// 11 and XR in Display Zoom mode
"1624": {
small: 310,
medium: 658,
large: 690,
left: 46,
right: 394,
top: 142,
middle: 522,
bottom: 902
},
// Plus in Display Zoom mode
"2001": {
small: 444,
medium: 963,
large: 972,
left: 81,
right: 600,
top: 90,
middle: 618,
bottom: 1146
}
}
}
remove(path) {
this.local.remove(path)
}
cropImage(img, rect, color, opacity) {
let draw = new DrawContext()
draw.size = new Size(rect.width, rect.height)
draw.drawImageAtPoint(img, new Point(-rect.x, -rect.y))
draw.setFillColor(new Color(color, Number(opacity)))
draw.fillRect(new Rect(0, 0, img.size["width"], img.size["height"]))
return draw.getImage()
}
async widgetCutBg() {
// Determine if user has taken the screenshot.
var message
message = this.curLang.s0
let exitOptions = [this.curLang.s6, this.curLang.s7]
let shouldExit = await this.generateAlert(message, exitOptions)
if (shouldExit) return
// Get screenshot and determine phone size.
let img = await Photos.fromLibrary()
let height = img.size.height
let phone = this.phoneSizes()[height]
if (!phone) {
message = this.curLang.s1
await this.generateAlert(message, ["OK"])
return
}
// Extra setup needed for 2436-sized phones.
if (height == 2436) {
message = this.curLang.s31
let types = ["iPhone 12 mini", "iPhone 11 Pro, XS, X"]
let typeIndex = await this.generateAlert(message, types)
let type = (typeIndex == 0) ? "mini" : "x"
phone = phone[type]
}
// Prompt for widget size and position.
message = this.curLang.s2
let sizes = [this.curLang.s8, this.curLang.s9, this.curLang.s10]
let size = await this.generateAlert(message, sizes)
message = this.curLang.s3
message += (height == 1136 ? this.curLang.s4 : "")
// Determine image crop based on phone size.
let crop = {w: "", h: "", x: "", y: ""}
if (size == 0) {
crop.w = phone.small
crop.h = phone.small
let positions = ["Top left", "Top right", "Middle left", "Middle right", "Bottom left", "Bottom right"]
let positionsString = [this.curLang.s11, this.curLang.s12, this.curLang.s13, this.curLang.s14, this.curLang.s15, this.curLang.s16]
let position = await this.generateAlert(message, positionsString)
// Convert the two words into two keys for the phone size dictionary.
let keys = positions[position].toLowerCase().split(' ')
crop.y = phone[keys[0]]
crop.x = phone[keys[1]]
} else if (size == 1) {
crop.w = phone.medium
crop.h = phone.small
// Medium and large widgets have a fixed x-value.
crop.x = phone.left
let positions = ["Top", "Middle", "Bottom"]
let positionsString = [this.curLang.s17, this.curLang.s18, this.curLang.s19]
let position = await this.generateAlert(message, positionsString)
let key = positions[position].toLowerCase()
crop.y = phone[key]
} else if (size == 2) {
crop.w = phone.medium
crop.h = phone.large
crop.x = phone.left
let positionsString = [this.curLang.s17, this.curLang.s19]
let position = await this.generateAlert(message, positionsString)
// Large widgets at the bottom have the "middle" y-value.
crop.y = position ? phone.middle : phone.top
}
// set mask layer color
let maskLayerColor = await this.generateInputAlert(this.curLang.s22, this.curLang.s23, '#000000')
if(maskLayerColor[0] == -1) return
let opacity = await this.generateInputAlert(this.curLang.s24, this.curLang.s25, '0.1')
if(opacity[0] == -1) return
// Crop image and finalize the widget.
let imgCrop = this.cropImage(img, new Rect(crop.x, crop.y, crop.w, crop.h), maskLayerColor[1], opacity[1])
message = this.curLang.s5
const exportPhotoOptions = [this.curLang.s20, this.curLang.s21]
const exportPhoto = await this.generateAlert(message, exportPhotoOptions)
if (exportPhoto) {
Photos.save(imgCrop)
} else {
this.local.writeImage(this.bgImgPath, imgCrop)
}
Script.complete()
}
/**
* 自定义操作入口
* @param customEnter [{name:"操作1", callback: function(){}}]
* @param isReset true:清空自带的操作
* false:在自带的操作后面补充
*/
async widgetEnter(customEnter, isReset) {
// 清空上次运行时间,防止第一次运行小组件不成功导致无法继续调整
await this.setVal('lastRunningTime', 0, 'local')
let options = [this.curLang.s28, this.curLang.s29, this.curLang.s33]
if (Array.isArray(customEnter)) {
let customEnterNames = customEnter.map((item, index) => {
return item.name[this.lang]
})
let customEnterCallback = customEnter.map((item, index) => {
return item.callback
})
if (isReset) {
options = customEnterNames
} else {
this.operations.push({callback: main})
this.operations.push({callback: function(){$.widgetCutBg()}})
this.operations.push({callback: function(){$.cleanCache()}})
options = options.concat(customEnterNames)
}
customEnterCallback.forEach((callback)=>{this.operations.push({callback: callback})})
}
options.push(this.curLang.s32)
this.operations.push({callback: function(){}})
return await this.generateAlert(this.curLang.s30, options)
}
async handleOperations(index) {
await this.operations[index].callback()
}
cleanCache() {
this.log(this.curLang.s34)
let filePath = this.local.joinPath(this.local.documentsDirectory(), this.dataFile)
if (this.local.fileExists(filePath)) {
this.local.remove(filePath)
}
filePath = this.bgImgPath
if (this.local.fileExists(filePath)) {
this.local.remove(filePath)
}
this.log(this.curLang.s35)
}
formatTimeDuring(total, lang = "zh", n = 0) {
total = Number(total);
let zhUnitArr = ["毫秒", "秒", "分钟", "小时", "天", "月", "年"];
let enUnitArr = ["ms", "s", "min", "h", "d", "m", "y"];
let scaleArr = [1000.0, 60.0, 60.0, 24.0, 30.0, 12.0, 100];
let len = total;
if (len > scaleArr[n]) {
len = total / scaleArr[n];
return this.formatTimeDuring(len, lang, ++n);
} else {
let unit = zhUnitArr[n]
if (lang === "en") {
unit = enUnitArr[n]
}
return len.toFixed(2) + "" + unit;
}
}
fileLengthFormat(total, unit = "", toByte = false) {
total = Number(total);
var unitArr = ["", "KB", "MB", "GB", "TB", "PB", "EB", "ZB"];
var n = 0;
try {
n = unitArr.indexOf(unit);
} catch (e) {
throw e;
}
if (toByte) {
if (n == 0) {
return total;
}
return this.fileLengthFormat(total * 1024, unitArr[--n], true);
}
var len = total;
if (len > 1000) {
len = total / 1024.0;
return this.fileLengthFormat(len, unitArr[++n]);
} else {
if (n == 0) {
return len.toFixed(2);
}
return len.toFixed(2) + " " + unitArr[n];
}
}
})(scriptName, scriptId, options)
}