Skip to content

Instantly share code, notes, and snippets.

@Senorsen
Last active August 29, 2015 14:16
Show Gist options
  • Save Senorsen/4fb096c10a1f1fdd5589 to your computer and use it in GitHub Desktop.
Save Senorsen/4fb096c10a1f1fdd5589 to your computer and use it in GitHub Desktop.
复旦选课网半自动脚本
/*
* @author Senorsen <sen@senorsen.com>
*/
var jquery_url = 'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.2.min.js',
script = document.createElement("script");
var stop_flag = false;
script.src = jquery_url;
document.getElementsByTagName("head")[0].appendChild(script);
function start_fetch(lessonid, verify, auto) {
if (typeof auto == 'undefined') stop_flag = false;
if (stop_flag) {
console.log('已停止');
stop_flag = false;
return false;
}
if (typeof verify == 'undefined') {
verify = $($('[name=leftFrame]')[0].contentDocument).find('#输入认证码').val();
if (verify == '') {
alert('错误:要在本函数第二个参数,或页面输入框内输入当前的验证码');
return false;
}
}
window.token = $($('[name=leftFrame]')[0].contentDocument).find('[name=token]').val();
$.post('http://xk.fudan.edu.cn/xk/sekcoursepeos.jsp', {xkh:lessonid, submit: '查询', model: '可选课程'}, function(data) {
window.data = data;
// exposing everything to global is just fine, for the convenience of debugging
target_tr = $('<div>' + window.data + '</div>').find('table[bgcolor=#9FD6FF] tr').eq(3);
var check1 = target_tr.find('td:nth-child(1)').text().trim(),
check2 = target_tr.find('td:nth-child(2)').text().trim(),
check3 = parseInt(target_tr.find('td:nth-child(4)').text().trim()),
check4 = parseInt(target_tr.find('td:nth-child(5)').text().trim());
if (check1 != lessonid && !auto) {
console.warn('警告:查到的课程为' + check1 + ' ' + check2 + ',与输入不一致,stop()以停止');
}
var is_ok = false;
if ((check3 > 0 && check4 == 0) || check3 > check4) is_ok = true;
console.log((is_ok ? '满足条件 ' : '不满足 ') + check1 + ' ' + check2 + ' 剩余人数: ' + check3 + ' 保留人数: ' + check4);
if (is_ok) {
start_attempt(lessonid, verify);
} else {
start_fetch(lessonid, verify, true);
}
}, 'text').fail(function() {
console.error('Xhr error, restart...');
setTimeout(function() {
start_fetch(lessonid, verify, true);
}, 1000);
});
}
function stop() {
stop_flag = true;
}
function start_attempt(lessonid, verify) {
$.post('http://xk.fudan.edu.cn/xk/doSelectServlet', {
token: window.token,
selectionId:lessonid,
xklb: 'ss',
rand: verify
}, function(data) {
window.adata = data;
if (/选课成功/.exec(adata)) {
console.log('选课成功');
} else {
console.error('可能发生错误,具体提示:' + adata.match(/alert\("(.+?)"\)/)[1]);
console.log('提示:如果错误是验证码相关的,可能需要手动刷新一下页面,然后再粘贴本代码操作');
}
}).fail(function() {
console.error('xhr error');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment