Greasy Fork

来自缓存

Greasy Fork is available in English.

慕课复制题目

在慕课页面上添加一个按钮,用于复制题目文本到剪贴板。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         慕课复制题目
// @version      1.0.2
// @author       BaiLu
// @description  在慕课页面上添加一个按钮,用于复制题目文本到剪贴板。
// @license MIT
// @match        https://www.icourse163.org/*
// @grant        none
// @namespace http://greasyfork.icu/users/1411786
// ==/UserScript==

(function () {
    'use strict';



    // 添加样式以显示按钮
    var style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = `
        #copyTextBtn {
            position: fixed;
            bottom: 70px;
            right: 20px;
            padding: 10px 20px;
            font-size: 16px;
            color: #fff;
            background-color: #007bff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            z-index: 9999;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            transition: background-color 0.3s ease;
        }
        #copyTextBtn:hover {
            background-color: #0056b3;
        }
        #copyTextBtn:active {
            background-color: #004494;
        }
    `;
    document.head.appendChild(style);



    // 创建按钮并添加到页面,但只在特定域名下
    var btn = document.createElement('button');
    btn.id = 'copyTextBtn';
    btn.textContent = '慕课题目';
    document.body.appendChild(btn);

    // 定义复制文本到剪贴板的函数
    function copyTextToClipboard(text) {
        if (navigator.clipboard && window.isSecureContext) {
            navigator.clipboard.writeText(text).then(function () {
                console.log('Text copied to clipboard');
            }).catch(function (error) {
                console.error('Could not copy text: ', error);
            });
        } else {
            var textArea = document.createElement("textarea");
            textArea.value = text;
            textArea.style.position = "fixed";
            document.body.appendChild(textArea);
            textArea.focus();
            textArea.select();
            try {
                var successful = document.execCommand('copy');
                var msg = successful ? 'Copied to clipboard' : 'Failed to copy';
                console.log(msg);
            } catch (err) {
                console.error('Could not copy text: ', err);
            }
            document.body.removeChild(textArea);
        }
    }

    // 为按钮添加点击事件
    btn.addEventListener('click', function () {
        let a = document.querySelectorAll(".u-questionItem")
        let b = ""
        a.forEach(i => {
            let s = i.querySelector(".u-icon-correct")
            if (!s) {
                b += i.innerText + "\n"
            } else {
                b += i.querySelector(".j-title").innerText + " "
                let s1 = i.querySelector(".choices.f-cb").querySelectorAll("li.f-cb")
                s1.forEach(ss => {
                    b += ss.innerText
                    if (ss.querySelector(".u-icon-correct")) {
                        b += "√ "
                    } else {
                        b += "x "
                    }
                })
            }
        })
        copyTextToClipboard(b);
    });

})();