鱼C论坛

 找回密码
 立即注册
查看: 294|回复: 5

[已解决]请问为什么下面的代码在后台运行时,无法进行初始化,以及进行有效的网页上下移动

[复制链接]
发表于 2024-1-22 16:28:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
// ==/UserScript==
var study_css = ".egg_study_btn{outline:0;border:0;position:fixed;top:5px;left:5px;padding:12px 20px;border-radius:10px;cursor:pointer;background-color:#fff;color:#d90609;font-size:18px;font-weight:bold;text-align:center;box-shadow:0 0 9px #666777}.egg_manual_btn{transition:0.5s;outline:none;border:none;padding:12px 20px;border-radius:10px;cursor:pointer;background-color:#e3484b;color:rgb(255,255,255);font-size:18px;font-weight:bold;text-align:center;}.egg_auto_btn{transition:0.5s;outline:none;border:none;padding:12px 20px;border-radius:10px;cursor:pointer;background-color:#666777;color:rgb(255,255,255);font-size:18px;font-weight:bold;text-align:center;}.egg_setting_box{position:fixed;top:70px;left:5px;padding:12px 20px;border-radius:10px;background-color:#fff;box-shadow:0 0 9px #666777}.egg_setting_item{margin-top:5px;height:30px;width:140px;font-size:16px;display:flex;justify-items:center;justify-content:space-between}input[type='checkbox'].egg_setting_switch{cursor:pointer;margin:0;outline:0;appearance:none;-webkit-appearance:none;-moz-appearance:none;position:relative;width:40px;height:22px;background:#ccc;border-radius:50px;transition:border-color .3s,background-color .3s}input[type='checkbox'].egg_setting_switch::after{content:'';display:inline-block;width:1rem;height:1rem;border-radius:50%;background:#fff;box-shadow:0,0,2px,#999;transition:.4s;top:3px;position:absolute;left:3px}input[type='checkbox'].egg_setting_switch:checked{background:#fd5052}input[type='checkbox'].egg_setting_switch:checked::after{content:'';position:absolute;left:55%;top:3px}.egg_qrcode_box{height:218px;width:218px;position: fixed;top: 400px;left: 5px;padding: 10px;border-radius: 10px;background-color: #fff;box-shadow: 0 0 9px #666777;}.egg_qrcode_title{padding: 5px 0 12px 0;font-size: 18px;text-align: center;color: #d90609;font-weight: bold;letter-spacing: 2px;}.egg_tip{position: fixed;z-index: 999;top: 5px;left: calc(50% - 120px);padding: 0px 20px; line-height: 44px;text-align: center; width: 200px; height: 44px;font-size: 18px;}.egg_tip_success{color: #67c23a; background-color: #f0f9eb;}.egg_tip_warning{color: #E6A23C; background-color: #fdf6ec;}.egg_tip_danger{color: #d90609; background-color: #fef0f0;}.egg_tip_info{color: #909399; background-color: #edf2fc;}";
GM_addStyle(study_css);
//https://www.xuexi.cn/lgdata/3uoe1tg20en0.json
//查询今日完成情况
const SearchSocreUrl = "https://pc-proxy-api.xuexi.cn/delegate/score/days/listScoreProgress?sence=score&deviceType=2";
//重要新闻列表(主)
const NewsUrl1 = "https://www.xuexi.cn/lgdata/1jscb6pu1n2.json";
//学习时评新闻列表
const NewsUrl2 = "https://www.xuexi.cn/lgdata/1ap1igfgdn2.json";
//新闻视频列表
const VideosUrl1 = "https://www.xuexi.cn/lgdata/3o3ufqgl8rsn.json";
//新闻视频列表
const VideosUrl2 = "https://www.xuexi.cn/lgdata/1742g60067k.json";
//每日答题页面
const ExamPracticeUrl = "https://pc.xuexi.cn/points/exam-practice.html";
//每周答题列表API
const ExamWeeklyListUrl = "https://pc-proxy-api.xuexi.cn/api/exam/service/practice/pc/weekly/more?pageNo={pageNo}&pageSize=50";
//专项答题列表API
const ExamPaperListUrl = "https://pc-proxy-api.xuexi.cn/api/exam/service/paper/pc/list?pageSize=50&pageNo={pageNo}";
//题目API(只有每周答题和专项练习)
//const ExamQueryUrl = "https://pc-proxy-api.xuexi.cn/api/exam/service/detail/queryV3?type={type}&id={id}&forced=true";//type=1专项练习,type=2每周答题
//每周答题页面
const ExamWeeklyUrl = "https://pc.xuexi.cn/points/exam-weekly-detail.html?id={id}";
//专项测试页面
const ExamPaperUrl = "https://pc.xuexi.cn/points/exam-paper-detail.html?id={id}";
//文本服务器保存API
const AnswerSaveUrl = "https://a6.qikekeji.com/txt/data/save/";
//文本服务器获取API
const AnswerDetailUrl = "https://a6.qikekeji.com/txt/data/detail/";
//获取强国二维码
const GetQRcodeUrl = "https://login.xuexi.cn/user/qrcode/generate";
//监听是否扫码登录成功Url   qrCode:  goto: https://oa.xuexi.cn  pdmToken:
const LisenScanQRcodeUrl = "https://login.xuexi.cn/login/login_with_qr";
//在线生成二维码
const GenerateQRcodeUrl = "https://tool.oschina.net/action/qrcode/generate?data={url}&output=image%2Fpng&error=L&type=0&margin=0&size=4&{time}";
//获取当前日期
var currDate = new Date().toISOString().split('T')[0];
var newsNum = 6;
var news = [];
var videoNum = 6;
var videos = [];
//配置
var settings = [];
var pause = false;//是否暂停答题
var hasSlideVerification = false;
//每周答题当前页码
var examWeeklyPageNo = 1;
//每周答题总页码
var examWeeklyTotalPageCount = null;
//专项答题当前页码
var examPaperPageNo = 1;
//专项答题总页码
var examPaperTotalPageCount = null;
//颜色配置
const Colors = {"primary" : "#409EFF","success" : "#67C23A","warning" : "#E6A23C","danger" : "#d90609","info" : "#909399"};
$(document).ready(function(){
    let url = window.location.href;
    if(url == "https://www.xuexi.cn" || url == "https://www.xuexi.cn/" || url == "https://www.xuexi.cn/index.html"){
        let ready = setInterval(function() {
            if(document.getElementsByClassName("text-wrap")[0]) {
                clearInterval(ready);//停止定时器
                //初始化设置
                initSetting();
                //初始化二维码
                initQRcode();
                //创建"开始学习"按钮
                createStartButton();

                var saveSettingbtn = document.querySelector("#saveSetting");
                //添加事件监听
                try{// Chrome、FireFox、Opera、Safari、IE9.0及其以上版本
                    saveSettingbtn.addEventListener("click",saveSetting,false);
                }catch(e){
                    try{// IE8.0及其以下版本
                        saveSettingbtn.attachEvent('onclick',saveSetting);
                    }catch(e){// 早期浏览器
                        console.log("不学习何以强国error: 开始学习按钮绑定事件失败")
                    }
                }

                //获取完成日期
                //                 let finishDate = GM_getValue('finishDate');
                //                 if(finishDate && finishDate == new Date().getDate()){
                //                     //如果上一次完成的日期就是今天,那么不用学习了
                //                     //设置定时器,刷新页面以保持登录状态
                //                     setTimeout(function(){
                //                         location.reload();
                //                     },30*60*1000);//30分钟刷新一次
                //                 }else{
                //                     //否则,如果没有上一次完成的日期,或者不是今天,那么就开始学习
                //                     //start();
                //                 }
            }
        }, 800);
    }else if(url.indexOf("login.html") != -1){
        document.querySelector("#body-body").style.overflow = "hidden";
        document.querySelector(".layout-header").remove();
        document.querySelector(".layout-footer").remove();
        document.querySelector(".redflagbox").remove();
        document.querySelector(".oath").remove();
        document.querySelector("audio").remove();
        document.querySelector("iframe").remove();
        setTimeout(function(){
            let loginbox = document.querySelector(".loginbox-inner");
            console.log(loginbox)
            loginbox.style.position = "fixed";
            loginbox.style.top = "0";
            loginbox.style.left = "0";
            loginbox.style.zIndex = "999";
        },1000);
        let num = 0;
        GM_setValue("loginNum",num);
        setInterval(function(){
            num++;
            GM_setValue("loginNum",num);
        },400);

    }else if(url == GM_getValue("readingUrl")){
        try{
            let settingTemp = JSON.parse(GM_getValue('studySetting'));
            if(!settingTemp[7]){
                createTip();//创建学习提示
            }
            reading(0);
        }catch(e){
            createTip();//创建学习提示
            reading(0);
        }
    }else if(url == GM_getValue("watchingUrl")){
        try{
            let settingTemp = JSON.parse(GM_getValue('studySetting'));
            if(!settingTemp[7]){
                createTip();//创建学习提示
            }
        }catch(e){
            createTip();//创建学习提示
        }
        let randNum = 0;
        var checkVideoPlayingInterval = setInterval(function(){
            let temp = getVideoTag();
            if(temp.video){
                if(!temp.video.muted){
                    temp.video.muted = true;
                }
                if(temp.video.paused){
                    temp.video.paused = false;
                    console.log("正在尝试播放视频")
                    if(randNum == 0){//尝试使用js的方式播放
                        try{
                            temp.video.play();//尝试使用js的方式播放
                        }catch(e){}
                        randNum++;
                    }else{
                        try{
                            temp.pauseButton.click();//尝试点击播放按钮播放
                        }catch(e){}
                        randNum--;
                    }
                }else{
                    console.log("成功播放")
                    clearInterval(checkVideoPlayingInterval);
                    reading(1);
                }
            }else{
                console.log("等待加载")
            }
        },800);
    }else if(url.indexOf("exam") != -1 && url.indexOf("list") == -1){
        //答题页面
        let ready = setInterval(function() {
            if(document.getElementsByClassName("title")[0]) {
                clearInterval(ready);//停止定时器
                //创建“手动答题”按钮
                createManualButton();
                //开始答题
                doingExam();

                let examInterval = setInterval(function(){
                    if(hasSlideVerification){
                        clearInterval(examInterval)
                    }else{
                        slideVerification();
                    }
                },1000)
                }
        }, 500);
    }else{//否则直接全局静音

    }
});


//获取video标签
function getVideoTag(){
    let iframe = document.getElementsByTagName("iframe")[0];
    let video = null;
    let pauseButton = null;
    if(iframe){
        //如果有iframe,说明外面的video标签是假的
        video = iframe.contentWindow.document.getElementsByTagName("video")[0];
        pauseButton = iframe.contentWindow.document.getElementsByClassName("prism-play-btn")[0];
    }else{
        //否则这个video标签是真的
        video = document.getElementsByTagName("video")[0];
        pauseButton = document.getElementsByClassName("prism-play-btn")[0];
    }
    return {
        "video":video,
        "pauseButton":pauseButton
    }
}

//读新闻或者看视频
//type:0为新闻,1为视频
async function reading(type){
    //看文章或者视频
    let time = parseInt(Math.random()*(90-75+1)+75,10) ;//75-90秒后关闭页面
    let firstTime = time - 2;
    let secendTime = 12;
    let scrollLength = document.body.scrollHeight/2;
    var readingInterval = setInterval(function(){
        time--;
        $("#studyTip").text(time + " 秒后关闭页面");
        if(time <= firstTime){
            try{
                $("html,body").animate({scrollTop:394},1000);
            }catch(e){
                window.scrollTo(0,394);
            }
            firstTime = -1;
        }
        if(time <= secendTime){
            try{
                $("html,body").animate({scrollTop:scrollLength/3},1000);
            }catch(e){
                window.scrollTo(0,scrollLength/3);
            }
            secendTime = -1;
        }
        if(time <= 0){
            if(type == 0){
                GM_setValue('readingUrl',null);
            }else{
                GM_setValue('watchingUrl',null);
            }
            clearInterval(readingInterval);
            window.close();
        }
    },1000);
    //关闭文章或视频页面
}
//创建学习提示
function createTip(){
    let tipInfo = document.createElement("div");
    //添加样式
    tipInfo.setAttribute("id","studyTip");
    tipInfo.innerText = "正在初始化....";
    tipInfo.style.position = "fixed";
    tipInfo.style.bottom = "15px";
    tipInfo.style.left = "5px";
    tipInfo.style.padding = "12px 14px";
    tipInfo.style.border = "none";
    tipInfo.style.borderRadius = "10px";
    tipInfo.style.backgroundColor = "#222222";
    tipInfo.style.color = "#ffffff";
    tipInfo.style.fontSize = "14px";
    tipInfo.style.fontWeight = "bold";
    //插入节点
    let body = document.getElementsByTagName("body")[0];
    body.append(tipInfo)
}
//等待窗口关闭
function waitingClose(newPage){
    return new Promise(resolve => {
        let doing = setInterval(function() {
            if(newPage.closed) {
                clearInterval(doing);//停止定时器
                resolve('done');
            }
        }, 1000);
    });
}
//阅读文章
async function readNews(){
    await getNews();
    for(let i =0; i < news.length; i++){
        GM_setValue('readingUrl',news.url);
        console.log("正在看第" + (i+1) + "个新闻");
        let newPage = GM_openInTab(news.url,{active: true,insert: true, setParent :true});
        await waitingClose(newPage);
        await waitingTime(1500);
    }
}
//获取新闻列表
function getNews(){
    return new Promise(resolve => {
        news = new Array();
        let n = 6;
        if(newsNum < 6){//如果需要学习的新闻数量不到6,也就是已经学过了,但是积分不够,补的
            n = newsNum;
        }
        console.log("还需要看" + n + "个新闻")
        //新闻数量是否足够
        let enough = true;
        //获取重要新闻
        $.ajax({
            type: "GET",
            url: NewsUrl1,
            dataType: "json",
            success: function(data){
                let j = 0;
                if(n == 6){//如果今天还没学过,则优先找今天的新闻
                    for(let i = 0; i < n; i++){
                        //如果有当天日期的,则加入
                        if(data[j].auditTime.indexOf(currDate) != -1){
                            news.push(data[j]);
                            j++;
                        }else{//否则跳出循环
                            break;
                        }
                    }
                }
                for(j; j < n; j++){
                    let temp = parseInt(Math.random()*(data.length+1),10);
                    news.push(data[temp]);
                }
                resolve('done');
            },
            error: function(){
                news = null;
                resolve('done');
            }
        });
    });
}
//获取视频列表
function getVideos(){
    return new Promise(resolve => {
        videos = new Array();
        let n = 6;
        if(videoNum < 6){//如果需要学习的视频数量不到6,也就是已经学过了,但是积分不够,补的
            n = videoNum;
        }
        console.log("还需要看" + n + "个视频")
        $.ajax({
            type: "GET",
            url: VideosUrl1,
            dataType: "json",
            success: function(data){
                let j = 0;
                if(n == 6){
                    for(let i = 0; i < n; i++){
                        //如果有当天日期的,则加入
                        if(data[j].auditTime.indexOf(currDate) != -1){
                            videos.push(data[j]);
                            j++;
                        }else{//否则跳出循环
                            break;
                        }
                    }
                }
                for(j; j < n; j++){
                    let temp = parseInt(Math.random()*(data.length+1),10);
                    videos.push(data[temp]);
                }
                resolve('done');
            },
            error: function(){
                videos = [];
                resolve('done');
            }
        });
    });
}
//看学习视频
async function watchVideo(){
    await getVideos();
    for(let i =0; i < videos.length; i++){
        GM_setValue('watchingUrl',videos.url);
        console.log("正在观看第" + (i+1) + "个视频");
        let newPage = GM_openInTab(videos.url,{active: true,insert: true, setParent :true})
        await waitingClose(newPage);
        await waitingTime(1500);
    }
}
//做每日答题
function doExamPractice(){
    return new Promise(resolve => {
        console.log("正在完成每日答题")
        let newPage = GM_openInTab(ExamPracticeUrl,{active: true,insert: true, setParent :true});
        let doing = setInterval(function() {
            if(newPage.closed) {
                clearInterval(doing);//停止定时器
                resolve('done');
            }
        }, 1000);
    });
}
//获取专项答题列表
function getExamPaper(){
    return new Promise(function(resolve) {
        $.ajax({
            type: "GET",
            url: ExamPaperListUrl.replace("{pageNo}",examPaperPageNo),
            xhrFields: {
                withCredentials: true //如果没有这个请求失败
            },
            dataType: "json",
            success: function(data){
                console.log(data)
                data = data.data_str;
                if(data.indexOf("-") != -1){
                    data = data.replace(/-/g, "+");
                }
                if(data.indexOf("_") != -1){
                    data = data.replace(/_/g, "/");
                }
                data = decodeURIComponent(escape(window.atob(data)));
                //JSON格式化
                data = JSON.parse(data);
                resolve(data);
            },
            error: function(){
                resolve(new Array());
            }
        });
    })
}
//查询专项答题列表看看还有没有没做过的,有则返回id
async function findExamPaper(){
    var continueFind = true;
    var examPaperId = null;
    console.log("正在寻找未完成的专项答题")
    while(continueFind){
        await getExamPaper().then(data => {
            if(data){
                if(examPaperTotalPageCount == null){
                    //如果总页码没初始化,则初始化
                    examPaperTotalPageCount = data.totalPageCount;
                }
                let examPapers = data.list;//获取专项答题的列表
                for(let j = 0; j < examPapers.length; j++){
                    //遍历查询有没有没做过的
                    if(examPapers[j].status != 2){//status: 1为"开始答题" , 2为"重新答题"
                        //如果不是"重新答题",则可以做
                        examPaperId = examPapers[j].id;
                        continueFind = false;
                        break;
                    }
                }
                if(!continueFind){
                }else{
                    //增加页码
                    examPaperPageNo++;
                    if(examPaperTotalPageCount == null || examPaperPageNo > examPaperTotalPageCount){
                        //已经找完所有页码,还是没找到,不再继续查找
                        continueFind = false;
                    }
                }
            }else{
                continueFind = false;
            }
        })
        //频繁请求可能导致出错,所以等一等
        await waitingTime(1000);
    }
    return examPaperId;
}
//做专项答题
function doExamPaper(){
    return new Promise(function(resolve) {
        //查找有没有没做过的专项答题,有则返回ID
        findExamPaper().then(examPaperId => {
            if(examPaperId != null){
                console.log("正在做专项答题")
                let newPage = GM_openInTab(ExamPaperUrl.replace("{id}",examPaperId),{active: true,insert: true, setParent :true});
                let doing = setInterval(function() {
                    if(newPage.closed) {
                        clearInterval(doing);//停止定时器
                        resolve('done');
                    }
                }, 1000);
            }else{
                console.log("没有找到未完成的专项答题,跳过")
                resolve('noTest');
            }
        });
    })
}
//获取每周答题列表
function getExamWeekly(){
    return new Promise(function(resolve) {
        $.ajax({
            type: "GET",
            url: ExamWeeklyListUrl.replace("{pageNo}",examWeeklyPageNo),
            xhrFields: {
                withCredentials: true //如果没有这个请求失败
            },
            dataType: "json",
            success: function(data){
                console.log(data)
                data = data.data_str;
                if(data.indexOf("-") != -1){
                    data = data.replace(/-/g, "+");
                }
                if(data.indexOf("_") != -1){
                    data = data.replace(/_/g, "/");
                }
                data = decodeURIComponent(escape(window.atob(data)));
                //JSON格式化
                data = JSON.parse(data);
                resolve(data);
            },
            error: function(){
                resolve(new Array());
            }
        });
    })
}
//查询每周答题列表看看还有没有没做过的,有则返回id
async function findExamWeekly(){
    var continueFind = true;
    var examWeeklyId = null;
    console.log("正在寻找未完成的每周答题")
    while(continueFind){
        await getExamWeekly().then(data => {
            if(data){
                if(examWeeklyTotalPageCount == null){
                    //如果总页码没初始化,则初始化
                    examWeeklyTotalPageCount = data.totalPageCount;
                }
                for(let i = 0; i< data.list.length; i++){
                    let examWeeks = data.list.practices;//获取每周的测试列表
                    for(let j = 0; j < examWeeks.length; j++){
                        //遍历查询有没有没做过的
                        if(examWeeks[j].status != 2){//status: 1为"开始答题" , 2为"重新答题"
                            //如果不是"重新答题",则可以做
                            examWeeklyId = examWeeks[j].id;
                            continueFind = false;
                            break;
                        }
                    }
                    if(!continueFind){
                        //如果已经找到了,则退出循环
                        break;
                    }
                }
                if(!continueFind){
                }else{
                    //增加页码
                    examWeeklyPageNo++;
                    if(examWeeklyTotalPageCount == null || examWeeklyPageNo > examWeeklyTotalPageCount){
                        //已经找完所有页码,还是没找到,不再继续查找
                        continueFind = false;
                    }
                }
            }else{
                continueFind = false;
            }
        })
        //频繁请求可能导致出错,所以等一等
        await waitingTime(1000);
    }
    return examWeeklyId;
}
//做每周答题
function doExamWeekly(){
    return new Promise(function(resolve) {
        //查找有没有没做过的每周测试,有则返回ID
        //examWeeklyId = 147;//测试题目
        findExamWeekly().then(examWeeklyId => {
            if(examWeeklyId != null){
                console.log("正在做每周答题")
                let newPage = GM_openInTab(ExamWeeklyUrl.replace("{id}",examWeeklyId),{active: true,insert: true, setParent :true});
                let doing = setInterval(function() {
                    if(newPage.closed) {
                        clearInterval(doing);//停止定时器
                        resolve('done');
                    }
                }, 1000);
            }else{
                console.log("没有找到未完成的每周答题,跳过")
                resolve('noTest');
            }
        });
    })
}
//获取答题按钮
function getNextButton(){
    return new Promise(function(resolve) {
        let nextInterVal = setInterval(()=>{
            let nextAll=document.querySelectorAll(".ant-btn");
            let next = nextAll[0];
            if(nextAll.length==2) {
                next = nextAll[1];
            }
            if(next.textContent){
                clearInterval(nextInterVal);//停止定时器
                resolve(next);
            }
        },800);
    })
}
//暂停锁
function doingPause(){
    return new Promise(function(resolve) {
        let doing = setInterval(function() {
            if(!pause) {
                clearInterval(doing);//停止定时器
                resolve('done');
            }else{
                console.log("等待")
            }
        }, 500);
    })
}

