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);
@tjulk
Copy link

tjulk commented Jan 9, 2012

go-home 项目能添加自主选择车次跟票种吗

@jingledog
Copy link

能有“改签”的自动查询票务情况吗?

@Blog2012
Copy link

Blog2012 commented Jan 9, 2012

IE 下面怎么不能自动记住密码和用户名 而且自动登录的字是乱码 进去之后没有自动刷票按钮 没有音乐 没有高亮 也没有自动提交功能 不知道这个大大能突破不?非常期待中

@quietlynn
Copy link
Author

@Blog2012 懒得给破浏览器写奇怪的代码的说。反正如果有人愿意修改的话就 fork 一个好了。

@arronzhang
Copy link

@Blog2012 测试一下 https://github.com/zzdhidden/12306 修了IE上的几个BUG... 那个脚本需要保存为utf-8编码才不会乱码...

@Blog2012
Copy link

昨天晚上已经发现了最新版本 非常完美了 在IE上面可以自动登录 自动刷票 自动提交 关键是还可以提前修改乘车日期 提前进入预定界面 而且增加了 自动提交频率控制(太快会被封IP的) 但是小小的问题是进去之后IE上面那个勾选特殊票种 默认为无 已经很不错了 非常完美了 谢谢各位大大 在此学习到了很多 虽然现在已经自己的票买到了 还在帮同事买 脚本已经很完美了 但是我会一直跟踪学习的 新年快乐!

@gauzeehom
Copy link

下来,学习学习

@fundon
Copy link

fundon commented Jan 10, 2012

@zzdhidden 可以转成UTF-16

@tjulk
Copy link

tjulk commented Jan 10, 2012

为什么我的ie不好使呢

@jmjustin
Copy link

刚刚用zzdhidden的程序,订上票了。特来感谢。一并感谢 quietlynn和 kevintop两位。谢啦。

@willlau
Copy link

willlau commented Jan 10, 2012

人太多的情况下,登录页面都加载不出来,提示Access Denied拒绝访问,手动刷太费劲了。建议加一个自动刷新,登录页面出来后便停止的功能。

@sunyanteng
Copy link

自动登陆可用,自动查询不可以。firefox下报tbl not defined,代码插不进去了。有人遇到相同情况么?
Error: tbl is undefined
Source File: file:///C:/Documents%20and%20Settings/terrencesun/Application%20Data/Mozilla/Firefox/Profiles/idju15g9.default/gm_scripts/12306_booking_assistant/12306_booking_assistant.user.js
Line: 113
Error: Permission denied for http://www.12306.cn to get property Window.iFrameHeight
Source File: http://www.12306.cn/mormhweb/ggxxfw/wbyyzj/201105/t20110529_1905.jsp?height=740
Line: 11

@roseforyou
Copy link

@Blog2012
Copy link

Chrome在公司不能安装 IE其实已经兼容了可以自动登录 自动刷票 修改购票日期提前进入订票界面 自动提交 已经做的很完美了 但是不知道为嘛突然又不能自动登录了 都自动登录了106次了郁闷ing

@creazestone
Copy link

首先感谢各位大大, 我想提一个需求, 在提示有票的同时, 还可以继续刷票, 因为有的车并不是你想坐的, 但他提示有票后, 又要手动点击刷新!!!

@quietlynn
Copy link
Author

@creazestone: 其实不妨做一个『忽略指定车次』的功能。不过我现在没有帐号,无法调试,欢迎各位 fork 实现。

@suncrash
Copy link

好像已经被12306封杀了,使用后就会出现Access Denied。换个IP就能打开

@puyj8818
Copy link

自动提交订单后,刷了300多次都还在继续刷是怎么回事啊?

@tonylzw
Copy link

tonylzw commented Jan 12, 2012

确实,IE 的自动登录程序是不能用,GOOgle浏览器不能支付,还是不够完美

@Blog2012
Copy link

?IE上面已经可以兼容了啊 自动登录 自动刷票 修改日期 自动提交 可能是你的脚本不对吧

@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