-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
200 lines (192 loc) · 6.93 KB
/
index.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
const { Extension, type, api } = require('clipcc-extension');
class CandyBucket {
constructor(candy = 5, refresh = 20 * 1000) {
this._currentCandy = candy;
this._full = candy;
setInterval(() => {
if (this._currentCandy < this._full) {
this._currentCandy++;
}
}, refresh);
}
get isEmpty() {
/*if (this._currentCandy === 0) return true;
else {
this._currentCandy --;
return false;
}*/
return false;//取消次数限制
}
}
class AlertExtension extends Extension {
onInit() {
const bucket = new CandyBucket();
let alerting = false;
api.addCategory({
categoryId: 'jasonxu.alert.alert',
messageId: 'jasonxu.alert.alert.messageId',
color: '#4F6C19'
});
api.addBlock({
opcode: 'jasonxu.alert.confirm.opcode',
type: type.BlockType.BOOLEAN,
messageId: 'jasonxu.alert.confirm',
categoryId: 'jasonxu.alert.alert',
function: args => {
if (window.clipAlert) {
if (alerting) return;
if (!bucket.isEmpty) {
return new Promise(resolve => {
alerting = true;
clipAlert(args.TITLE, args.MESSAGE)
.then(result => {
alerting = false;
resolve(result);
});
});
}
}
if (!bucket.isEmpty) return confirm(args.MESSAGE);
},
param: {
TITLE: {
type: type.ParameterType.STRING,
default: 'Here\'s an example'
},
MESSAGE: {
type: type.ParameterType.STRING,
default: 'AlexCui AK IOI?'
}
}
});
api.addBlock({
opcode: 'jasonxu.alert.prompt.opcode',
type: type.BlockType.REPORTER,
messageId: 'jasonxu.alert.prompt',
categoryId: 'jasonxu.alert.alert',
function: args => {
try {
if (!bucket.isEmpty) {
return prompt(args.MESSAGE);
}
} catch {
return NaN;
}
},
param: {
MESSAGE: {
type: type.ParameterType.STRING,
default: 'What is your name?'
}
}
});
api.addBlock({
opcode: 'jasonxu.alert.simple.opcode',
type: type.BlockType.COMMAND,
messageId: 'jasonxu.alert.simple',
categoryId: 'jasonxu.alert.alert',
function: args => {
if (window.clipAlert) {
if (alerting) return;
if (!bucket.isEmpty) {
return new Promise(resolve => {
alerting = true;
clipAlert(args.TITLE, args.MESSAGE)
.then(() => {
alerting = false;
resolve();
});
});
}
}
if (!bucket.isEmpty) return alert(args.MESSAGE);
},
param: {
TITLE: {
type: type.ParameterType.STRING,
default: 'Dannr yyds'
},
MESSAGE: {
type: type.ParameterType.STRING,
default: 'ClipTeam yyds!'
}
}
});
api.addBlock({
opcode: 'jasonxu.alert.openWindow.opcode',
type: type.BlockType.COMMAND,
messageId: 'jasonxu.alert.openWindow',
categoryId: 'jasonxu.alert.alert',
function: args => {
if (window.clipAlert) {
if (alerting) return;
if (!bucket.isEmpty) {
new Promise(() => {
alerting = true;
clipAlert('提示', '项目正在尝试打开新窗口:' + args.URL)
.then(result => {
alerting = false;
if (result) window.open(args.URL, args.TITLE, 'width=' + args.WIDTH + ',height=' + args.HEIGHT);
});
});
}
}
else if (!bucket.isEmpty) {
if (confirm('项目正在尝试打开新窗口:' + args.URL)) window.open(args.URL, args.TITLE, 'width=' + args.WIDTH + ',height=' + args.HEIGHT);
}
return;
},
param: {
URL: {
type: type.ParameterType.STRING,
default: 'https://codingclip.com'
}, WIDTH: {
type: type.ParameterType.STRING,
default: '200'
}, HEIGHT: {
type: type.ParameterType.STRING,
default: '100'
}, TITLE: {
type: type.ParameterType.STRING,
default: 'NewWindow'
}
}
});
api.addBlock({
opcode: 'jasonxu.alert.setUrl.opcode',
type: type.BlockType.COMMAND,
messageId: 'jasonxu.alert.setUrl',
categoryId: 'jasonxu.alert.alert',
function: args => {
if (window.clipAlert) {
if (alerting) return;
if (!bucket.isEmpty) {
new Promise(() => {
alerting = true;
clipAlert('提示', '项目正在尝试跳转到新页面' + args.URL)
.then(result => {
alerting = false;
if (result) window.location.href = args.URL;
return;
});
});
}
}
else if (!bucket.isEmpty) {
if (confirm('项目正在尝试跳转到新页面:' + args.URL)) window.location.href = args.URL;
}
return;
},
param: {
URL: {
type: type.ParameterType.STRING,
default: 'https://codingclip.com'
}
}
});
}
onUninit() {
api.removeCategory('jasonxu.alert.alert');
}
}
module.exports = AlertExtension;