//滑动验证
async function slideVerification(){
    let xc_window = document.querySelector("#nc_1_n1t");
    if(xc_window){
        pause = true;//先暂停
        hasSlideVerification = true;
        //先关掉
        document.querySelector("#J-risk-close-btn").click();
        await waitingTime(1000);
        //回到上一题
        document.querySelector(".pre-btn").click();
        await waitingTime(1000);
        //回到这题
        document.querySelector(".next-btn").click();
        await waitingTime(1000);
        //提交答题
        document.querySelector(".next-btn").click();
        await waitingTime(1000);

        console.log("滑动验证");
        let btn = document.querySelector(".btn_slide");
        let mousedown = document.createEvent("MouseEvents");
        let rect = btn.getBoundingClientRect();
        let x = rect.x||rect.left;
        let y = rect.y||rect.top;
        let w = 300;
        //点击滑块
        try{
            mousedown.initMouseEvent("mousedown",true,true,unsafeWindow,0,x, y, x, y,false,false,false,false,0,null);
        }catch(e){
            mousedown = new MouseEvent("mousedown",{
                bubbles: true,
                cancelable: true,
                view: unsafeWindow,//控制台用window,脚本要用unsafeWindow
                detail: 0,
                screenX: 0,
                screenY: 0,
                clientX: 0,
                clientY: 0,
                ctrlKey: false,
                altKey: false,
                shiftKey: false,
                metaKey: false,
                button:0,
                relatedTarget: null,
            });
        }
        console.log("点击滑块");
        btn.dispatchEvent(mousedown);

        let dx = 0;
        let dy = 0;

        //滑动滑块
        let intervaltimer = setInterval(function(){
            var mousemove = document.createEvent("MouseEvents");
            var _x = x + dx;
            var _y = y + dy;
            //滑动滑块
            try{
                mousemove.initMouseEvent("mousemove",true,true,unsafeWindow,0,_x, _y, _x, _y,false,false,false,false,0,null);
            }catch(e){
                mousemove = new MouseEvent("mousemove",{
                    bubbles: true,
                    cancelable: true,
                    view: unsafeWindow,//控制台用window,脚本要用unsafeWindow
                    detail: 0,
                    screenX: _x,
                    screenY: _y,
                    clientX: _x,
                    clientY: _y,
                    ctrlKey: false,
                    altKey: false,
                    shiftKey: false,
                    metaKey: false,
                    button:0,
                    relatedTarget: null,
                });
            }
            console.log("滑动滑块");
            btn.dispatchEvent(mousemove);

            if(_x - x >= w){
                clearInterval(intervaltimer);
                var mouseup = document.createEvent("MouseEvents");
                try{
                    mouseup.initMouseEvent("mouseup",true,true,unsafeWindow,0,_x, _y, _x, _y,false,false,false,false,0,null);
                }catch(e){
                    mouseup = new MouseEvent("mouseup",{
                        bubbles: true,
                        cancelable: true,
                        view: unsafeWindow,//控制台用window,脚本要用unsafeWindow
                        detail: 0,
                        screenX: _x,
                        screenY: _y,
                        clientX: _x,
                        clientY: _y,
                        ctrlKey: false,
                        altKey: false,
                        shiftKey: false,
                        metaKey: false,
                        button:0,
                        relatedTarget: null,
                    });
                }
                console.log("松开滑块");
                btn.dispatchEvent(mouseup);
                pause = false;
            }
            else{
                dx += parseInt(Math.random()*(209-199)+199)/33;
                console.log(x,y,_x,_y,dx);
            }
        }, 30);
    }
}

