南宁理工学院自动看网课

南宁理工学院自动看网课,自动识别验证码。

// ==UserScript==
// @name         南宁理工学院自动看网课
// @version      3.1
// @description  南宁理工学院自动看网课,自动识别验证码。
// @author       smallbolt
// @match        *://zxshixun.bwgl.cn/user/node*
// @match        *://gyxy.bwgl.cn/user/node*
// @match        *://mooc.bwgl.cn/user/node*
// @iconURL     https://img0.baidu.com/it/u=1805021814,1772967375&fm=253&fmt=auto&app=138&f=JPEG?w=707&h=500
// @grant        GM_xmlhttpRequest
// @license    	 MIT
// @namespace  	 https://greasyfork.org/zh-CN/users/1393388
// @connect      121.43.154.94
// @connect      121.43.154.94:6688
// ==/UserScript==












let current = 0;
let checkCaptchaTimer = null;
let timerCnt = 0;
let layuiLayerContent = null;
let links = null;
let videoElement = null;
let version = "专业升级版"
let containerTextElement = null;

function getCurrent() {
    links = $('a[target="_self"]');
    links.each((index, item) => {
        if ($(item).hasClass("on")) {
            return current = index
        }
    });
}
// 下一个
async function playNext() {
    clearInterval(checkCaptchaTimer);
    if (current === links.length - 1) {
        addText("已看完!")
    } else {
        addText("播放下个视频...")
        await pause(3)
        links[current + 1].click();
    }
}


async function inputCaptcha() {
    if (layuiLayerContent.length && layuiLayerContent.is(':visible')) {
        addText("验证码出现,准备填写验证码...出现undefined是未被授权的意思");
        await pause(2, 5)

        
        let imgs = layuiLayerContent.find("img")
        let img = imgs[0].style.opacity === '0' ? imgs[1] : imgs[0]

     
        let canvas = document.createElement("canvas");
        let ctx = canvas.getContext("2d");
        canvas.width = img.width;
        canvas.height = img.height;
        ctx.drawImage(img, 0, 0, img.width, img.height);
        let code = canvas.toDataURL("image/png").split("base64,")[1];

   
        let ans = await getCode(code)

     
        let inputs = layuiLayerContent.find("input")
        let input = inputs[0].style.display === 'none' ? inputs[1] : inputs[0]
        $(input).mousedown()
        input.value = ans


        await pause(2, 5)
        const playButton = $('.layui-layer-btn0');
        if (playButton.length) {
            playButton.click();
            checkCaptchaTimer = setInterval(playVideo, 1000);
            addText("已自动开始播放!");
        } else {
            addText("未找到开始播放,尝试刷新页面...");
            location.reload();  
        }
    }
}


function getCode(code) {
    return new Promise((resolve, reject) => {
 let userName = localStorage.getItem('userName')

        const datas = {

	   "userName": userName,   //记录用户ID用于区分   

            "img_base64": String(code),
        }
        GM_xmlhttpRequest({
            method: "POST",
            url: "http://121.43.154.94:6688/api/ocr/image",
            data: JSON.stringify(datas),
            headers: {
                "Content-Type": "application/json",
            },
            responseType: "json",
            onload: function (response) {
                if (response.status == 200) {
                    if (response.responseText.indexOf("未授权!!!!!!!!") != -1)
                        addText(response.response["msg"]);
                    try {
                        var result = response.response["result"];
                        addText("识别结果:" + result);
                        return resolve(result);
                    } catch (e) {
                        if (response.responseText.indexOf("接口请求频率过高") != -1)
                            addText(response.responseText);
                    }
                } else {
                    addText("未授权!!!!!!!!未授权!!!!!!!!未授权!!!!!!!!");
                }
            }
        });
    });
}


