Greasy Fork

Greasy Fork is available in English.

超星隐藏显示正确答案

通过单击键盘左上方的~/`/·键来隐藏/显示正确答案,用于期末复习

当前为 2024-01-01 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         超星隐藏显示正确答案
// @namespace    http://tampermonkey.net/
// @version      1.2.6
// @description  通过单击键盘左上方的~/`/·键来隐藏/显示正确答案,用于期末复习
// @author       LeonYew-SWPU
// @match        https://mooc1.chaoxing.com/mooc-ans/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=chaoxing.com
// @projectURL   https://github.com/LeonYew-SWPU/HideShowChaoXingAnswer
// @license MIT
// ==/UserScript==
(function() {
    'use strict';
    // 引入iconfont
    var link = document.createElement('link');
    link.href = 'https://at.alicdn.com/t/c/font_4394726_ifn084x1vh.css';
    link.rel = 'stylesheet';
    link.type = 'text/css';
    document.head.appendChild(link);    // 将link元素添加到head元素中、

    // 初始化,获取元素
    var answersWindow = document.getElementsByClassName('mark_answer'); // 获取答案框div
    var answersChoice = document.getElementsByClassName('mark_key clearfix'); // 获取答案选择题div
    var answerFill = {
        'colorDeep': document.getElementsByClassName('mark_fill colorDeep'), // 获取填空题我的答案
        'colorGreen': document.getElementsByClassName('mark_fill colorGreen') // 填空题正确答案
    }
    var answerStatuses = document.getElementsByClassName('mark_score'); // 获取答案状态div(对错+分数)
    var lightGrey = '#f9f9f9'; // 浅灰色
    var isHidden = false; // 答案隐藏指标

    // 遍历答案框div,添加遮罩层,初始化Status
    for (var i = 0; i < answersWindow.length; i++) {
        addCover(answersWindow[i]);
    }

    document.onkeydown = function (e) {
        if (e.key == '`') {
            flipAns();
        }
    }

    function flipAns(){
        // 改变答案隐藏指标
        isHidden = !isHidden;
        // 遍历答案div
        // 如果遍历窗口div的话,遇到简答题会找不到answers[i].style,也找不到answerStatuses[i].style
        for (var i = 0; i < answersWindow.length; i++) {
            // 每个answerWindow一定有的元素
            answersWindow[i].getElementsByClassName('cover')[0].style.display = (isHidden) ? 'block' : 'none'; // 显示遮罩层
            answersWindow[i].getElementsByClassName('icon-hide')[0].style.display = (isHidden) ? 'none' : 'inline'; // 隐藏眼睛图标
            answersWindow[i].getElementsByClassName('icon-eye')[0].style.display = (isHidden) ? 'inline' : 'none'; // 显示闭眼图标
            answersWindow[i].style.backgroundColor = (isHidden) ? lightGrey : ''; // 背景调灰
            answerStatuses[i].style.display = (isHidden) ? 'none' : 'block'; // 隐藏答案状态div

            // 每个answerWindow可能有的元素:选择题答案
            if (answersChoice[i]) {
                answersChoice[i].style.display = (isHidden) ? 'none' : 'block'; // 隐藏选择题答案
            }
            // 每个answerWindow可能有的元素:选择题答案、填空答案
            if (answerFill.colorDeep[i]) {
                answerFill.colorDeep[i].style.display = (isHidden) ? 'none' : 'block'; // 隐藏填空题我的答案
                answerFill.colorGreen[i].style.display = (isHidden) ? 'none' : 'block'; // 隐藏填空题正确答案
            }
            
        }
        // 后台提示
        console.log((isHidden) ? '答案已隐藏' : '答案已显示');
    }

    function addCover(answersWindow){
        // 修改answersWindow的布局方式为flex
        answersWindow.style.display = 'flex';
        answersWindow.style.flexDirection = 'row';

        // 添加遮罩层
        var cover = document.createElement('a');
        cover.className = 'cover';
        cover.href = '#'; // 做成超链接,表示可点击
        cover.style.textDecoration = 'underline'; // 添加下划线
        cover.style.display='none';
        cover.style.textAlign = 'left';
        cover.textContent = '答案已隐藏';
        cover.onclick = function(e){
            e.preventDefault(); // 阻止超链接的默认行为
            flipAns();
        }
        answersWindow.appendChild(cover);

        // 添加眼睛图标
        var eye = document.createElement('i');
        eye.className = 'iconfont icon-eye';
        eye.style.display = 'none'; // 最开始为隐藏状态
        eye.style.fontSize = '20px';
        eye.style.marginRight = '15px';
        eye.style.verticalAlign = 'middle';
        eye.style.fontStyle = 'normal';
        eye.onclick = function(e){
            flipAns();
        }
        answersWindow.prepend(eye);

        // 添加闭眼图标
        var eyeInvisible = document.createElement('i');
        eyeInvisible.className = 'iconfont icon-hide';
        eyeInvisible.style.fontSize = eye.style.fontSize;
        eyeInvisible.style.marginRight = eye.style.marginRight;
        eyeInvisible.style.verticalAlign = eye.style.verticalAlign;
        eyeInvisible.style.fontStyle = eye.style.fontStyle;
        eyeInvisible.onclick = eye.onclick;
        answersWindow.prepend(eyeInvisible);
    }
})();