-
Notifications
You must be signed in to change notification settings - Fork 9
/
Pushbullet.js
784 lines (757 loc) · 21 KB
/
Pushbullet.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
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
/*
1.App 内支持发送、接收、删除内容
2.Today widget 支持发送剪贴板内容(Line 18 处可自行修改回原方式)
3.Action Extension 支持自动识别发送 Note, Link,File 类型
4.Safari Extension 运行增加获取当前页面标题、链接、选中内容
5.Note 类型检测到链接时自动转换为 Link 类型,接收自动打开链接
6.指定接收设备,避免发送端安装 App 的情况下也收到通知
7.AppStore 链接自动转换为 ASO100 页面(支持 PC 查看美区等商店内容且数据更全面,Line 345 处可自行删除取消转换)
*/
var timeout = 3
var accesstoken = getToken()
var device = getDevice()
var device_num = 1
if (Array.isArray(device)) {
device_num = device.length
}
// 从 Today Widget 启动
if ($app.env == $env.today) {
if (accesstoken) {
// pushbullet:全部功能 / pushbulletCnt:发送内容
//pushbullet(accesstoken, device)
pushbulletCnt($clipboard.text, accesstoken, device, device_num)
} else {
var message = {
title: "Access Token Missing 😅",
message: "Execute This Script In JSBox App For More Information.",
actions: [{
title: "Run in JSBox",
handler: function() {
$app.openURL("jsbox://run?name=" + $addin.current.name.split(".js")[0])
}
},
{
title: "Cancel",
handler: function() {
$app.close()
}
}
]
}
$ui.alert(message)
}
}
// 从应用内启动
if ($app.env == $env.app) {
if (accesstoken) {
pushbullet(accesstoken, device, device_num)
} else {
settingConfig()
}
}
// 从 Action Entension 启动
if ($app.env == $env.action) {
if (accesstoken) {
pushbulletAction(accesstoken, device, device_num)
} else {
settingConfig()
}
}
// 从 Safari 启动
if ($app.env == $env.safari) {
if (accesstoken) {
pushbulletSafari(accesstoken, device, device_num)
} else {
settingConfig()
}
}
function pushbullet(accesstoken, device, device_num) {
let auto_input = $context.query.Pushbullet_Content
if (auto_input) {
auto_input = decodeURIComponent(auto_input)
pushbulletCnt(auto_input, accesstoken, device, device_num, "toHomescreen")
} else {
$ui.menu({
items: ["Send ⬆️", "Get ⬇️", "Delete 🗑"],
handler: function(title, idx) {
if (idx == 0) {
pushbulletCnt($clipboard.text, accesstoken, device, device_num)
} else if (idx == 1) {
getItem(accesstoken)
} else if (idx == 2) {
$ui.alert({
title: "Delete Confirm 🗑",
message: "One Or All?",
actions: [{
title: "One",
handler: function() {
deleteItem(accesstoken)
}
}, {
title: "All",
handler: function() {
$ui.loading("Loading...")
$http.request({
method: "DELETE",
url: "https://api.pushbullet.com/v2/pushes",
header: {
"Access-Token": accesstoken
},
timeout: timeout,
handler: function(resp) {
toast(resp)
delayClose()
}
})
}
},
{
title: "Cancel",
handler: function() {
$app.close()
}
}
]
})
}
},
finished: function(cancelled) {
if (cancelled) {
$app.close()
}
}
})
}
}
function pushbulletCnt(content, accesstoken, device, device_num, redirect) {
if (!content || content == "") {
$ui.error("Clipboard is Empty!", 1)
delayClose(redirect)
} else {
let patt = /(https?|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/;
let result = null;
if ((result = patt.exec(content)) != null) {
sendLink(content, result[0], null, accesstoken, device, device_num, redirect)
} else {
sendNote(content, accesstoken, device, device_num, redirect)
}
}
}
function pushbulletSafari(accesstoken, device, device_num) {
var link = $context.safari.items.baseURI
var title = $context.safari.items.title
var selection = $context.safari.items.selection.text
sendLink(title, link, selection, accesstoken, device, device_num)
}
function pushbulletAction(accesstoken, device, device_num) {
var re = /^file:\/\//i;
if ($context.link && !re.test($context.link)) {
sendLink($context.text || $context.link, $context.link, null, accesstoken, device, device_num)
} else if ($context.text) {
var link = $context.text.match(/https?:\/\/[^\s]+/i)
if (link) {
link = link[0]
sendLink($context.text || link, link, null, accesstoken, device, device_num)
} else {
sendNote($context.text, accesstoken, device, device_num)
}
} else if ($context.data) {
var file = $context.data
$ui.toast("Setting URL for file...")
$ui.loading("Loading...")
file_name = file.fileName
$http.request({
method: "POST",
url: "https://api.pushbullet.com/v2/upload-request",
header: {
"Access-Token": accesstoken,
},
body: {
file_name: file_name
},
timeout: timeout,
handler: function(resp) {
toast(resp)
var upload_url = resp.data.upload_url
var file_url = resp.data.file_url
if (file_url.indexOf("pushbulletusercontent.com/") != -1) {
$ui.toast("file_url SUCCEEDED 💡")
} else {
$ui.toast("file_url FAILED ❌")
$app.close()
}
$ui.toast("File Uploading...")
$ui.loading("Loading...")
$http.request({
method: "POST",
url: upload_url,
form: {
file: file
},
timeout: 30,
handler: function(resp) {
toast(resp)
sendFile(file_url, file_name, accesstoken, device, device_num)
}
})
}
})
}
}
async function sendFile(file_url, file_name, accesstoken, device, device_num) {
$ui.loading("Loading...")
for (let i = 0; i < device_num; i++) {
let resp = await $http.request({
method: "POST",
url: "https://api.pushbullet.com/v2/pushes",
header: {
"Access-Token": accesstoken,
},
body: {
device_iden: device[i],
type: "file",
file_url: file_url,
file_name: file_name,
},
timeout: timeout
})
toast(resp)
if (i == device_num - 1) {
delayClose(redirect)
}
}
}
async function sendNote(note, accesstoken, device, device_num, redirect) {
$ui.loading(note)
for (let i = 0; i < device_num; i++) {
let resp = await $http.request({
method: "POST",
url: "https://api.pushbullet.com/v2/pushes",
header: {
"Access-Token": accesstoken,
"Content-Type": "application/json"
},
body: {
device_iden: device[i],
type: "note",
body: note
},
timeout: timeout
})
toast(resp)
if (i == device_num - 1) {
delayClose(redirect)
}
}
}
async function sendLink(title, link, selection, accesstoken, device, device_num, redirect) {
//Convert iOS App Store urls to ASO100 ⬇️
var patt = /itunes\.apple\.com\/(\w+)\/app\/.*?\?mt=(?!12).*/;
var result = null;
if ((result = patt.exec(link)) != null) {
var regex = /.+id(\d+).*/;
var appid = regex.exec(link)[1];
link = "https://www.qimai.cn/app/baseinfo/appid/" + appid + "/country/" + result[1];
}
//End ⬆️
$ui.loading(link)
for (let i = 0; i < device_num; i++) {
let resp = await $http.request({
method: "POST",
url: "https://api.pushbullet.com/v2/pushes",
header: {
"Access-Token": accesstoken,
"Content-Type": "application/json"
},
body: {
device_iden: device[i],
type: "link",
title: title,
body: selection,
url: link,
},
timeout: timeout
})
toast(resp)
if (i == device_num - 1) {
delayClose(redirect)
}
}
}
function getItem(accesstoken, last_cursor) {
$ui.loading("Loading...")
let cursor_param = ""
if (last_cursor) {
cursor_param = `&cursor=${last_cursor}`
}
$http.request({
method: "GET",
url: `https://api.pushbullet.com/v2/pushes?active=true&limit=500${cursor_param}`,
header: {
"Access-Token": accesstoken
},
timeout: timeout,
handler: function(resp) {
$ui.loading(false)
let pushes = resp.data.pushes
let cursor = resp.data.cursor
if (pushes.length == 0 && !cursor) {
$ui.alert("NO PUSHES❌")
$app.close()
} else if (pushes.length == 0 && cursor) {
getItem(accesstoken, cursor)
} else {
let list = pushes.map(function(item) {
if (item.type == "note") {
if (item.body.indexOf("\n") >= 0) {
return item.body.trim().split("\n")[0] + "...↩️"
} else {
return item.body
}
} else if (item.type == "link") {
if (item.body) {
return "🔗:" + item.body
} else if (item.title) {
return "🔗:" + item.title
} else {
return "🔗:" + item.url
}
} else {
var filename = item.file_url
return "📝:" + filename.substr(filename.lastIndexOf('/') + 1)
}
})
if (cursor) {
list.push("➤ ➤ Next Page ➤ ➤")
}
$ui.menu({
items: list,
handler: function(title, idx) {
if (title == "➤ ➤ Next Page ➤ ➤") {
getItem(accesstoken, cursor)
} else if (pushes[idx].type == "link") {
if (pushes[idx].body) {
$clipboard.text = "[" + pushes[idx].body + "]" + "(" + pushes[idx].url + ")"
} else if (pushes[idx].title) {
$clipboard.text = "[" + pushes[idx].title + "]" + "(" + pushes[idx].url + ")"
} else {
$clipboard.text = pushes[idx].url
}
var title = "Link and Note Copied 📌"
selectResult(title, $clipboard.text, pushes[idx].url)
} else if (pushes[idx].type == "note") {
$clipboard.text = pushes[idx].body
var link = $detector.link(pushes[idx].body)
if (link.length == 1) {
var title = "Note Copied 📌"
var message = "Find 🔗: " + link
selectResult(title, message, link)
} else if (link.length > 1) {
$ui.toast("Note Copied 📌 Multi-Links Dectected 🔗")
$ui.menu({
items: link,
handler: function(title, idx) {
$clipboard.text = link[idx]
selectResult2("Link Copied 📌", link[idx])
}
})
} else {
$ui.toast("Copied 📌")
delayClose()
}
} else {
var title = "Pushbullet File 📝"
var url = pushes[idx].file_url
$clipboard.text = url
$ui.toast("File URL Copied 📌")
selectResult2(title, url)
}
},
finished: function(cancelled) {
if (cancelled) {
$app.close()
}
}
})
}
}
})
}
function deleteItem(accesstoken, last_cursor) {
$ui.loading("Loading...")
let cursor_param = ""
if (last_cursor) {
cursor_param = `&cursor=${last_cursor}`
}
$http.request({
method: "GET",
url: `https://api.pushbullet.com/v2/pushes?active=true&limit=500${cursor_param}`,
header: {
"Access-Token": accesstoken
},
timeout: timeout,
handler: function(resp) {
$ui.loading(false)
let pushes = resp.data.pushes
let cursor = resp.data.cursor
if (pushes.length == 0 && !cursor) {
$ui.alert("NO PUSHES❌")
$app.close()
} else if (pushes.length == 0 && cursor) {
deleteItem(accesstoken, cursor)
} else {
let list = pushes.map(function(item) {
if (item.type == "note") {
return item.body.replace(/\n/g," ")
} else if (item.type == "link") {
if (item.body) {
return "🔗:[" + item.body.replace(/\n/g,"") + "]" + "(" + item.title.replace(/\n/g,"") + ")"
} else if (item.title) {
return "🔗:[" + item.title.replace(/\n/g,"") + "]" + "(" + item.url + ")"
} else {
return "🔗:" + item.url
}
} else {
let filename = item.file_url
return "📝:" + filename.substr(filename.lastIndexOf('/') + 1)
}
})
if (cursor) {
list.push("➤ ➤ Next Page ➤ ➤")
}
$ui.menu({
items: list,
handler: function(title, idx) {
if (title == "➤ ➤ Next Page ➤ ➤") {
deleteItem(accesstoken, cursor)
} else {
let iden = pushes[idx].iden
$http.request({
method: "DELETE",
url: "https://api.pushbullet.com/v2/pushes/" + iden,
header: {
"Access-Token": accesstoken
},
timeout: timeout,
handler: function(resp) {
toast(resp)
deleteItem(accesstoken, last_cursor)
}
})
}
},
finished: function(cancelled) {
if (cancelled) {
$app.close()
}
}
})
}
}
})
}
function getToken() {
if ($file.exists("pushbullet.txt")) {
var file = $file.read("pushbullet.txt")
if (file.string) {
return file.string.replace(/(\r|\n)/gi, "")
} else {
return 0
}
} else {
return 0
}
}
function getDevice() {
if ($file.exists("device.txt")) {
var file = $file.read("device.txt")
if (file.string && file.string != "") {
let devices = file.string.replace(/(\r|\n)/gi, "").split(",")
return devices
} else {
return ""
}
} else {
return ""
}
}
function toast(resp) {
if (resp.response.statusCode == 400) {
console.info(resp.response)
console.info(resp.data)
$ui.toast("Error! Please check the console log!")
$ui.loading(false)
} else if (resp.response) {
$ui.toast("Request Succeeded💡")
$ui.loading(false)
} else {
$ui.toast("Request Timeout, Try Again Later ❌")
$ui.loading(false)
$app.close()
}
}
function delayClose(redirect) {
$thread.main({
delay: 0.8,
handler: function() {
if ($app.env == $env.action || $app.env == $env.safari) {
$context.close()
} else {
if (redirect && redirect == "toHomescreen") {
$system.home();
}
$app.close()
}
}
})
}
function selectResult(title, message, url, quicklook = 0) {
$ui.alert({
title: title,
message: message,
actions: [{
title: "Open",
handler: function() {
$app.openURL(url)
delayClose()
}
},
{
title: "Preview",
handler: function() {
if (quicklook == 0) {
$safari.open({
url: url,
handler: function() {
$app.close()
}
})
} else {
$http.download({
url: url,
handler: function(resp) {
$quicklook.open({
data: resp.data,
handler: function() {
$app.close()
}
})
}
})
}
}
},
{
title: "Copy URL",
handler: function() {
$clipboard.text = url
$ui.toast("Copied 📌")
delayClose()
}
},
{
title: "Cancel",
handler: function() {
$app.close()
}
}
]
})
}
function settingConfig() {
$ui.render({
props: {
title: "Pushbullet"
},
views: [{
type: "text",
props: {
id: "message_token",
text: "\nInput your access token to use the API.",
font: $font(16),
editable: 0
},
layout: function(make) {
make.left.top.right.inset(5)
make.height.equalTo(55)
}
},
{
type: "input",
props: {
id: "accesstoken",
placeholder: "Paste Your Access Token",
align: $align.center,
font: $font(15)
},
layout: function(make) {
var preView = $("message_token")
make.top.equalTo(preView.bottom).inset(10)
make.left.right.inset(10)
make.height.equalTo(30)
},
events: {
returned: function(sender) {
$("input").blur()
}
}
},
{
type: "text",
props: {
id: "message_device",
text: "\nYou can specify one or more devices(split with ',') with the identity.\nPlease input nothing if you want to send to all.",
font: $font(16),
editable: 0
},
layout: function(make) {
var preView = $("accesstoken")
make.top.equalTo(preView.bottom).inset(5)
make.left.right.inset(5)
make.height.equalTo(70)
}
},
{
type: "input",
props: {
id: "device",
placeholder: "Paste Your Device Identity",
align: $align.center,
font: $font(15)
},
layout: function(make) {
var preView = $("message_device")
make.top.equalTo(preView.bottom).inset(10)
make.left.right.inset(10)
make.height.equalTo(30)
},
events: {
returned: function(sender) {
$("input").blur()
}
}
},
{
type: "button",
props: {
id: "submit",
title: "Submit",
font: $font(15)
},
layout: function(make) {
var preView = $("device")
make.top.equalTo(preView.bottom).inset(30)
make.left.right.inset(10)
make.height.equalTo(30)
},
events: {
tapped: function() {
handleButtonSubmit()
}
}
},
{
type: "button",
props: {
id: "register",
title: "Create Access Token on Pushbullet.com",
font: $font(15)
},
layout: function(make) {
var preView = $("submit")
make.top.equalTo(preView.bottom).inset(10)
make.left.right.inset(10)
make.height.equalTo(30)
},
events: {
tapped: function() {
$app.openURL("https://www.pushbullet.com/#settings/account")
}
}
},
{
type: "button",
props: {
id: "setting",
title: "More Information",
font: $font(15)
},
layout: function(make) {
var preView = $("register")
make.top.equalTo(preView.bottom).inset(10)
make.left.right.inset(10)
make.height.equalTo(30)
},
events: {
tapped: function() {
$ui.preview({
url: "http://telegra.ph/PushbulletAccessToken-09-17"
})
}
}
}
]
})
}
function handleButtonSubmit() {
var accesstoken = $("accesstoken").text
var deviceiden = $("device").text
if (accesstoken == '') {
$ui.toast("Input Access Token.")
} else {
$ui.loading("CONNECTING...")
$http.request({
method: "GET",
url: "https://api.pushbullet.com/v2/pushes?active=true",
header: {
"Access-Token": accesstoken
},
timeout: timeout,
handler: function(resp) {
$ui.loading(false)
if (!resp.response) {
$ui.toast("Request Timeout, Try Again Later ❌")
} else if (resp.response.statusCode == 200) {
$ui.toast("Verifying Succeeded💡")
$file.write({
data: $data({
string: accesstoken
}),
path: "pushbullet.txt"
})
$file.write({
data: $data({
string: deviceiden
}),
path: "device.txt"
})
$("accesstoken").blur()
$app.close()
} else {
$("accesstoken").text = ""
$ui.toast("Wrong Access Token, Try Again Later ❌")
$("accesstoken").focus()
}
}
})
}
}
function selectResult2(title, url) {
$ui.alert({
title: title,
message: url,
actions: [{
title: "Preview",
handler: function() {
$safari.open({
url: url,
})
}
}, {
title: "Cancel",
handler: function() {
$app.close()
}
}]
})
}