forked from sunyymq/JSBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPushbullet.js
142 lines (137 loc) · 4.06 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
/*
Pushbullet
支持从剪切板
发送 Push(支持 note 类型)
接收 Push(支持 note, link 类型)
by Neurogram
*/
// 请将 Api key 填写到下方""中
var apiKey = ""
// 获取 Push 的最大个数,默认 10
var pushLimit = 10
$ui.menu({
items: ["Get Push", "Send Push", "Delete all Pushes"],
handler: function(title, idx) {
if (idx == 0) {
$http.request({
method: "GET",
url: "https://api.pushbullet.com/v2/pushes?active=true&limit=" + pushLimit,
header: {
"Access-Token": apiKey
},
handler: function(resp) {
pushResults = resp.data.pushes
var pushItems = pushResults.length
if (pushItems == 0) {
$ui.alert("Sorry, no pushes here")
$app.close()
} else if (pushItems == 1) {
pushItem(0, pushResults[0].type)
} else {
$ui.menu({
items: pushResults.map(function(item) {
if (item.type == "link") {
if (item.url == undefined) {
var urlShow = "🔗" + item.body
} else {
var urlShow = "🔗" + item.url
}
}
return urlShow || item.body
}),
handler: function(title, idx) {
pushItem(idx, pushResults[idx].type)
}
})
}
}
})
} else if (idx == 1) {
if ($clipboard.text == "") {
$ui.alert("Clipboard is empty")
} else {
$http.request({
method: "POST",
url: "https://api.pushbullet.com/v2/pushes",
header: {
"Access-Token": apiKey
},
body: {
type: "note",
body: $clipboard.text
}
})
}
} else {
$ui.alert({
title: "This will delete all of your pushes and cannot be undone",
actions: [{
title: "Delete",
handler: function() {
$http.request({
method: "DELETE",
url: "https://api.pushbullet.com/v2/pushes",
header: {
"Access-Token": apiKey
},
handler: function(resp) {
$ui.toast("Success")
}
})
}
}, {
title: "Cancel",
handler: function() {
$app.close()
}
}]
})
}
}
})
function copyText(text) {
$clipboard.text = text
$ui.toast("Success")
}
function pushItem(itemIdx, itemType) {
if (itemType == "link") {
urlItem(pushResults[itemIdx].url || pushResults[itemIdx].body)
} else if (itemType == "note") {
var links = $detector.link(pushResults[itemIdx].body)
if (links == "") {
copyText(pushResults[itemIdx].body)
} else {
$ui.menu({
items: ["Copy Text", "View Link"],
handler: function(title, idx) {
if (idx == 0) {
copyText(pushResults[itemIdx].body)
} else {
if (links.length == 1) {
urlItem(links[0])
} else {
$ui.menu({
items: links,
handler: function(title, idx) {
urlItem(links[idx])
}
})
}
}
}
})
}
}
}
function urlItem(url) {
$ui.menu({
items: ["Copy Link", "Open in Safari"],
handler: function(title, idx) {
if (idx == 0) {
copyText(url)
} else {
$app.openURL(url)
}
}
})
}