A-A+
[52pojie]油猴JS脚本页面跳转变量无法传递求助
如题,本人初学JS,想写一个自动刷课的油猴脚本,遇到下面的问题,求大佬帮小弟看看,感激不尽!
1.进入选课页面,自动选择要刷的课;
2.进入课程详情页,自动选择要刷的章节,全部完成则跳转至选课页面;
3.在章节视频播放页面,判断刷课是否完成,全部完成则跳转至课程详情页
目前遇到的问题有两个:
1.自动刷完第一章节视频后,跳转至详课程详情页,仍自动播放第一章节的视频,不播放第二章节的视频;
2.课程播放结束后,并点击完成播放时,弹出对话框,导致无法自动返回课程详情页。
// ==UserScript==
// @name 安徽干部教育在线刷课脚本3.0
// @namespace http://tampermonkey.net/
// @version 2024-02-23
// @description 自动浏览和选择课程章节,自动播放视频
// @AuThor You
// @match https://www.ahgbjy.gov.cn/*
// @Icon https://www.google.com/s2/favicons?sz=64&domain=baidu.com
// @grant GM_setValue
// @grant GM_getValue
// @run-at document-start
// ==/UserScript==
//屏蔽弹窗
(function () {
'use strict';
// 重写 window.alert 方法以阻止警告窗口显示
window.alert = function() {};
// 重写 window.confirm 方法以阻止确认对话框显示
window.confirm = function() {
console.log('确认对话框被屏蔽了!原始消息: ' );
// 默认情况下,confirm返回false
return false;
};
// 重写 window.prompt 方法以阻止提示输入框显示
window.prompt = function() {
console.log('提示输入框被屏蔽了!原始消息: ' );
// 默认情况下,prompt返回空字符串
return '';
};
var currentURL = window.location.href;
// 选课页面
function xuanke() {
//判断当前页课程是否全部学完,如果学完点击下页,否则进入未学习的课程
var xxstat = document.getElementsByClassName("coursespan")
//如果最后一个节点也学完了,则全部完成,跳转下页
if (xxstat[xxstat.length-1].lastChild.innerHTML == "已完成"){
var NextPage= "https://www.ahgbjy.gov.cn/"+document.getElementsByClassName("pagination")[0].lastChild.previousSibling.previousSibling.firstChild.getAttribute("href");
window.location.href = NextPage;
//否则,找到第一个未学完的页面,点击开始学习
}else{
for(var n=0;n<xxstat.length;n++){
if (xxstat[n].lastChild.innerHTML!="已完成"){
xxstat[n].click();
break;
}
}
}
}
// 课程详情页
function xiangqing() {
var j = GM_getValue('jj');
var kewc = document.getElementsByClassName("col-md-2")
//判断课程是否全部完成,如果完成跳转至首页,否则点击开始学习
if (kewc[kewc.length-1].getElementsByTagName("span")[0].innerHTML == 100)
{
window.open("https://www.ahgbjy.gov.cn/pc/course/courselist.do?categoryid=&year=&coutype=0&mostNInput=0&mostHInput=0&mostTJInput=&keyword=", "_blank");
}else{
//点击开始学习
var xx = document.getElementsByClassName("btn btn-default startlearn");
xx[0].click();
}
}
// 视频课程
function StarAndExit() {
//播放视频
// 先暂停
setTimeout(function() {
document.getElementsByClassName("jp-stop")[0].click();
}, 1000);
// 再播放
setTimeout(function() {
document.getElementsByClassName("jp-play")[0].click();
}, 4000);
function myFunction(){
//视频课程
var a = document.getElementsByClassName("jp-current-time");
if (a.length > 0) {
var sc = a[0].innerHTML;
var timeflag = "00:00";
//判断播放完毕
if (sc === timeflag) {
// 点击完成播放,上传记录
var wcbtn = document.getElementsByClassName("btn btn-default")
// wcbtn[1].removeEventListener('click', function() {});
wcbtn[1].click();
}
}
}
//循环10S判断是否播放完成
setInterval(myFunction, 10000);
}
//ppt课程
function playscorm(){
var fr = document.mainFrame;
var s = fr.document.getElementsByClassName("continueStudyButton");
if (s.length>0) {
s[0].click();
} else {
console.error("Element with class 'continueStudyButton' not found.");
}
function dianjikaishi(){
// 在这里执行获取iframe中元素的操作
var fr = document.mainFrame;
//获取播放状态
var PlayStat = fr.document.getElementById("toPlay");
if(PlayStat.getAttribute("style").includes('block')){
PlayStat.click();
}
if (fr.document.getElementsByClassName("totalTimeLabel")[0].innerHTML==fr.document.getElementsByClassName("currentTimeLabel")[0].innerHTML) {
// 点击完成播放,上传记录
var wcbtn = document.getElementsByClassName("btn btn-default")
//wcbtn[1].removeEventListener('click', function() {});
wcbtn[1].click();
}
};
setInterval(dianjikaishi, 10000);
}
function StartFunc() {
// 根据当前页面执行相应操作
if (currentURL.includes('https://www.ahgbjy.gov.cn/pc/course/courselist.do')) {
xuanke(); // 开始选课
} else if (currentURL.includes('https://www.ahgbjy.gov.cn/pc/course/coursedetail.do')) {
xiangqing();
} else if (currentURL.includes('https://www.ahgbjy.gov.cn/pc/course/playvideo.do' )){
StarAndExit();
}else if (currentURL.includes('https://www.ahgbjy.gov.cn/pc/course/playscorm.do' )){
playscorm();
}
}
window.onload = function () {
// 调用需要在页面加载完成后执行的函数
StartFunc();
};
})();
其他人解答:
在油猴内直接使用window是获取不到真正的window的,需要使用unsafeWindow代替。所以你的代码里应该将window替换为unsafeWindow就可以了
通过此方法最终解决问题
文章来源:https://www.52pojie.cn/thread-1894876-1-1.html
论坛发帖者:shihdhr
发帖者主页:https://www.52pojie.cn/home.php?mod=space&uid=1304776&do=thread&view=me&from=space
页面截图:


布施恩德可便相知重
微信扫一扫打赏
支付宝扫一扫打赏