This repository was archived by the owner on Oct 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathassistant.js
More file actions
110 lines (97 loc) · 3.17 KB
/
assistant.js
File metadata and controls
110 lines (97 loc) · 3.17 KB
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
/*
12306 Assistant
Copyright (C) 2012 flytreeleft (flytreeleft@126.com)
THANKS:
Hidden, Jingqin Lynn, 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/>.
*/
$(document).ready(function() {
$('body').bind({
'message': function() {
notify($(this).attr('message'));
}
});
if ($('head title').html() == '登录') {
if ($('head').html().match(/var\s+isLogin\s*=\s*true/g)) {
// 已经登录,则直接跳转到查询页面
window.location.href = 'https://dynamic.12306.cn/otsweb/order/querySingleAction.do?method=init';
} else {
chrome.extension.sendRequest({action: 'user'}, function(user) {
login(user);
});
}
} else if ($('head title').html() == '车票预订') {
chrome.extension.sendRequest({action: 'ticket'}, function(ticket) {
query(ticket);
});
}
});
function notify(msg, type) {
chrome.extension.sendRequest({action: 'notify', type: type, msg: msg}, function(response) {});
}
function play(type) {
chrome.extension.sendRequest({action: 'play', type: type}, function(response) {});
}
function login(user) {
$('body').append(
$('<script type="text/javascript" src="'+chrome.extension.getURL('assistant/login.js')+'"/>')
).bind({
'loginSuccess': function() {
notify('登录成功,开始查询车票吧!');
play('login');
window.location.href = 'https://dynamic.12306.cn/otsweb/order/querySingleAction.do?method=init';
}
});
for (var id in user) {
user[id] && $('#'+id).val(user[id]);
}
}
function query(ticket) {
$('body').append(
$('<script type="text/javascript" src="'+chrome.extension.getURL('assistant/query.js')+'"/>')
).append(
$('<script type="text/javascript" src="'+chrome.extension.getURL('assistant/book.js')+'"/>')
).bind({
'periodOfPresale': function() {
chrome.extension.sendRequest({action: 'periodOfPresale', value: $(this).attr('periodOfPresale')}, function() {});
},
'hasTicket': function() {
notify('现在有票,可以预定...');
play('ticket');
},
'bookTicket': function() {
notify('正在预定车票,请等待...');
},
'bookSuccess': function() {
notify('预订成功,请尽快完成订单并提交');
play('book');
book();
}
});
for (var id in ticket) {
ticket[id] && $('#'+id).val(ticket[id]);
}
}
function book() {
$('body').append(
$('<script type="text/javascript" src="'+chrome.extension.getURL('assistant/order.js')+'"/>')
).bind({
'orderSuccess': function() {
notify('订单提交成功,请在规定时间内完成支付');
play('order');
}
});
}