-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
206 lines (151 loc) · 5.05 KB
/
content.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
let err = (msg) => {
//throw new Error(msg);
console.log(msg);
};
//submit form
let submit = (callback) => {
let frm = $('#query_form');
$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
success: function (data) {
callback(data);
},
error: function (data) {
err('Submit MTR form error');
}
});
};
//handle captcha
let captcha = (callback) => {
chrome.runtime.sendMessage({
type: "captcha"
}, function (res) {
if (res && res['type'] && res['type'] === "captcha" && res['data'] && res['data']['code']) {
callback(res.data.code);
}
else {
err('Received error code from background.js');
}
});
};
//make message
let makemsg = (data) => {
let trainNum = data.length;
let arrMsg = [];
data.forEach((val, idx, arr) => {
let arrPrice = [];
val.seatTypes.forEach(v => {
if (v.availFlag === true) {
arrPrice.push(v.price);
}
});
let msg = `No. ${idx}, ${val.stationTrainCode}(${val.arriveTime}-${val.departTime}), Price. (${arrPrice.join('/')})`;
arrMsg.push(msg);
});
return `${trainNum} Train found. ${arrMsg.join(" | ")};`;
};
//do send sms
let sendsms = (msg, tel, callback) => {
chrome.runtime.sendMessage({
type: "sms",
data: { msg, tel }
}, function (res) {
if (res && res['type'] && res['type'] === "sms" && res['data'] && res['data']['success'] === true) {
callback(res.data.success);
}
else {
err('sms send error from background.js');
}
});
}
(() => {
//Init UI
let ui = $(`
<div class="searchcontent_t">
<div class="searchcontent1_t">
<ul>
<li>
<span class="required-field">*</span>
轮询间隔(分钟)<br>
<span style="white-space: nowrap">
<input id="intervalNum" type="text" value="5" class="ui-autocomplete-input" autocomplete="off">
</span>
</li>
<li>
<span class="required-field">*</span>
电话号码(中国内地)<br>
<span style="white-space: nowrap">
<input id="telNum" type="text" value="" class="ui-autocomplete-input" autocomplete="off">
</span>
</li>
</ul>
</div>
</div>
<div class="searchbutton_t">
<input type="button" class="searchbutton5" border="0" value="开始轮询" id="startBtn">
<input type="button" class="searchbutton5" border="0" value="停止监控" id="stopBtn">
</div>
`);
$('.searchbg_t').append(ui);
//main interval function
let interval_function = () => {
let telNum = $('#telNum').val();
//Handle captcha
captcha(code => {
$('#captcha').val(code);
//submit
submit(res => {
console.log(res);
if (res['success'] === true) {
//has tickets
console.log(makemsg(res.data));
sendsms(makemsg(res.data), telNum, (smsRes) => {
if (smsRes) {
console.log('Successful send message, close timer.');
clearInterval(interval_timer);
interval_timer = null;
}
});
}
});
});
};
//setInterval
let interval_timer = null;
$('#startBtn').click(() => {
let intervalNum = $('#intervalNum').val() - 0;
if (intervalNum < 5) {
alert('因成本问题,轮训时间在5分钟以上。');
return;
}
let telNum = $('#telNum').val();
if (/^1[34578]\d{9}$/.test(telNum) === false) {
alert("手机号码有误,请重填");
return;
}
if (!interval_timer) {
interval_timer = setInterval(interval_function, intervalNum * 1000 * 60);
$('#intervalNum').prop('readonly', false);
$('#telNum').prop('readonly', false);
$('#startBtn').prop('disabled', true);
$('#stopBtn').prop('disabled', false);
alert('监控已开始');
}
});
$('#stopBtn').click(() => {
if (interval_timer) {
clearInterval(interval_timer);
interval_timer = null;
$('#intervalNum').prop('readonly', true);
$('#telNum').prop('readonly', true);
$('#startBtn').prop('disabled', false);
$('#stopBtn').prop('disabled', true);
alert('监控已停止');
}
});
//default lock stop button
$('#stopBtn').prop('disabled', true);
})();