Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 20th, 2012  |  syntax: None  |  size: 5.31 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.                 12306 Auto Query => A javascript snippet to help you book tickets online.
  3.                 Copyright (C) 2011 Jingqin Lynn
  4.                
  5.                 Includes jQuery
  6.                 Copyright 2011, John Resig
  7.                 Dual licensed under the MIT or GPL Version 2 licenses.
  8.                 http://jquery.org/license
  9.  
  10.                 Includes Sizzle.js
  11.                 http://sizzlejs.com/
  12.                 Copyright 2011, The Dojo Foundation
  13.                 Released under the MIT, BSD, and GPL Licenses.
  14.                
  15.                 This program is free software: you can redistribute it and/or modify
  16.                 it under the terms of the GNU General Public License as published by
  17.                 the Free Software Foundation, either version 3 of the License, or
  18.                 (at your option) any later version.
  19.  
  20.                 This program is distributed in the hope that it will be useful,
  21.                 but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.                 GNU General Public License for more details.
  24.  
  25.                 You should have received a copy of the GNU General Public License
  26.                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
  27.  
  28. */
  29.  
  30. // ==UserScript==
  31. // @name                12306 Auto Query
  32. // @namespace           http://project.quietmusic.org/j/
  33. // @description A javascript snippet to help you book tickets online.
  34. // @include             *://dynamic.12306.cn/otsweb/order/querySingleAction.do*
  35.  
  36. // ==/UserScript==
  37.  
  38. function withjQuery(callback, safe){
  39.         if(typeof(jQuery) == "undefined") {
  40.                 var script = document.createElement("script");
  41.                 script.type = "text/javascript";
  42.                 script.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";
  43.                
  44.                 if(safe) {
  45.                         var cb = document.createElement("script");
  46.                         cb.type = "text/javascript";
  47.                         cb.textContent = "jQuery.noConflict();(" + callback.toString() + ")(jQuery);";
  48.                         script.addEventListener('load', function() {
  49.                                 document.head.appendChild(cb);
  50.                         });
  51.                 }
  52.                 else {
  53.                         var dollar = undefined;
  54.                         if(typeof($) != "undefined") dollar = $;
  55.                         script.addEventListener('load', function() {
  56.                                 jQuery.noConflict();
  57.                                 $ = dollar;
  58.                                 callback(jQuery);
  59.                         });
  60.                 }
  61.                 document.head.appendChild(script);
  62.         } else {
  63.                 callback(jQuery);
  64.         }
  65. }
  66.  
  67. withjQuery(function($){
  68.         var isTicketAvailable = false;
  69.  
  70.         //The table for displaying tickets
  71.         var tbl = $(".obj")[0];
  72.  
  73.         tbl.addEventListener("DOMNodeInserted", function() {
  74.                 if(checkTickets(event.target))
  75.                 {
  76.                         isTicketAvailable = true;
  77.                         highLightRow(event.target);
  78.                 }
  79.                 tbl.firstAppend=false;
  80.         }, true);
  81.  
  82.         //Trigger the button
  83.         var doQuery = function() {
  84.                 displayQueryTimes(queryTimes++);
  85.                 isTicketAvailable = false;
  86.                 tbl.firstAppend = true;
  87.                 g.firstRemove = true;
  88.                 document.getElementById(isStudentTicket ? "stu_submitQuery" : "submitQuery").click();
  89.         }
  90.  
  91.         var checkTickets = function(row) {
  92.                 var hasTicket = false;
  93.                 var canBook = true;
  94.                 $("td input[type=button]", row).each(function(i, e) {
  95.                         if(e.classList.contains("yuding_x")) {
  96.                                 canBook = false;
  97.                         }
  98.                 });
  99.                 if(!canBook) return false;
  100.                
  101.                 $("td", row).each(function(i, e) {
  102.                         if(ticketType[i-1]) {
  103.                                 var info = e.innerText.trim();
  104.                                 if(info != "--" && info != "无") {
  105.                                         hasTicket = true;
  106.                                         highLightCell(e);
  107.                                 }
  108.                         }
  109.                 });
  110.  
  111.                 return hasTicket;
  112.         }
  113.  
  114.         //The box into which the message is inserted.
  115.         var g = document.getElementById("gridbox");
  116.         //When the message is removed, the query should be completed.
  117.         g.addEventListener("DOMNodeRemoved", function() {
  118.                 if(g.firstRemove) {
  119.                         g.firstRemove = false;
  120.                         if (isTicketAvailable) {
  121.                                 if (isAutoQueryEnabled)
  122.                                         document.getElementById("refreshButton").click();
  123.                                 onticketAvailable(); //report
  124.                         }
  125.                         else {
  126.                                  //wait for the button to become valid
  127.                         }
  128.                 }
  129.         }, true);
  130.        
  131.         //hack into the validQueryButton function to detect query
  132.         var _validQueryButton = validQueryButton;
  133.        
  134.         validQueryButton = function() {
  135.                 _validQueryButton();
  136.                 if(isAutoQueryEnabled) doQuery();
  137.         }
  138.        
  139.         var queryTimes = 0; //counter
  140.         var isAutoQueryEnabled = false; //enable flag
  141.  
  142.         //please DIY:
  143.         var onticketAvailable = function() {
  144.                 if(Audio) {
  145.                         new Audio("http://upload.wikimedia.org/wikipedia/commons/8/8a/Canon_and_Gigue_in_D.ogg").play();
  146.                 }
  147.                 else {
  148.                         alert("可以订票了!")
  149.                 }
  150.         }
  151.         var highLightRow = function(row) {
  152.                 $(row).css("background-color", "red");
  153.         }
  154.         var highLightCell = function(cell) {
  155.                 $(cell).css("background-color", "blue");
  156.         }
  157.         var displayQueryTimes = function(n) {
  158.                 document.getElementById("refreshTimes").innerText = n;
  159.         };
  160.  
  161.         var isStudentTicket = false;
  162.  
  163.         //Control panel UI
  164.         $("<div/>").attr("style", "position:fixed;right:0;bottom:0;z-index:999;").append(
  165.                 $("<input/>").attr("type", "checkBox").change(function(){
  166.                         isStudentTicket = this.checked;
  167.                 })
  168.         ).append(
  169.                 $("<span/>").text("学生")
  170.         ).append(
  171.                 $("<button/>").attr("id", "refreshButton").text("自动刷新").click(function() {
  172.                         if(!isAutoQueryEnabled) {
  173.                                 isAutoQueryEnabled = true;
  174.                                 doQuery();
  175.                                 this.innerText="停止刷新";
  176.                         }
  177.                         else {
  178.                                 isAutoQueryEnabled = false;
  179.                                 this.innerText="自动刷新";
  180.                         }
  181.                 })
  182.         ).append(
  183.                 $("<p/>").text("尝试次数:").append(
  184.                         $("<span/>").attr("id", "refreshTimes").text("0")
  185.                 )
  186.         ).appendTo(document.body);
  187.        
  188.         //Ticket type selector & UI
  189.         var ticketType = new Array();
  190.         $(".hdr tr:eq(2) td").each(function(i,e) {
  191.                 ticketType.push(false);
  192.                 if(i<3) return;
  193.                 ticketType[i] = true;
  194.                
  195.                 var c = $("<input/>").attr("type", "checkBox").attr("checked", "true");
  196.                 c[0].ticketTypeId = i;
  197.                 c.change(function() {
  198.                         ticketType[this.ticketTypeId] = this.checked;
  199.                 }).appendTo(e);
  200.         });
  201. }, true);