Better Luogu!

Luogu Expansion

目前為 2024-08-09 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Better Luogu!
// @namespace    http://tampermonkey.net/
// @version      1.6
// @description:zh  洛谷扩展
// @description  Luogu Expansion
// @author       volatile
// @match        *://*.luogu.com.cn/*
// @match        *://*.luogu.com/*
// @icon         https://fecdn.luogu.com.cn/luogu/logo.png?0fdd294ff62e331d2f70e1a37ba4ee02
// @grant        GM_addStyle
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_deleteValue
// @grant        GM_listValues
// @grant        GM_setClipboard
// @grant        GM_xmlhttpRequest
// @grant        GM_getResourceText
// @grant        unsafeWindow
// @connect      baidu.com
// @require      https://unpkg.com/sweetalert/dist/sweetalert.min.js
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    //进入插件
    console.log("This is Better Luogu!");
    console.log("\n          _____                    _____                    _____                    _____            _____          \n         /\\    \\                  /\\    \\                  /\\    \\                  /\\    \\          /\\    \\         \n        /::\\____\\                /::\\    \\                /::\\____\\                /::\\____\\        /::\\    \\        \n       /::::|   |               /::::\\    \\              /:::/    /               /:::/    /       /::::\\    \\       \n      /:::::|   |              /::::::\\    \\            /:::/   _/___            /:::/    /       /::::::\\    \\      \n     /::::::|   |             /:::/\\:::\\    \\          /:::/   /\\    \\          /:::/    /       /:::/\\:::\\    \\     \n    /:::/|::|   |            /:::/__\\:::\\    \\        /:::/   /::\\____\\        /:::/    /       /:::/  \\:::\\    \\    \n   /:::/ |::|   |           /::::\\   \\:::\\    \\      /:::/   /:::/    /       /:::/    /       /:::/    \\:::\\    \\   \n  /:::/  |::|   | _____    /::::::\\   \\:::\\    \\    /:::/   /:::/   _/___    /:::/    /       /:::/    / \\:::\\    \\  \n /:::/   |::|   |/\\    \\  /:::/\\:::\\   \\:::\\    \\  /:::/___/:::/   /\\    \\  /:::/    /       /:::/    /   \\:::\\ ___\\ \n/:: /    |::|   /::\\____\\/:::/__\\:::\\   \\:::\\____\\|:::|   /:::/   /::\\____\\/:::/____/       /:::/____/  ___\\:::|    |\n\\::/    /|::|  /:::/    /\\:::\\   \\:::\\   \\::/    /|:::|__/:::/   /:::/    /\\:::\\    \\       \\:::\\    \\ /\\  /:::|____|\n \\/____/ |::| /:::/    /  \\:::\\   \\:::\\   \\/____/  \\:::\\/:::/   /:::/    /  \\:::\\    \\       \\:::\\    /::\\ \\::/    / \n         |::|/:::/    /    \\:::\\   \\:::\\    \\       \\::::::/   /:::/    /    \\:::\\    \\       \\:::\\   \\:::\\ \\/____/  \n         |::::::/    /      \\:::\\   \\:::\\____\\       \\::::/___/:::/    /      \\:::\\    \\       \\:::\\   \\:::\\____\\    \n         |:::::/    /        \\:::\\   \\::/    /        \\:::\\__/:::/    /        \\:::\\    \\       \\:::\\  /:::/    /    \n         |::::/    /          \\:::\\   \\/____/          \\::::::::/    /          \\:::\\    \\       \\:::\\/:::/    /     \n         /:::/    /            \\:::\\    \\               \\::::::/    /            \\:::\\    \\       \\::::::/    /      \n        /:::/    /              \\:::\\____\\               \\::::/    /              \\:::\\____\\       \\::::/    /       \n        \\::/    /                \\::/    /                \\::/____/                \\::/    /        \\::/____/        \n         \\/____/                  \\/____/                  ~~                       \\/____/                          \n                                                                                                                     ");

    var css = ".swal-overlay {background-color: rgba(0, 0, 0, 0.5);} .rating-star {cursor: pointer;font-size: 24px;color: #ccc;}.rating-star.active, .rating-star:hover {color: #ff9800;}";
    var style = document.createElement("style");
    style.innerHTML = css;
    document.head.appendChild(style);


    let nowurl = window.location.href;
    console.log(nowurl);

    let nowcolor1;
    let nowcolor="";
    if(nowurl == 'https://www.luogu.com.cn/'){
        nowcolor1 = document.querySelector('#app-old > div.lg-index-content.am-center > div:nth-child(1) > div > div > div > div.am-u-md-4.lg-punch.am-text-center > h2 > a').className;
        for(let i=0;i<nowcolor1.length;i++){
            if(nowcolor1[i]==' ') break;
            nowcolor = nowcolor + nowcolor1[i];
        }
    }
    console.log(nowcolor);

    function setcookie(name,value,days,path,domain,secure){
        let expires = "";
        if(days){
            let date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = "; expires=" + date.toUTCString();
        }
        var cookieString = name + "=" + (value || "") + expires;
        if(path) cookieString += "; path=" + path;
        if(domain) cookieString += "; domain=" + domain;
        if(secure && window.location.protocol === "https:") cookieString += "; secure";
        document.cookie = cookieString;
    }

    function getcookie(name){
        const cookiestring = document.cookie;
        const cookies = cookiestring.split('; ');
        for(const cookie of cookies){
            const [cookiename, cookievalue] = cookie.split('=');
            if(cookiename === name) return cookievalue;
        }
        if(name == 'color'){
            setcookie('color',nowcolor,114514,'/','luogu.com.cn',true);
            return nowcolor;
        }
        else if(name == 'hanger'){
            setcookie('hanger','null',114514,'/','luogu.com.cn',true);
            return "null";
        }
        else if(name == 'version'){
            setcookie('version','1.6',114514,'/','luogu.com.cn',true);
            return "1.6";
        }
    }


    function deletecookie(name){
        document.cookie = name+ '=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
    }

    function colorschange(changecolor){//emm,这个函数,额,可能要用吧
        if(changecolor == 'lg-fg-gray') return 'rgb\(191, 191, 191\)';
        else if(changecolor == 'lg-fg-blue') return 'rgb\(14, 144, 210\)';
        else if(changecolor == 'lg-fg-green') return 'rgb\(94, 185, 94\)';
        else if(changecolor == 'lg-fg-orange') return 'rgb\(230, 126 34\)';
        else if(changecolor == 'lg-fg-red') return 'rgb\(231, 76, 60\)';
        else if(changecolor == 'lg-fg-purple') return 'rgb\(142, 68, 173\)';
        else if(changecolor == 'lg-fg-brown') return 'rgb\(173, 139, 0\)';
    }

    function update(){
        swal("Better Luogu! 1.6","更新:1.修复了关于tag的bug;",{
            buttons: {
                cancel: "取消",
                _continue: {
                    text: "继续",
                    value: "continue",
                },
            },
        })
        .then((value) => {
            if(value=="continue") swal("Better Luogu! 1.6","2.全部替换为sweetalert(你会喜欢的😍😍😍)");
        });
    }

    if(getcookie('version') != '1.6'){
        setcookie('version','1.6',114514,'/','luogu.com.cn',true);
        update();
    }

    let changecolor = getcookie('color');

    let tagchange = getcookie('tag');
    let tag = document.createElement("span");
    let tagcolor;
    if(changecolor != 'lg-fg-blue' && changecolor != 'lg-fg-orange'){
        tagcolor = changecolor.replace('fg','bg');
        tag.className = 'am-badge am-radius ' + tagcolor;
    }
    else if(changecolor == 'lg-fg-blue'){
        tagcolor = 'color-none';
        tag.className = 'am-badge am-radius ' + tagcolor;
        tag.style.background = 'rgb(52, 152, 219)';
    }
    else if(changecolor == 'lg-fg-orange'){
        tagcolor = 'color-none';
        tag.className = 'am-badge am-radius ' + tagcolor;
        tag.style.background = 'rgb(243, 156, 17)';
    }
    if(changecolor != 'lg-fg-brown') tag.textContent = tagchange;
    else tag.textContent = '作弊者';

    //首页
    if(nowurl == 'https://www.luogu.com.cn/'){
        //日历
        const today=new Date();
        const year=today.getFullYear();
        const month=String(today.getMonth()+1);
        const day=String(today.getDate());
        console.log(year);
        console.log(month);
        console.log(day);
        let date_html='<h2>今天是 '+year+'-'+month+'-'+day+'</h2>';
        if(month==1&&day==1){
            date_html+='<h2>今天是元旦,新的一年,新的开始</h2>';
        }
        else if(month==2&&day==29){
            date_html+='<h2>今年是闰年</h2>';
        }
        else if(month==4&&day==1){
            date_html+='<h2>今天是愚人节</h2>';
        }
        else if(month==5&&day==1){
            date_html+='<h2>今天是劳动节</h2>';
        }
        else if(month==6&&day==1){
            date_html+='<h2>今天是儿童节</h2>';
        }
        else if(month==9&&day==10){
            date_html+='<h2>今天是教师节</h2>';
        }
        else if(month==10&&day==1){
            date_html+='<h2>今天是国庆节,七天小长假,启动!</h2>'
        }
        else if(month==12&&day==31){
            date_html+='<h2>明年见</h2>';
        }
        else date_html+='<h2>欢迎!</h2>';
        let date = document.createElement('div');
        date.className = 'lg-article';
        date.innerHTML = date_html;
        document.querySelector('div.lg-right > div:nth-child(1)').insertAdjacentElement('beforebegin', date);

        //更改用户名颜色
        let color = document.querySelector('#app-old > div.lg-index-content.am-center > div:nth-child(1) > div > div > div > div.am-u-md-4.lg-punch.am-text-center > h2 > a');
        console.log(color);
        if(changecolor != 'null') color.className=changecolor;

        function removeDivWithText(text) {
            document.querySelectorAll('div').forEach(div => {
                if(div.textContent === text) div.remove();
            });
        }

        function findElement(element, text, cclass){
            document.querySelectorAll(element).forEach(E => {
                if(E.textContent === text) E.className = cclass;
            });
        }

        removeDivWithText('暂无推荐');
        removeDivWithText('洛谷根据您近期的做题情况,使用机器学习自动为您推荐符合您目前程度的题目。本列表每日更新一次。');

        GM_xmlhttpRequest({
            method: "GET",
            url: 'https://top.baidu.com/board?tab=realtime',
            onload: function(response){
                let rs = new DOMParser();
                let doc = rs.parseFromString(response.responseText, "text/html");
                for(let i=1;i<=5;i++){
                    let th = i.toString();
                    let selector = '#sanRoot > main > div.container.right-container_2EFJr > div > div:nth-child(2) > div:nth-child(' + th + ') > div.content_1YWBm > a > div.c-single-text-ellipsis';
                    let _rs = doc.querySelector(selector);
                    console.log(_rs.textContent);
                    let selector2 = '#sanRoot > main > div.container.right-container_2EFJr > div > div:nth-child(2) > div:nth-child(' + th + ') > div.content_1YWBm > a';
                    let rslink = doc.querySelector(selector2);
                    console.log(rslink);
                    let link = document.createElement("a");
                    link.href = rslink.href;
                    link.textContent = _rs.textContent;
                    let RS = document.createElement("p");
                    RS.appendChild(link);
                    findElement('h2','智能推荐','zntj');
                    document.getElementsByClassName('zntj')[0].parentNode.append(RS);
                }
            }
        });
    }

    window.onload = function(){
        let _hanger = getcookie('hanger');
        let __hanger = document.createElementNS('http://www.w3.org/2000/svg','svg');
        __hanger.setAttribute('width','16');
        __hanger.setAttribute('height','16');
        __hanger.setAttribute('viewBox','0 0 16 16');
        __hanger.setAttribute('style','margin-bottom: -3px;');

        if(_hanger == 'green') __hanger.setAttribute('fill','#5eb95e');
        else if(_hanger == 'blue') __hanger.setAttribute('fill','#3498db');
        else if(_hanger == 'golden') __hanger.setAttribute('fill','#f1c40f');

        let ___hanger = document.createElementNS('http://www.w3.org/2000/svg','path');
        ___hanger.setAttribute('d','M16 8C16 6.84375 15.25 5.84375 14.1875 5.4375C14.6562 4.4375 14.4688 3.1875 13.6562 2.34375C12.8125 1.53125 11.5625 1.34375 10.5625 1.8125C10.1562 0.75 9.15625 0 8 0C6.8125 0 5.8125 0.75 5.40625 1.8125C4.40625 1.34375 3.15625 1.53125 2.34375 2.34375C1.5 3.1875 1.3125 4.4375 1.78125 5.4375C0.71875 5.84375 0 6.84375 0 8C0 9.1875 0.71875 10.1875 1.78125 10.5938C1.3125 11.5938 1.5 12.8438 2.34375 13.6562C3.15625 14.5 4.40625 14.6875 5.40625 14.2188C5.8125 15.2812 6.8125 16 8 16C9.15625 16 10.1562 15.2812 10.5625 14.2188C11.5938 14.6875 12.8125 14.5 13.6562 13.6562C14.4688 12.8438 14.6562 11.5938 14.1875 10.5938C15.25 10.1875 16 9.1875 16 8ZM11.4688 6.625L7.375 10.6875C7.21875 10.8438 7 10.8125 6.875 10.6875L4.5 8.3125C4.375 8.1875 4.375 7.96875 4.5 7.8125L5.3125 7C5.46875 6.875 5.6875 6.875 5.8125 7.03125L7.125 8.34375L10.1562 5.34375C10.3125 5.1875 10.5312 5.1875 10.6562 5.34375L11.4688 6.15625C11.5938 6.28125 11.5938 6.5 11.4688 6.625Z');
        __hanger.appendChild(___hanger);
        let _hanger_ = document.createElement("a");
        _hanger_.href = 'https://www.luogu.com/discuss/142324';
        _hanger_.appendChild(__hanger);

        let button = document.createElement("a");
        button.innerHTML = '<button id="NLTB" style=\"background-color: rgb\(94,114,228\);border-radius: 7px;color: white;border: none;padding: 7px 12px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;margin: 4px 2px;cursor: pointer;border: none;box-shadow: 2px 3px 7px #000;\">NEWLG</button>';
        if(nowurl == 'https://www.luogu.com.cn/' || nowurl == 'https://www.luogu.com.cn/chat' || nowurl.includes('https://www.luogu.com.cn/user/notification') || (nowurl.includes('https://www.luogu.com.cn/user/') && !nowurl.includes('https://www.luogu.com.cn/user/setting'))) document.querySelector('#app > div.main-container > div.wrapper.wrapped.lfe-body.header-layout.tiny > div.container > nav').insertAdjacentElement('beforebegin', button);
        else{
            let user_nav = document.querySelector('div.user-nav[data-v-2dfcfd35]');
            let c_usernav = document.querySelector('nav[data-v-2dfcfd35]');
            user_nav.style.verticalAlign = 'middle';
            c_usernav.insertBefore(button,c_usernav.firstChild);
            button.href = 'https://zhuangjihong.github.io/NEWLG/';
        }
        const tb = document.getElementById("NLTB");

        function reloadmenu(){
            swal("Better Luogu!","更改成功","success",{button: "刷新"}).then((value) => {location.reload();});
        }

        function changeusercolor(){
            swal({
                title: "Better Luogu!",
                text: "输入一个词语(规则见油叉插件页)",
                content: "input",
                button: {
                    text: "更改",
                    closeModal: false,
                },
            })
            .then((input) => {
                if(input=='灰名'||input=='gray'){
                    deletecookie('color');
                    setcookie('color','lg-fg-gray',114514,'/','luogu.com.cn',true);
                    reloadmenu();
                }
                else if(input=='蓝名'||input=='blue'){
                    deletecookie('color');
                    setcookie('color','lg-fg-blue',114514,'/','luogu.com.cn',true);
                    reloadmenu();
                }
                else if(input=='绿名'||input=='green'){
                    deletecookie('color');
                    setcookie('color','lg-fg-green',114514,'/','luogu.com.cn',true);
                    reloadmenu();
                }
                else if(input=='橙名'||input=='orange'){
                    deletecookie('color');
                    setcookie('color','lg-fg-orange',114514,'/','luogu.com.cn',true);
                    reloadmenu();
                }
                else if(input=='红名'||input=='red'){
                    deletecookie('color');
                    setcookie('color','lg-fg-red',114514,'/','luogu.com.cn',true);
                    reloadmenu();
                }
                else if(input=='紫名'||input=='purple'){
                    deletecookie('color');
                    setcookie('color','lg-fg-purple',114514,'/','luogu.com.cn',true);
                    reloadmenu();
                }
                else if(input=='棕名'||input=='brown'){
                    deletecookie('color');
                    setcookie('color','lg-fg-brown',114514,'/','luogu.com.cn',true);
                    reloadmenu();
                }
                else if(input=='复原'||input=='recover'){
                    deletecookie('color');
                    setcookie('color',nowcolor,114514,'/','luogu.com.cn',true);
                    reloadmenu();
                }
                else swal("Better Luogu!","请按规范输入","error");
            });
        }

        function changehanger(){
            swal("Better Luogu!","选择一个勾子", {
                buttons: {
                    cancel: "取消",
                    greenhanger: {
                        text: "绿勾",
                        value: "greenhanger",
                    },
                    bluehanger: {
                        text: "蓝勾",
                        value: "bluehanger",
                    },
                    goldenhanger: {
                        text: "金勾",
                        value: "goldenhanger",
                    },
                    nullhanger: {
                        text: "无勾",
                        value: "nullhanger",
                    },
                }
            })
            .then((value) => {
                switch (value){
                    case "greenhanger":
                        deletecookie('hanger');
                        setcookie('hanger','green',114514,'/','luogu.com.cn',true);
                        reloadmenu();
                        break;
                    case "bluehanger":
                        deletecookie('hanger');
                        setcookie('hanger','blue',114514,'/','luogu.com.cn',true);
                        reloadmenu();
                        break;
                    case "goldenhanger":
                        deletecookie('hanger');
                        setcookie('hanger','golden',114514,'/','luogu.com.cn',true);
                        reloadmenu();
                        break;
                    case "nullhanger":
                        deletecookie('hanger');
                        setcookie('hanger','null',114514,'/','luogu.com.cn',true);
                        reloadmenu();
                        break;
                }
            });
        }

        function changetag(){
            swal({
                title: "Better Luogu!",
                text: "输入一个tag(无输入以取消)",
                content: "input",
                button: {
                    text: "更改",
                    closeModal: false,
                },
            })
            .then((value) => {
                if(getcookie('color') == 'lg-fg-brown') swal("Better Luogu!","当前为棕名","error");
                else if(value.length > 10) swal("Better Luogu!","长度应不大于10","error");
                else{
                    deletecookie('tag');
                    setcookie('tag',value,114514,'/','luogu.com.cn',true);
                    reloadmenu();
                }
            });
        }

        tb.addEventListener('click', function(){
            if(nowurl == 'https://www.luogu.com.cn/'){
                swal("Better Luogu!","选择一个更改", {
                    buttons: {
                        cancel: "取消",
                        username: {
                            text: "颜色",
                            value: "color",
                        },
                        hanger: {
                            text: "勾子",
                            value: "hanger",
                        },
                        tag: {
                            text: "tag",
                            value: "tag",
                        },
                        update: {
                            text: "更新",
                            value: "update",
                        },
                    },
                })
                .then((value) => {
                    switch (value){
                        case "color":
                            changeusercolor();
                            break;
                        case "hanger":
                            changehanger();
                            break;
                        case "tag":
                            changetag();
                            break;
                        case "update":
                            update();
                            break;
                    }
                });
            }
        });
        if(nowurl == 'https://www.luogu.com.cn/'){
            if(getcookie('hanger')!='null'){
                document.querySelector('#app-old > div.lg-index-content.am-center > div:nth-child(1) > div > div > div > div.am-u-md-4.lg-punch.am-text-center > h2 > a').innerHTML += '&nbsp;';
                document.querySelector('#app-old > div.lg-index-content.am-center > div:nth-child(1) > div > div > div > div.am-u-md-4.lg-punch.am-text-center > h2 > a').appendChild(_hanger_);
            }
            if(getcookie('tag')!=''){
                document.querySelector('#app-old > div.lg-index-content.am-center > div:nth-child(1) > div > div > div > div.am-u-md-4.lg-punch.am-text-center > h2 > a').innerHTML += '&nbsp;';
                document.querySelector('#app-old > div.lg-index-content.am-center > div:nth-child(1) > div > div > div > div.am-u-md-4.lg-punch.am-text-center > h2 > a').appendChild(tag);
            }
        }
        else if(nowurl.includes('https://www.luogu.com.cn/user/')&&!nowurl.includes('https://www.luogu.com.cn/user/setting')){
            if(document.querySelector('#app > div.main-container > main > div > div.full-container > section.main > div > div:nth-child(2)').className == 'introduction marked') document.querySelector('#app > div.main-container > main > div > div.full-container > section.main > div > div:nth-child(2)').removeAttribute('style');
            else document.querySelector('#app > div.main-container > main > div > div.full-container > section.main > div > div:nth-child(2)').remove();
            let jieshao = document.querySelector('#app > div.main-container > main > div > div.full-container > section.main > div > div.introduction.marked');
            if(jieshao.style.display == 'none') jieshao.removeAttribute('style');
        }
    };
})();