-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautoquery_old.js
238 lines (212 loc) · 6.93 KB
/
autoquery_old.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
var isTicketAvailable = false;
//The table for displaying tickets
var tbl = $(".obj")[0];
tbl.addEventListener("DOMNodeInserted", function() {
if(checkTickets(event.target))
{
if (! isTicketAvailable) {
onticketAvailable(); //report
}
isTicketAvailable = true;
highLightRow(event.target);
}
tbl.firstAppend=false;
}, true);
var propCheckInterval = 1000;
var waitHandle = null;
var queryAvailable = false;
var availCheckCount = 0;
function waitQueryAvailable()
{
queryAvailable = false;
if (isStudentTicket) {
if ($("#stu_submitQuery").attr("disabled") == false){
queryAvailable = true;
}
} else {
var $qbCls = $("#submitQuery").attr("class");
if ($qbCls.substr(-2) == "_u") {
queryAvailable = true;
}
}
if (queryAvailable) {
cancelQueryWait();
doQuery();
} else if(isAutoQueryEnabled) {
++ availCheckCount;
$("#availCheckTimes").text("Checking ... " + availCheckCount );
waitHandle = window.setTimeout (waitQueryAvailable, propCheckInterval);
}
}
function cancelQueryWait()
{
availCheckCount = 0;
$("#availCheckTimes").text("");
window.clearTimeout(waitHandle);
waitHandle = null;
}
//not used yet.
function expandObjectBox(){
$("#gridbox").attr("style", "background-color:white; overflow-x:hidden; overflow-y:visible; width:100%; height: 913px; cursor:default;");
var objBox = $("div.objbox");
if (objBox && objBox != undefined) {
objBox.attr("style", "width:100%; position: relative; top: -137px; height: 850px; overflow-y:visible; overflow-x:auto;");
console.log (objBox.attr("style"));
} else {
console.log ("Can not aquire object box div object");
}
}
//Trigger the button
var doQuery = function() {
displayQueryTimes(queryTimes++);
isTicketAvailable = false;
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) {
expandObjectBox();
if (isAutoQueryEnabled) {//switch auto query off when got tickets
$("#refreshButton").click();
}
} else {
//wait for the button to become valid
if (isAutoQueryEnabled) {
//may be the query button would be availed soon after query finished
waitHandle = window.setTimeout(waitQueryAvailable, propCheckInterval / 2);
}
}
}
}, true);
//Disabled because content-script can not access page script
//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 onticketAvailable = function() {
chrome.extension.sendRequest({what:"ticketNotify"}, function(resp){
if (! resp.ok) {
alert ("You've got tickets");
}
});
}
var highLightRow = function(row) {
$(row).css("background-color", "#AAFFDD");
}
var highLightCell = function(cell) {
$(cell).css("background-color", "#AADDFF");
}
var displayQueryTimes = function(n) {
document.getElementById("refreshTimes").innerText = n;
};
var isStudentTicket = false;
//Control panel UI
$("<div/>").attr("style", "position:fixed;top:0;left:0; width:700px; height: 50px; background-color:#FFFFDD; font-size:18px;").append(
$("<input/>").attr("type", "checkBox").change(function(){
isStudentTicket = this.checked;
})
).append(
$("<span/>").text("Students Ticket ")
).append(
$("<button/>").attr("id", "refreshButton").attr("class", "search_u").attr("style", "color:#FA0;").text("Start Auto Refresh").click(function() {
if(!isAutoQueryEnabled) {
isAutoQueryEnabled = true;
doQuery();
this.innerText="Stop Auto Query";
//enable it
var stationsToSave = {from: {name:($("#fromStationText").attr("value")), code:($("#fromStation").attr("value"))},
to: {name: ($("#toStationText").attr("value")), code: ($("#toStation").attr("value"))}
};
console.log ("CS: prepare to save:" + stationsToSave.from.code + " --> " + stationsToSave.to.code);
chrome.extension.sendRequest({what: "saveStations", stations:stationsToSave}, function(resp){});
}
else {
//disable it
cancelQueryWait();
isAutoQueryEnabled = false;
this.innerText="Start Auto Query";
}
})
).append(
$("<span/>").text(" Query Count: ").append(
$("<span/>").attr("id", "refreshTimes").text("0")
)
).append(
$("<span/>").attr("id", "availCheckTimes").text("0")
).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;
}).appendTo(e);
});
Date.prototype.format = function(format) //author: meizz
{
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
(this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)if(new RegExp("("+ k +")").test(format))
format = format.replace(RegExp.$1,
RegExp.$1.length==1 ? o[k] :
("00"+ o[k]).substr((""+ o[k]).length));
return format;
}
var preSaleDays = 12;
var lastPreSaleDate = new Date( (new Date()).getTime() + 1000 * (preSaleDays - 1) * 24 * 3600);
$("#startdatepicker").attr("value", lastPreSaleDate.format("yyyy-MM-dd"));
function defval(val1, defvalue)
{
console.log ("Val1 = " + val1);
return val1 ? val1 : defvalue;
}
chrome.extension.sendRequest ({what: "stations"}, function (stations) {
$("#fromStationText").attr("value", defval(stations.from.name, "哈尔滨"));
$("#fromStation").attr("value", defval(stations.from.code, "HBB"));
$("#toStationText").attr("value", defval(stations.to.name, "北京"));
$("#toStation").attr("value", defval(stations.to.code, "BJP"));
});