//答题过程(整合)
async function doingExam() {
    let nextButton = null;
    let qNum = 0;//题号,第一题从0开始算
    let shouldSaveAnswer = false;
    while(true){
        //先等等再开始做题
        await waitingTime(2500);
        await doingPause();
        nextButton = await getNextButton();
        if(nextButton.textContent == "再练一次" || nextButton.textContent == "再来一组" || nextButton.textContent == "查看解析"){
            break;
        }
        try {
            document.querySelector(".tips").click();
        } catch(e) {
            console.log(e);
        }
        //所有提示
        var allTips=document.querySelectorAll("font");
        await waitingTime(1500);
        //选项按钮
        var allbuttons=document.querySelectorAll(".q-answer");
        //获取所有填空
        var blanks=document.querySelectorAll("input[type=text]");
        try {
            //获取问题类型
            var questionType= document.querySelector(".q-header").textContent;
            questionType=questionType.substr(0,3)
        } catch (e) {
        }
        var results = [];
        switch(questionType) {
            case"填空题":{
                //第几个填空
                var inputBubblesEvent= new Event('input',{bubbles:true});
                if(blanks.length>1) {//如果有多个填空
                    if(allTips.length == 0){//如果没有提示,先获取看看有没有答案
                        try{//尝试点击视频播放按钮,播不播都没关系
                            document.getElementsByClassName("outter")[0].click();
                        }catch(e){}
                        //生成秘钥
                        let key = getKey();
                        //尝试获取答案
                        let answerData = await getAnswer(key);
                        if(answerData.status == 0 || answerData == "error"){//没有答案
                            for(let i=0;i< blanks.length;i++) {//没答案,随便填点东西
                                blanks.setAttribute("value",i);
                                //必须要阻止事件,不然无效
                                blanks.dispatchEvent(inputBubblesEvent);
                            }
                            shouldSaveAnswer = true;//答完保存答案
                        }else{//获取到了答案
                            //格式化
                            answerData = JSON.parse(answerData.data.txt_content);
                            answerData = answerData[0].content;
                            //因为有多个空,所以有多个答案,先切割
                            answerData = answerData.split(";");
                            for(let i=0;i< answerData.length;i++) {//将答案填入
                                blanks.setAttribute("value",answerData);
                                blanks.dispatchEvent(inputBubblesEvent);
                            }
                        }
                    }else if(allTips.length == blanks.length) {
                        //如果填空数量和提示数量一致
                        for(let i=0;i< allTips.length;i++) {
                            //将答案填写到对应的空中
                            let answer=allTips.textContent;
                            if(answer && answer.length>0) {
                                blanks.setAttribute("value",answer);
                                blanks.dispatchEvent(inputBubblesEvent);
                            }else{
                                //发生了错误,只好随便填一下
                                blanks.setAttribute("value",i);
                                blanks.dispatchEvent(inputBubblesEvent);
                            }
                        }
                    }else if(allTips.length > blanks.length) {
                        //若提示数量比填空的数量多
                        //直接将所有答案整合填进去
                        let answer = "";
                        for(let i = 0; i < allTips.length; allTips++){
                            answer += allTips.textContent();
                        }
                        for(let j = 0;j<blanks.length; j++) {
                            blanks[j].setAttribute("value",answer);
                            blanks[j].dispatchEvent(inputBubblesEvent);
                        }
                    }else{
                        //一般不会跑到这,如果到这了输出一下,表示惊讶
                        console.log("居然跑到了这里")
                    }
                }else if(blanks.length == 1) {//只有一个空,直接把所有tips合并。
                    let answer = "";
                    if(allTips.length != 0 ){//如果有提示
                        for(let i = 0; i< allTips.length; i++){
                            answer += allTips.textContent;
                        }
                    }else{
                        try{//尝试点击视频播放按钮,不过播不播都没关系
                            document.querySelector('video').play();
                        }catch(e){}
                        let key = getKey();
                        let answerData = await getAnswer(key);
                        if(answerData.status == 0 || answerData == "error"){
                            //没有获取到答案
                            answer = "不知道";
                            //没有其他人做过这道视频题,所以需要答完保存答案,这样其他人遇到就能做对
                            shouldSaveAnswer = true;
                        }else{
                            //有答案
                            answerData = JSON.parse(answerData.data.txt_content);
                            answer = answerData[0].content;
                        }
                    }
                    blanks[0].setAttribute("value",answer);
                    blanks[0].dispatchEvent(inputBubblesEvent);
                    break;
                }
                else
                {
                    //怕有没空白的情况。
                }
                break;
            }
            case "多选题":{
                results = [];
                let hasButton = false;
                for(let i=0;i< allTips.length;i++) {
                    let tip=allTips;
                    let answer = tip.textContent;
                    if(answer && answer.length>0) {
                        for(let j = 0; j < allbuttons.length; j++) {
                            //获取按钮
                            let selectButton = allbuttons[j];
                            //获取按钮的上的答案
                            let buttonAnswer = selectButton.textContent;
                            if(buttonAnswer == answer || buttonAnswer.indexOf(answer) != -1 || answer.indexOf(buttonAnswer) != -1){
                                hasButton = true;
                                if (!$(selectButton).hasClass("chosen")){
                                    selectButton.click();
                                }
                                break;
                            }
                        }
                    }
                }
                if(!hasButton){
                    //没找到按钮,随便选一个
                    allbuttons[0].click();
                }
                break;
            }
            case "单选题":{
                let results = [];
                let answer = "";
                for(let i=0;i< allTips.length;i++) {
                    answer += allTips.textContent;
                }
                if(answer && answer.length>0) {
                    let answerButton = null;
                    for(let i=0; i < allbuttons.length; i++) {
                        let buttonAnswer= allbuttons.textContent;
                        //对比答案,如果完全一样,那么肯定是
                        if(buttonAnswer == answer) {
                            answerButton = allbuttons;
                            break;
                        }else if(buttonAnswer.indexOf(answer) != -1 || answer.indexOf(buttonAnswer) != -1){
                            //如果只是包含答案的部分内容,则可能是,还需要继续找
                            answerButton = allbuttons;
                            continue;
                        }
                    }
                    if(answerButton){
                        answerButton.click();
                    }else{
                        //没找到按钮,随便选一个
                        allbuttons[0].click();
                    }
                } else {
                    //没答案,随便选一个
                    allbuttons[0].click();
                }
                break;
            }
            default:
                break;
        }
        qNum++;
        nextButton = await getNextButton();
        if(nextButton.textContent != "再练一次" && nextButton.textContent != "再来一组" && nextButton.textContent != "查看解析"){
            nextButton.click();
            if(shouldSaveAnswer){//如果应该保存答案
                let key = getKey();//获取key
                let answerTemp = document.getElementsByClassName("answer")[0].innerText;
                let reg = new RegExp(' ', "g")
                let answer = "";
                try{//从字符串中拿出答案
                    answer = answerTemp.split(":")[1];
                    answer = answer.replace(reg,";");
                }catch(e){
                    answer = answerTemp;
                }
                await saveAnswer(key,answer);
                shouldSaveAnswer = false;
            }
        } else {
            //已经做完,跳出循环
            break;
        }
    }
    window.close();
}
//获取关键字
function getKey(){
    //获取题目的文本内容
    let key = document.getElementsByClassName("q-body")[0].innerText;
    //外部引用md5加密
    key = md5(key);
    console.log(key)
    return key;
}
//保存答案
function saveAnswer(key,value){
    return new Promise(function(resolve) {
        value = [{"title":key,"content":value}];
        value = JSON.stringify(value);
        $.ajax({
            type: "POST",
            url: AnswerSaveUrl,
            data:{
                txt_name: key,
                txt_content:value,
                password:"",
                v_id: ""
            },
            dataType: "json",
            success: function(data){
                resolve(data);
            },
            error: function(){
                resolve("error");
            }
        });
    })
}
//获取答案
function getAnswer(key){
    return new Promise(function(resolve) {
        $.ajax({
            type: "POST",
            url: AnswerDetailUrl,
            data:{
                txt_name: key,
                password:""
            },
            dataType: "json",
            success: function(data){
                resolve(data);
            },
            error: function(){
                resolve("error");
            }
        });
    })
}
//等待时间工具函数
function waitingTime(time){
    if(!Number.isInteger(time)){
        time = 1000;
    }
    return new Promise(resolve => {
        setTimeout(function(){
            resolve('done');
        },time);
    });
}
//查询今日完成情况
function getToday(){
    return new Promise(function(resolve) {
        $.ajax({
            type: "GET",
            url: SearchSocreUrl,
            xhrFields: {
                withCredentials: true //如果没有这个请求失败
            },
            dataType: "json",
            success: function(temp){
                //console.log(temp);
                resolve(temp.data.taskProgress);
            },
            error: function(){
                resolve(new Array());
            }
        });
    })
}
//初始化配置
function initSetting(){
    if(GM_getValue("studyVersion")){
        try{
            let settingTemp = JSON.parse(GM_getValue('studySetting'));
            if(settingTemp != null){
                settings = settingTemp;
            }else{
                settings = [true,true,true,true,false];
            }
        }catch(e){
            //没有则直接初始化
            settings = [true,true,true,true,false];
        }
    }else{
        GM_setValue("studyVersion","2.3")
        settings = [true,true,true,true,false];
        GM_setValue('studySetting',JSON.stringify(settings));
    }
}
//创建“手动答题”按钮
function createManualButton(){
    let title = document.getElementsByClassName("title")[0];
    let manualButton = document.createElement("button");
    manualButton.setAttribute("id","manualButton");
    manualButton.innerText = "关闭自动答题";
    manualButton.className = "egg_auto_btn";
    //添加事件监听
    try{// Chrome、FireFox、Opera、Safari、IE9.0及其以上版本
        manualButton.addEventListener("click",clickManualButton,false);
    }catch(e){
        try{// IE8.0及其以下版本
            manualButton.attachEvent('onclick',clickManualButton);
        }catch(e){// 早期浏览器
            console.log("不学习何以强国error: 手动答题按钮绑定事件失败")
        }
    }
    //插入节点
    title.parentNode.insertBefore(manualButton, title.nextSibling);
}
//点击手动学习按钮
function clickManualButton(){
    let manualButton = document.getElementById("manualButton");
    if(manualButton.innerText == "关闭自动答题"){
        manualButton.innerText = "开启自动答题";
        manualButton.className = "egg_manual_btn";
        pause = true;
    }else{
        manualButton.innerText = "关闭自动答题";
        manualButton.className = "egg_auto_btn";
        pause = false;
    }
}
//创建“开始学习”按钮和配置
function createStartButton(){
    let base = document.createElement("div");
    var baseInfo="";
    baseInfo += "<form id=\"settingData\" class=\"egg_menu\" action=\"\" target=\"_blank\" onsubmit=\"return false\"><div class=\"egg_setting_box\"><div class=\"egg_setting_item\"><label>新闻<\/label><input class=\"egg_setting_switch\" type=\"checkbox\" name=\"0\" " + (settings[0] ? 'checked': '') +" \/><\/div><div class=\"egg_setting_item\"> <label>视频<\/label> <input class=\"egg_setting_switch\" type=\"checkbox\" name=\"1\" " + (settings[1] ? 'checked': '') +" \/><\/div><div class=\"egg_setting_item\"> <label>每日答题<\/label> <input class=\"egg_setting_switch\" type=\"checkbox\" name=\"3\" "+ (settings[3] ? 'checked': '') +" \/><\/div> <hr \/> <div title='Tip:开始学习后,隐藏相关页面和提示(不隐藏答题中的关闭自动答题按钮)' class=\"egg_setting_item\"> <label>运行隐藏<\/label> <input class=\"egg_setting_switch\" type=\"checkbox\" name=\"4\""+ (settings[4] ? 'checked' : '' ) + "/></div><div id='saveSetting' style=\"color:#d90609;border: solid 2px;justify-content:center;align-items: center;border-radius:20px;cursor: pointer;margin: 12px 0;font-size:14px;\" class=\"egg_setting_item\"><label style=\"cursor: pointer;\">保存配置<\/label><\/div><a style=\"text-decoration: none;\" title=\"视频不自动播放?点此查看解决办法\" target=\"blank\" href=\"https://docs.qq.com/doc/DZllGcGlJUG1qT3Vx\"><div style=\"color:#5F5F5F;font-size:14px; \"class=\"egg_setting_item\"><label style=\"cursor:pointer;\">视频不自动播放?<\/label><\/div><\/a><\/div><\/form>";
    base.innerHTML = baseInfo;
    let body = document.getElementsByTagName("body")[0];
    body.append(base)
    let startButton = document.createElement("button");
    startButton.setAttribute("id","startButton");

    startButton.innerText = "开始学习";
    startButton.className = "egg_study_btn egg_menu";

    //获取上一次学习完成的日期
    //     let finishDate = GM_getValue('finishDate');
    //     if(finishDate && finishDate == new Date().getDate()){
    //         //如果上一次完成的日期就是今天,那么不用学习了
    //         startButton.innerText = "已完成";
    //         startButton.className = "egg_study_btn egg_menu";
    //         startButton.style.color = "#c7c7c7";
    //         startButton.style.cursor = "default";
    //         startButton.setAttribute("disabled",true);
    //     }else{
    //         //否则需要学习
    //         startButton.innerText = "开始学习";
    //         startButton.className = "egg_study_btn egg_menu";
    //     }


    //添加事件监听
    try{// Chrome、FireFox、Opera、Safari、IE9.0及其以上版本
        startButton.addEventListener("click",start,false);
    }catch(e){
        try{// IE8.0及其以下版本
            startButton.attachEvent('onclick',start);
        }catch(e){// 早期浏览器
            console.log("不学习何以强国error: 开始学习按钮绑定事件失败")
        }
    }
    //插入节点
    body.append(startButton)
}

