-
Notifications
You must be signed in to change notification settings - Fork 208
/
12306BookingAssistant.user.js
332 lines (308 loc) · 9.84 KB
/
12306BookingAssistant.user.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
/*
* 12306 Auto Query => A javascript snippet to help you book tickets online.
* 12306 Booking Assistant
* Copyright (C) 2011 Hidden
*
* 12306 Auto Query => A javascript snippet to help you book tickets online.
* Copyright (C) 2011 Jingqin Lynn
*
* 12306 Auto Login => A javascript snippet to help you auto login 12306.com.
* Copyright (C) 2011 Kevintop
*
* Includes jQuery
* Copyright 2011, John Resig
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// ==UserScript==
// @name 12306 Booking Assistant
// @version 1.1
// @author [email protected]
// @namespace https://github.com/zzdhidden
// @description 12306 订票助手之(自动登录,自动查票)
// @include *://dynamic.12306.cn/otsweb/loginAction.do*
// @include *://dynamic.12306.cn/otsweb/order/querySingleAction.do*
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
// ==/UserScript==
function withjQuery(callback, safe){
if(typeof(jQuery) == "undefined") {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";
if(safe) {
var cb = document.createElement("script");
cb.type = "text/javascript";
cb.textContent = "jQuery.noConflict();(" + callback.toString() + ")(jQuery);";
script.addEventListener('load', function() {
document.head.appendChild(cb);
});
}
else {
var dollar = undefined;
if(typeof($) != "undefined") dollar = $;
script.addEventListener('load', function() {
jQuery.noConflict();
$ = dollar;
callback(jQuery);
});
}
document.head.appendChild(script);
} else {
callback(jQuery);
}
}
withjQuery(function($){
$(document).click(function() {
if( window.webkitNotifications && window.webkitNotifications.checkPermission() != 0) {
window.webkitNotifications.requestPermission();
}
});
function notify(str, timeout, skipAlert) {
if( window.webkitNotifications && window.webkitNotifications.checkPermission() == 0 ) {
var notification = webkitNotifications.createNotification(
null, // icon url - can be relative
'订票', // notification title
str
);
notification.show();
if ( timeout ) {
setTimeout(function() {
notification.cancel();
}, timeout);
}
return true;
} else {
if( !skipAlert ) {
alert( str );
}
return false;
}
}
if( window.location.href.indexOf("querySingleAction.do") != -1 ) {
//query
var isTicketAvailable = false;
//The table for displaying tickets
var tbl = $(".obj")[0];
// Not work on IE
tbl.addEventListener("DOMNodeInserted", function() {
if(checkTickets(event.target))
{
isTicketAvailable = true;
highLightRow(event.target);
}
tbl.firstAppend=false;
}, true);
//Trigger the button
var doQuery = function() {
displayQueryTimes(queryTimes++);
tbl.firstAppend = true;
g.firstRemove = true;
document.getElementById(isStudentTicket ? "stu_submitQuery" : "submitQuery").click();
}
var checkTickets = function(row) {
var hasTicket = false;
var canBook = true;
$("td input[type=button]", row).each(function(i, e) {
if(e.classList.contains("yuding_x")) {
canBook = false;
}
});
if(!canBook) return false;
$("td", row).each(function(i, e) {
if(ticketType[i-1]) {
var info = e.innerText.trim();
if(info != "--" && info != "无") {
hasTicket = true;
highLightCell(e);
}
}
});
return hasTicket;
}
//The box into which the message is inserted.
var g = document.getElementById("gridbox");
//When the message is removed, the query should be completed.
g.addEventListener("DOMNodeRemoved", function() {
if(g.firstRemove) {
g.firstRemove = false;
if (isTicketAvailable) {
if (isAutoQueryEnabled)
document.getElementById("refreshButton").click();
onticketAvailable(); //report
}
else {
//wait for the button to become valid
}
}
}, true);
//hack into the validQueryButton function to detect query
var _validQueryButton = validQueryButton;
validQueryButton = function() {
_validQueryButton();
if(isAutoQueryEnabled) doQuery();
}
var queryTimes = 0; //counter
var isAutoQueryEnabled = false; //enable flag
//please DIY:
var audio = null;
var onticketAvailable = function() {
if(window.Audio) {
if(!audio) {
audio = new Audio("http://www.w3school.com.cn/i/song.ogg");
audio.loop = true;
}
audio.play();
notify("可以订票了!", null, true);
} else {
notify("可以订票了!");
}
}
var highLightRow = function(row) {
$(row).css("background-color", "red");
}
var highLightCell = function(cell) {
$(cell).css("background-color", "blue");
}
var displayQueryTimes = function(n) {
document.getElementById("refreshTimes").innerText = n;
};
var isStudentTicket = false;
//Control panel UI
var ui = $("<div>请先选择好出发地,目的地,和出发时间。 </div>")
.append(
$("<input id='isStudentTicket' type='checkbox' />").change(function(){
isStudentTicket = this.checked;
})
)
.append(
$("<label for='isStudentTicket'></label>").html("学生票 ")
)
.append(
$("<button style='padding: 5px 10px; background: #2CC03E;border-color: #259A33;border-right-color: #2CC03E;border-bottom-color:#2CC03E;color: white;border-radius: 5px;text-shadow: -1px -1px 0 rgba(0, 0, 0, 0.2);'/>").attr("id", "refreshButton").html("开始刷票").click(function() {
if(!isAutoQueryEnabled) {
isTicketAvailable = false;
if(audio && !audio.paused) audio.pause();
isAutoQueryEnabled = true;
doQuery();
this.innerText="停止刷票";
}
else {
isAutoQueryEnabled = false;
this.innerText="开始刷票";
}
})
)
.append(
$("<span>").html(" 尝试次数:").append(
$("<span/>").attr("id", "refreshTimes").text("0")
)
)
.append(
//Custom ticket type
$("<div>如果只需要刷特定的票种,请在余票信息下面勾选。</div>")
.append($("<a href='#' style='color: blue;'>只勾选坐票 </a>").click(function() {
$(".hdr tr:eq(2) td").each(function(i,e) {
$(this).find("input").attr("checked", $(this).text().indexOf("座") != -1 ).change();
});
return false;
}))
.append($("<a href='#' style='color: blue;'>只勾选卧铺 </a>").click(function() {
$(".hdr tr:eq(2) td").each(function(i,e) {
$(this).find("input").attr("checked", $(this).text().indexOf("卧") != -1 ).change();
});
return false;
}))
);
var container = $(".cx_title_w:first");
container.length ?
ui.insertBefore(container) : ui.appendTo(document.body);
//Ticket type selector & UI
var ticketType = new Array();
$(".hdr tr:eq(2) td").each(function(i,e) {
ticketType.push(false);
if(i<3) return;
ticketType[i] = true;
var c = $("<input/>").attr("type", "checkBox").attr("checked", true);
c[0].ticketTypeId = i;
c.change(function() {
ticketType[this.ticketTypeId] = this.checked;
console.log( this );
}).appendTo(e);
});
}
else if( window.location.href.indexOf("loginAction.do") != -1 ) {
//login
var url = "https://dynamic.12306.cn/otsweb/loginAction.do?method=login";
var queryurl = "https://dynamic.12306.cn/otsweb/order/querySingleAction.do?method=init";
//Check had login, redirect to query url
if( parent && parent.$ ) {
var str = parent.$("#username_ a").attr("href");
if( str && str.indexOf("sysuser/user_info") != -1 ){
window.location.href = queryurl;
return;
}
}
function submitForm(){
var submitUrl = url;
$.ajax({
type: "POST",
url: submitUrl,
data: {
"loginUser.user_name": $("#UserName").val()
, "user.password": $("#password").val()
, "randCode": $("#randCode").val()
},
timeout: 30000,
//cache: false,
//async: false,
success: function(msg){
if (msg.indexOf('请输入正确的验证码') > -1) {
alert('请输入正确的验证码!');
};
if (msg.indexOf('当前访问用户过多') > -1) {
reLogin();
}
else {
notify('登录成功,开始查询车票吧!');
window.location.href = queryurl;
};
},
error: function(msg){
reLogin();
},
beforeSend: function(XHR){
//alert("Data Saved: " + XHR);
}
});
}
var count = 1;
function reLogin(){
count ++;
$('#refreshButton').html("("+count+")次登录中...");
setTimeout(submitForm, 2000);
}
//初始化
$("#subLink").after($("<a href='#' style='padding: 5px 10px; background: #2CC03E;border-color: #259A33;border-right-color: #2CC03E;border-bottom-color:#2CC03E;color: white;border-radius: 5px;text-shadow: -1px -1px 0 rgba(0, 0, 0, 0.2);'/>").attr("id", "refreshButton").html("自动登录").click(function() {
count = 1;
$(this).html("(1)次登录中...");
notify('开始尝试登录,请耐心等待!', 4000);
submitForm();
return false;
}));
alert('如果使用自动登录功能,请输入用户名、密码及验证码后,点击自动登录,系统会尝试登录,直至成功!');
}
}, true);