New Userscripts

在 WZOI 中,给样例输入输出添加复制按钮。

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

// ==UserScript==
// @name         New Userscripts
// @namespace    http://tampermonkey.net/
// @description  在 WZOI 中,给样例输入输出添加复制按钮。
// @version      2024-02-19
// @author       chenbs
// @match        https://wzoi.cc/*
// @grant        none
// @license MIT
// ==/UserScript==
(function() {
    'use strict';
    // 复制字符串函数
    function copy(text){
        var input = document.createElement('textarea');
        input.setAttribute('id', 'input_for_copyText');
        input.value = text;
        document.getElementsByTagName('body')[0].appendChild(input);
        document.getElementById('input_for_copyText').select();
        document.execCommand('copy');
        document.getElementById('input_for_copyText').remove();
    }
    // 样例
    var fa = document.getElementsByClassName("sample_io");
    for(let i=0; i<fa.length; i++){
        let cp = document.createElement("button");
        cp.className = "copy-button";
        cp.innerText = "复制";
        cp.onclick = function(){let s=this.parentNode.innerText;copy(s.substr(0,s.length-3));};
        fa[i].appendChild(cp);
    }
    // 题解
    fa = document.getElementsByClassName("language-cpp");
    for(let i=0; i<fa.length; i++){
        if(fa[i].tagName == "PRE"){ // 检查类型是否正确
            let cp = document.createElement("button");
            cp.className = "copy-button";
            cp.innerText = "复制";
            cp.onclick = function(){let s=this.parentNode.innerText;copy(s.substr(2,s.length));};
            fa[i].insertBefore(cp, fa[i].firstChild);
        }
    }
    // 样式
    let tmp = document.createElement("style");
    tmp.innerText = `
        button.copy-button{
            display: block;
            transition:all .2s ease;
        }
        button.copy-button:hover{
            background: #DDDDDD;
        }
    `;
    document.body.appendChild(tmp);
})();