//显示提示
function showTip(title,type = "success",time = 1000){
    let tipBox = document.createElement("div");
    let baseInfo="";
    if(type == null){
        type = "success";
    }
    baseInfo += "<div class='egg_tip egg_tip_" + type + "'>" + title + "</div>";
    tipBox.innerHTML = baseInfo;
    let body = document.getElementsByTagName("body")[0];
    body.append(tipBox);
    if(time == null){
        time = 1000;
    }
    //经过一定时间后,取消显示提示
    setTimeout(function(){
        tipBox.remove();
    },time);
}

//保存配置
function saveSetting(){
    let form = document.getElementById("settingData");
    let formData = new FormData(form);
    settings[0] = (formData.get('0') != null);//文章
    settings[1] = (formData.get('1') != null);//视频
    settings[3] = (formData.get('3') != null);//每日答题
    console.log("保存配置")
    GM_setValue('studySetting',JSON.stringify(settings));
    showTip("保存成功");
}


//是否显示目录
function showMenu(isShow = true){
    let items = document.getElementsByClassName("egg_menu");
    for(let i = 0;i < items.length; i++){
        items.style.display = isShow ? "block":"none";
    }
}

//获取二维码的ID
function getQRcodeId(){
    return new Promise(function(resolve) {
        $.ajax({
            type: "GET",
            url: GetQRcodeUrl,
            dataType: "json",
            success: function(temp){
                console.log(temp);
                if(temp.success){
                    resolve(temp.result);
                }else{
                    resolve(null);
                }
            },
            error: function(){
                resolve(null);
            }
        });
    })
}

