⭐超星学习通作业/考试复制粘贴、题目选项一键复制、答题框粘贴、自动保存⭐

学习通复制粘贴、题目选项一键复制、答题框粘贴、自动保存

// ==UserScript==
// @name         ⭐超星学习通作业/考试复制粘贴、题目选项一键复制、答题框粘贴、自动保存⭐
// @namespace    http://tampermonkey.net/
// @version      0.7
// @description  学习通复制粘贴、题目选项一键复制、答题框粘贴、自动保存
// @author       载月明归
// @match        *://mooc1.chaoxing.com/exam-ans/exam/test/*
// @match        *://mooc1.chaoxing.com/exam-ans/mooc2/exam/preview*
// @match        *://mooc1.chaoxing.com/mooc2/work/dowork*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=chaoxing.com
// @require      https://code.jquery.com/jquery-3.1.1.min.js
// @grant        GM_registerMenuCommand
// @grant        GM_setValue
// @grant        GM_getValue
// @license      GPL
// ==/UserScript==

(function() {
    //     'use strict';
    var $ = $ || window.$;

    // 去除禁止选中和复制
    setTimeout(()=>{
        $("body").removeAttr("onselectstart");
        $("html").css("user-select", "unset");
    },1000);

    // 更改背景颜色
    $(".whiteDiv").css("background","white");

    // 判断新老学习通
    if(window.location.href.includes("newMooc=true") || window.location.href.includes("work")){
        let answerOptObj;
        let title = "";
        let optionStr = "";
        let task1 = false;
        let task2 = false;
        let copyModeName;
        let copyModeIndex = GM_getValue("copyModeIndex") == undefined ? 0 : GM_getValue("copyModeIndex");

        if(copyModeIndex == 0){
            copyModeName = "使用copy按钮复制方式";
        }
        else if(copyModeIndex == 1){
            copyModeName = "使用点击题目复制方式";
        }

        let id = GM_registerMenuCommand(copyModeName, function(){
            copyModeIndex = copyModeIndex == 0 ? 1 : 0;
            GM_setValue("copyModeIndex", copyModeIndex);
            location.reload(false);
        });

        let interval1=setInterval(function(){

            if($(".mark_name").length > 0 && !task1){
                $(".mark_name:eq(1)").css("background","white");
                $("h3.mark_name").css("cursor", "default");

                if(copyModeIndex == 0){
                    // 拼接选项和答案 点击题目进行复制
                    $("h3.mark_name").on("mousedown",function(){
                        // 拼接答案选项
                        title = $(this).text();
                        answerOptObj = $(this).siblings().find(".answerBg"); // 答案选项
                        optionStr = "" // 清空
                        for(let i = 0; i<answerOptObj.length; i++){
                            optionStr += answerOptObj.eq(i).text().replace(/\s+/g, ' ').trim() + "\n";
                        }
                        // console.log(title + "\n" + optionStr)
                        copyToBoard(title + "\n" + optionStr)
                    });
                }
                else if(copyModeIndex == 1){
                    $(".mark_name").append("<b style='border: none;outline: none;background: white;cursor:default;user-select: none' class='copyButton';>copy</b>");
                    // 拼接选项和答案 点击copy复制
                    $(".copyButton").on("click",function(){
                        // 拼接答案选项
                        // alert($(this).parent().parent().find(".stem_answer").text().replace(/\r|\n/ig,""));
                        // title =$(this).siblings("span").text() + $(this).siblings("div").text();
                        title =$(this).parent("h3:eq(0)").text().split("copy")[0];// 题号+ 题型 + 题目
                        answerOptObj = $(this).parent().parent().find(".answerBg"); // 答案选项
                        optionStr = "" // 清空
                        for(let i = 0; i<answerOptObj.length; i++){
                            // alert(answerOptObj.eq(i).find("span").text())
                            // optionStr += answerOptObj.eq(i).find("span").text() +" "+ answerOptObj.eq(i).find("div").text() + "\n";
                            optionStr += answerOptObj.eq(i).text().replace(/\s+/g, ' ').trim() + "\n";
                        }
                        copyToBoard(title + "\n" + optionStr)
                    });
                }
                task1 = true;
            }

            if($("iframe#ueditor_0").contents().find('body').length >0){
                // 粘贴
                $('iframe[id^="ueditor_"]').each(function (){
                    let bodyElement = $(this).contents().find('body');
                    let iframeObj = this;
                    bodyElement[0].addEventListener('paste', function(e){
                        e.stopPropagation();
                    }, true);
                    // 自动点保存
                    bodyElement.on("blur",function(){
                        $(iframeObj).parents(".sub_que_div_parent").find(".savebtndiv a").click();
                    });
                });

                task2 = true;
            }

            if(task1 && task2){
                setTimeout(function(){clearInterval(interval1);},1000);
            }

        });


        // alert("new Mooc")
    }
    else if(!window.location.href.includes("examnotes")){
        // alert("不支持老学习通");
        // let interval1=setInterval(function(){
        //     if($(".mark_name").length > 0){
        //         $(".mark_name").append("<button class='copyButton'>copy</button>");
        //         $(".copyButton").on("click",function(e){
        //             copyToBoard(`${$(".mark_name:eq(0)").text()}`);
        //             //alert($(".mark_name").find("div").text());
        //             // 按钮在表单中 故阻止表单提交
        //             // e.preventDefault();
        //             // return false;
        //         });
        //         clearInterval(interval1);
        //     }
        // });
    }


    // 复制到剪贴板
    function copyToBoard (text) {
        try {
            const input = document.createElement('textarea')
            input.value = text
            document.body.appendChild(input)
            // input.focus() // focus会滚动页面到input处
            input.select()
            document.execCommand('copy')
            document.body.removeChild(input)
        } catch (err) {
            alert("浏览器不支持")
        }
    }

})();