|
发表于 2012-1-9 20:49:44
|
显示全部楼层
引用第0楼weist123于2012-01-09 19:43发表的 妙啊!网友开发出了《12306网站自动登录代码》!!! :
[b]妙啊!网友开发出了《12306网站自动登录代码》!!!
可惜啊,我不知怎样用,园地高手多,那位高人解读一下?急用!
========================================
/*
12306 Auto Login => A javascript snippet to help you auto login 12306.com.
Copyright (C) 2011 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/>.
*/
//以下注释
// ==UserScript==
// @name 12306 Auto Login
// @author kevintop@gmail.com
// @namespace https://plus.google.com/107416899831145722597
// @description A javascript snippet to help you auto login 12306.com
// @include *://dynamic.12306.cn/otsweb/loginAction.do*
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
// ==/UserScript==
function withjQuery(callback, safe){
if(typeof(jQuery) == "undefined") {//当前页jQuery变量如果不存在
var script = document.createElement("script"); //插入jQuery.js
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 url = "https://dynamic.12306.cn/otsweb/loginAction.do?method=login";
var queryurl = "https://dynamic.12306.cn/otsweb/order/querySingleAction.do?method=init";
function submitForm(){
//定义AJAX提交的变量
var submitUrl = url + "&loginUser.user_name=" + $("#UserName").val() + "&user.password=" + $("#password").val() + "&randCode=" + $("#randCode").val();
$.ajax({
//AJAX闭包
type: "OST",
url: submitUrl,
cache: false,
async: false,
success: function(msg){//有返回值,则分析返回值内容
if (msg.indexOf(’请输入正确的验证码’) > -1) {//如果返回值内容包含’请输入正确的验证码’字符串
alert(’请输入正确的验证码!’);//弹出要求输入验证码的对话框
};
if (msg.indexOf(’当前访问用户过多’) > -1) {{//如果返回值内容包’当前访问用户过多’字符串
reLogin();//执行重新登录函数
}
else {//如果不包含以上内容,则在id=’reloginNum’的表中插入 登录成功提示,并自动刷新当前页,以使页面变为用户登录状态
$(’#reloginNum’).html(’登录成功,请刷新!’);
location.replace(queryurl);
};
},
error: function(msg){ //如果没有返回值或者返回非200(如500错误),执行重新登录函数
reLogin();
},
beforeSend: function(XHR){//AJAX提交前数据处理,已注销
//alert("Data Saved: " + XHR);
}
});
}
function reLogin(){//重新登录函数
//将id=reloginNum的div中显示的重新登录次数+1,并重新存储,并在2000毫秒(2秒)后执行’submitForm()函数
var currNum = $(’#reloginNum’).html();
$(’#reloginNum’).html(parseInt(currNum) + 1);
setTimeout(’submitForm()’, 2000);
}
//初始化
$("body").prepend("<div style=’white-space:nowrap;’>重试次数:<div id=’reloginNum’ >0</div></div>");
$("#subLink").after("<a href=’#’ id=’autoSubmit’ class=’button_a’ ><span><ins>自动登录</ins> </span> </a>");
$("#autoSubmit").live("click", function(){
alert(’开始尝试登录,请耐心等待!’);
submitForm();
});
alert(’如果使用自动登录功能,请输入用户名、密码及验证码后,点击自动登录,系统会尝试登录,直至成功!’);
}, true);
....... 此内容很简单。我们再IE中登录的时候,每次错误都会刷新验证码,此处由于是脚本执行,页面中的验证码并没有得到刷新,所以只要sessio不失效,就一直使用第一次显示的验证码值向服务器发出请求,而不是每次都用新的验证码。 |
|