//获取扫码登录状态
function getScanQRcodeStatus(qrcodeId){
    return new Promise(function(resolve) {
        $.ajax({
            type: "POST",
            url: LisenScanQRcodeUrl,
            xhrFields: {
                withCredentials: true //如果没有这个请求失败
            },
            dataType: "json",
            data:{
                qrCode:qrcodeId,
                goto:"https://oa.xuexi.cn",
                pdmToken:""
            },
            success: function(temp){
                resolve(temp);
            },
            error: function(){
                resolve(false);
            }
        });
    })
}

//初始化登录二维码
async function initQRcode(){
    let loggedBox = document.querySelectorAll("a[class='logged-link']")[0];
    if(loggedBox && loggedBox.innerText){
        //已经登录了
    }else{
        //没登录
        let qrcodeId = await getQRcodeId();
        if(qrcodeId){
            //生成登录二维码url
            //let url = encodeURIComponent("https://login.xuexi.cn/login/qrcommit?showmenu=false&code=" + qrcodeId + "&appId=dingoankubyrfkttorhpou");
            //let generateQRcodeUrl = GenerateQRcodeUrl.replace("{url}",url).replace("{time}",new Date().getTime());
            let qrcodeBox = document.createElement("div");
            qrcodeBox.setAttribute("id","qglogin");
            //console.log(generateQRcodeUrl)
            qrcodeBox.innerHTML = "<iframe class='egg_qrcode_box' src='https://pc.xuexi.cn/points/login.html?ref=https://www.xuexi.cn/'></iframe>";//"<div class=\"egg_qrcode_box\"><div class=\"egg_qrcode_title\">扫码登录</div><img src=\""+ generateQRcodeUrl +"\"></div>"
            let body = document.getElementsByTagName("body")[0];
            body.setAttribute("id","body-body");
            body.append(qrcodeBox);

            let lastNum = 0;
            //监听有没有扫码登录
            setInterval(function(){
                let nextNum = GM_getValue("loginNum");
                if(lastNum != 0 && lastNum == nextNum){
                    location.reload();
                }
                lastNum = nextNum;
            },1200);
        }else{
            alert("获取登录二维码失败,请手动打开登录页面登录")
        }
    }
}

