Skip to content

Instantly share code, notes, and snippets.

@quietlynn
Created January 3, 2012 12:01
Show Gist options
  • Save quietlynn/1554666 to your computer and use it in GitHub Desktop.
Save quietlynn/1554666 to your computer and use it in GitHub Desktop.
12306 Auto Query => A javascript snippet to help you book ticket
/*
12306 Auto Query => A javascript snippet to help you book tickets online.
Copyright (C) 2011-2012 Jingqin Lynn
Includes jQuery
Copyright 2011, John Resig
Dual licensed under the MIT or GPL Version 2 licenses.
http://jquery.org/license
Includes Sizzle.js
http://sizzlejs.com/
Copyright 2011, The Dojo Foundation
Released under the MIT, BSD, and GPL Licenses.
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 Auto Query
// @namespace http://project.quietmusic.org/j/
// @description A javascript snippet to help you book tickets online.
// @include *://dynamic.12306.cn/otsweb/order/querySingleAction.do*
// ==/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($){
var isTicketAvailable = false;
//The table for displaying tickets
var tbl = $(".obj")[0];
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 != "--" && 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(Audio) {
if(!audio) {
audio = new Audio("http://www.w3school.com.cn/i/song.ogg");
audio.loop = true;
}
audio.play();
}
else {
alert("可以订票了!");
}
}
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
$("<div/>").attr("style", "position:fixed;right:0;bottom:0;z-index:999;").append(
$("<input/>").attr("type", "checkBox").change(function(){
isStudentTicket = this.checked;
})
).append(
$("<span/>").text("学生")
).append(
$("<button/>").attr("id", "refreshButton").text("自动刷新").click(function() {
if(!isAutoQueryEnabled) {
isTicketAvailable = false;
if(audio && !audio.paused) audio.pause();
isAutoQueryEnabled = true;
doQuery();
this.innerText="停止刷新";
}
else {
isAutoQueryEnabled = false;
this.innerText="自动刷新";
}
})
).append(
$("<p/>").text("尝试次数:").append(
$("<span/>").attr("id", "refreshTimes").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);
});
}, true);
@tonylzw
Copy link

tonylzw commented Jan 13, 2012

正确的脚本在哪里,麻烦提供一下
感觉GOOGLE 的脚本已经被封了
提交订单怎么都提交不了
IE手动提交一下就提交了

Copy link

ghost commented Jan 13, 2012

@bird2005fly 同样有这个问题,勾选了学生票之后无法自动刷新

@fjptd
Copy link

fjptd commented Jan 15, 2012

现在生成订单时,会提示“很抱歉!当前提交订单的人过多,请稍后尝试。”,是不是请再写个JS,帮忙下哦

@assure
Copy link

assure commented Jan 16, 2012

现上是不是不好用了,一上午都没有登录进去

@wshwshe
Copy link

wshwshe commented Jan 17, 2012

今天用了下自动登录的还可以用,但是自动刷新票的不知道怎么用了。

@csgfox
Copy link

csgfox commented Jan 18, 2012

铁道部出新手段了,会一直显示某趟车有票,但提交订单就提示没票,所以你就刷不了了,一刷还是提示有票,然后就自动停止了

@wshwshe
Copy link

wshwshe commented Jan 19, 2012 via email

@zanghongtu2006
Copy link

zanghongtu2006 commented Jan 19, 2012 via email

@jmjustin
Copy link

自动提交订单那里铁道部是不是改程序了?我每次提交到第二次的时候都提示验证码出错了。各位有这种情况吗?

@zhang637
Copy link

请问一下代码什么意思?_validQueryButton 与 validQueryButton什么关系

//hack into the validQueryButton function to detect query
var _validQueryButton = validQueryButton;

validQueryButton = function() {
    _validQueryButton();
    if(isAutoQueryEnabled) doQuery();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment