利用Tampermonkey写脚本抢课
学校抢课…实在抢不到,于是想到了利用脚本不断刷新页面,来捡漏子。
听了实验室大神的推荐,选用了tampermonkey插件来写脚本。
- 在谷歌应用商店搜索tampermonkey并安装
- 安装完后选择添加脚本
- 绑定执行脚本的页面
- 这里有个坑,关于要不要require jquery的问题。
- 如果绑定的页面中已引用jq了,就不要添加require jq了,否则会冲突
- 如果没有,则记得添加jq
然后写脚本就是了
以下是针对我抢课页面的脚本代码,写这篇博客的过程中,课已经抢到了,哈哈哈~
(function() {
'use strict';
//return;
$('table tr').each(function(){
var lesson = $(this).find("td").eq(1).children('span').text();
if(lesson == 'Java技术' || lesson == '有效共同技巧(MOOC)'){
if($(this).children('td').eq(7).children('a').text() != ''){
$(this).children('td').eq(7).children('a').trigger('click');
console.log(window.frames[0].document.querySelectorAll('input[id=contentParent_dgData_ImageButton1_0]'));
setTimeout(function(){
$(window.frames[0].document.querySelectorAll('input[id=contentParent_dgData_ImageButton1_0]')).click();
$('#contentParent_dgData_ImageButton1_0').click();
console.log('chose class now !');
}, 2000);
}
else{
location.reload();
}
}
});
// setTimeout(location.reload(), 10000);
// Your code here...
})();
代码由公司的大神帮忙写出来的,其中有些地方现在还不是很了解,因为涉及到页面中动态加载的内容,用普通的jq选择器没法选择到新增的元素,因此用到了
$(window.frames[0].document.querySelectorAll('input[id=contentParent_dgData_ImageButton1_0]')).click();
这段神奇的代码。有空我还是得好好学习下原生的javascript,值得学习的地方还很多呀。