//开始
async function start(){
    console.log("初始化...");
    let loggedBox = document.querySelectorAll("a[class='logged-link']")[0];
    console.log("检查是否登录...")
    if(loggedBox && loggedBox.innerText){
        let startButton = document.getElementById("startButton");
        startButton.innerText = "正在学习";
        startButton.style.cursor = "default";
        startButton.setAttribute("disabled",true);
        if(settings[4]){
            showMenu(false);
        }
        let taskProgress = null;
        let continueToDo = true;
        let tasks = [false,false,false]
        while(continueToDo){
            //查询今天还有什么任务没做完
            console.log("检查今天还有什么任务没做完")
            taskProgress = await getToday();
            console.log(taskProgress)
            if(taskProgress != null){
                console.log("开始学习")

                //检查文章
                if(settings[0] && taskProgress[0].currentScore != taskProgress[0].dayMaxScore){
                    tasks[0] = false;//只要还有要做的,就当做没完成
                    newsNum = taskProgress[0].dayMaxScore - taskProgress[0].currentScore;//还需要看多少个新闻
                    console.log("1.看新闻");
                    await readNews();
                }else{
                    tasks[0] = true;
                }

                //检查视频
                let temp = parseInt(taskProgress[1].dayMaxScore - taskProgress[1].currentScore);
                if(settings[1] && temp != 0){
                    tasks[1] = false;//只要还有要做的,就当做没完成
                    videoNum = temp;//还需要看多少个视频
                    console.log("2.看视频");
                    await watchVideo();
                }else{
                    tasks[1] = true;
                }

                //检查每日答题
                if(settings[3] && taskProgress[3].currentScore != taskProgress[3].dayMaxScore){
                    tasks[2] = false;//只要还有要做的,就当做没完成
                    console.log("3.做每日答题");
                    await doExamPractice();
                }else{
                    tasks[2] = true;
                }

                if(tasks[0] && tasks[1] && tasks[2]){
                    //如果检查都做完了,就不用继续了
                    continueToDo = false;
                }
            }else{
                alert("发生意外错误")
                continueToDo = false;
            }
            console.log("continueToDo : " + continueToDo)
        }
        console.log("已完成")
        startButton.innerText = "已完成";
        startButton.style.color = "#c7c7c7";
        if(settings[4]){
            showMenu()
        }
        //设置完成日期
        //         GM_setValue('finishDate',new Date().getDate());
        //         setTimeout(function(){
        //             location.reload();
        //         },5000);//5秒后刷新页面
    }else{
        //提醒登录
        alert("请先登录");
    }
    return false;
}[/code]
最佳答案
2024-1-22 16:31:22
lxg1434334642 发表于 2024-01-22 16:31
请进行代码修复和调整