async function playVideo() {
    timerCnt++;
    if (timerCnt % 5 === 0) {
        addText("等待加载,已加载:" + timerCnt + "秒")
    }
    if (timerCnt > 20) {
        addText("刷新页面")
        location.reload();
        return
    }
    if (!videoElement) {
        if (links[current].title && links[current].title === "考试") {
            addText("课程已看完,自动停止!")
            clearInterval(checkCaptchaTimer)
        } else {
            getVideoElement();
        }
        return
    }
    
    layuiLayerContent = $('.layui-layer-content');
    if (layuiLayerContent.length > 0) {
        clearInterval(checkCaptchaTimer);
        await inputCaptcha()
        return;
    }

   
    // if (!videoElement) return;
    if (videoElement.paused) {
        videoElement.play();
        if (videoElement.readyState === 4) {
            const message = containerTextElement.text().includes("视频加载完成")
                ? "请将浏览器置于前台运行。(若学时会增加可忽略)" : "视频加载完成,准备播放";
            addText(message);
        }
    } else {
        timerCnt = 0;
    }
}


const getVideoElement = () => {
    videoElement = document.querySelector("video");
    videoElement.muted = true;
    videoElement.playbackRate = 1.0;
    videoElement.volume = 0;
    videoElement.onended = async function () {
        await playNext();
    };
}


const addContainer = () => {
    const container = $('<container></container>')
    container.addClass('popup');

    const header = $("<div></div>")
    header.addClass('container-header')
    header.text("南宁理工学院自动看网课  3r 可以就打个赏微信smallbolt")
    container.append(header)

 
    header.on("mousedown", function (event) {
 
        let shiftX = event.clientX - header.offset().left;
        let shiftY = event.clientY - header.offset().top;
      
        function onMouseMove(event) {
            container.css({
                left: event.pageX - shiftX + 'px',
                top: event.pageY - shiftY + 'px'
            })
        }
  
        function onMouseUp() {
            $(document).off('mousemove', onMouseMove);
            $(document).off('mouseup', onMouseUp);
        }
        $(document).on('mousemove', onMouseMove);
        $(document).on('mouseup', onMouseUp);
    })

    const hr = $("<hr>")
    container.append(hr)

    containerTextElement = $("<div></div>")
    containerTextElement.addClass('container-text')
    container.append(containerTextElement)
    addText("<h4>提示1</h4>:可以就打个赏微信smallbolt")
    addText("<h4>提示2</h4>:服务器维护需要钱</b>。")
    addText("<h4>提示3</h4>:可以就打个赏微信smallbolt<br>")
    addText("启动成功...")
    $("body").append(container)
}


const addStyle = () => {
    const style = $("<style></style>")
    style.prop('type', 'text/css')
    style.html(
        `
.popup {
    position: fixed;
    top: 50px;
    left: 150px;
    width: 520px;
    font: 14px Menlo, Monaco, Consolas, "Courier New", monospace;
    z-index: 9999999999999999999999;
    background-color: #fff;
    box-shadow: 0 0 5px 1px rgba(0, 0, 0, .3);
    padding: 10px;
    border-radius: 5px;
}
.popup h4{
    display: inline-block;
}
.popup b{
    color: red;
}

.container-header {
    height: 30px;
    cursor: move;
    line-height: 30px;
}

.container-text {
    margin-top: 10px;
    max-height: 200px;
    min-height: 30px;
    overflow: auto;
}
        `
    )
    $('body').append(style);
}



const addText = text => {
    containerTextElement.append(text + "<br>")
    containerTextElement.scrollTop(containerTextElement[0].scrollHeight)
}


function pause(start, end = undefined) {
    let delay = start;
    if (end) {
        delay = Math.floor(Math.random() * (end - start)) + start;
        addText(`等待 ${delay} 秒后继续...`);
    }
    return new Promise(resolve => {
        setTimeout(() => {
            resolve();
        }, delay * 1000); // 将秒转换为毫秒
    });
}



const init = async () => {
    addContainer() // 容器
    addStyle() // 容器样式
    getCurrent() // 获取当前页面所属节次
    // 添加文本
    addText("初始化完成,可以解放双手了<br>")
    await pause(3, 5)
}



(function () {
    'use strict';
  
    $(document).ready(async function () {
        await init()
        checkCaptchaTimer = setInterval(playVideo, 1000);
    });
})();