根据您的需求,我对代码进行了修复和调整。请查看下面的代码:

```javascript
// ==UserScript==
// @name         学习强国脚本

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-1-22 16:28:31 | 显示全部楼层
该代码存在一些问题,导致无法运行和初始化。首先,它使用了一些未定义的变量,例如"news"、"videos"和"settings"。此外,还有一些语法错误,例如未关闭函数括号和结束分号缺失。

如果您有相关的初始化和功能需求,我可以帮助您进行修复和调整代码。请提供更多详细信息,以便我能够提供更准确的帮助。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-1-22 16:31:16 | 显示全部楼层
FishC 发表于 2024-1-22 16:28
该代码存在一些问题,导致无法运行和初始化。首先,它使用了一些未定义的变量,例如"news"、"videos"和"set ...

请进行代码修复和调整
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-1-22 16:31:22 | 显示全部楼层    本楼为最佳答案   
lxg1434334642 发表于 2024-01-22 16:31
请进行代码修复和调整


根据您的需求,我对代码进行了修复和调整。请查看下面的代码:

```javascript
// ==UserScript==
// @name         学习强国脚本

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-1-22 16:32:27 | 显示全部楼层
FishC 发表于 2024-1-22 16:31
根据您的需求,我对代码进行了修复和调整。请查看下面的代码:

```javascript

请回复完整的代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-1-22 16:32:45 | 显示全部楼层


非常抱歉,由于原始代码太长且存在许多问题,无法在此回复完整的修复和调整后的代码。建议您对原始代码进行以下

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 23:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表