// ==UserScript== // @name Luogu Search AnyWhere // @version 0.2 // @description Search AnyWhere in Luogu! // @author tiger2005 // @match https://www.luogu.com.cn/* // @icon https://cdn.luogu.com.cn/upload/usericon/3.png // @grant none // @license MIT // @require https://code.jquery.com/jquery-3.6.0.min.js // @namespace https://greasyfork.org/users/829530 // @downloadURL none // ==/UserScript== (function() { 'use strict'; var addedContent = false; let majorInterval = setInterval(function(){ if($(".user-nav .search-wrap").length == 0) return; // clearInterval(majorInterval); var sicon = $(".user-nav .search-wrap").next(); $(".user-nav .search-wrap").remove(); sicon.css("margin-left", "10px"); if(! addedContent){ $("body").append(` `); addedContent = true; $(".inputArea > input").focus(function(){ $(this).parent().addClass("onFocus"); }); $(".inputArea > input").blur(function(){ $(this).parent().removeClass("onFocus"); if($(this).val().length != 0) $(this).parent().addClass("withContent"); else $(this).parent().removeClass("withContent"); }); $(".inputArea").mouseenter(function(){ $(this).addClass("onHover"); }); $(".inputArea").mouseleave(function(){ $(this).removeClass("onHover"); }); } const problemColors = [ "Gray", "Red", "Orange", "Yellow", "Green", "Blue", "Purple", "Black" ]; const problemNames = [ "暂无评定", "入门", "普及-", "普及/提高-", "普及+/提高", "提高+/省选-", "省选/NOI-", "NOI/NOI+/CTSC" ]; var searchTimeout = null; const searchInfo = () => { searchTimeout = null; var info = $(".inputArea > input").val(); info = $.trim(info); if(info == ""){ $(".searchAnywhereContent").html(""); return; } $(".searchAnywhereContent").html(`
加载中……
`); $(".searchAnywhereContent > div").unbind('click').click((event) => { event.stopPropagation(); }) var userHtml = ""; var problemHtml = ""; var finishWorks = 0; const finishWork = () => { ++ finishWorks; if(finishWorks == 2){ if(userHtml == "" && problemHtml == "") $(".searchAnywhereContent").html(`
未搜索到相关内容
`); else{ $(".searchAnywhereContent").html(`
` + userHtml + problemHtml + `
`); $(".searchShowProblems").unbind("click").click(function(event){ window.open(`https://www.luogu.com.cn/problem/list?keyword=${info}&page=1&type=P%7CB%7CCF%7CSP%7CAT%7CUVA`, "_blank"); }); $(".searchUserCard").unbind("click").click(function(event){ window.open(`https://www.luogu.com.cn/user/${$(this).attr("uid")}`, "_blank"); }) $(".searchProblemCard").unbind("click").click(function(event){ window.open(`https://www.luogu.com.cn/problem/${$(this).attr("pid")}`, "_blank"); }) $(".searchAnywhereContent > div").unbind('click').click((event) => { event.stopPropagation(); }) } } }; const getProblemStatus = (x, y) => { if(!x && !y) return ``; if(!y) return ``; return ``; } const getCCFLevel = (x) => { if(x == null || x < 3) return ""; var color = ""; if(x <= 5) color = "#5eb95e"; else if(x <= 7) color = "#07a2f1"; else color = "#f1c40f"; return `` } $.ajax({ url: `/api/user/search?keyword=${info}`, type: 'GET', success: function(json){ json = json.users; if(json.length != 0 && json[0] != null){ userHtml = `
用户
`; json.forEach((item) => { if(item == null) return; if(item.color == "Cheater") item.badge = "作弊者"; userHtml += `
UID ${item.uid}
${item.name}${getCCFLevel(item.ccfLevel)}${item.badge != null && item.badge != "" ? `
${item.badge}
` : ""}
` }); } finishWork(); } }); $.ajax({ url: `/problem/list`, type: 'GET', headers: {"x-luogu-type": "content-only"}, data: { keyword: info, page: 1, type: "P|B|CF|SP|AT|UVA" }, success: function(json){ json = json.currentData.problems; if(json.count != 0){ problemHtml = `
题目
查看所有 ${json.count} 道题目
`; for(var i=0; i
${getProblemStatus(item.submitted, item.accepted)}
${item.title}
题号
${item.pid}
尝试
${item.totalSubmit}
通过
${item.totalAccepted}
${problemNames[item.difficulty]}
`; }; } finishWork(); } }); }; $(".inputArea > input").unbind('input propertychange').on('input propertychange', function(){ if(searchTimeout != null) clearTimeout(searchTimeout); searchTimeout = setTimeout(searchInfo, 500); }); let searchAnywhereOpen = false; sicon.unbind('click').click(function(){ if(! searchAnywhereOpen){ $(".searchAnywhere").css("display", "block"); setTimeout(() => { $(".searchAnywhere").css("opacity", "1"); }, 20); } else{ $(".searchAnywhere").css("opacity", "0"); setTimeout(() => { $(".searchAnywhere").css("display", "none"); }, 200); } searchAnywhereOpen = !searchAnywhereOpen; }); $(".searchAnywhere").unbind('click').click(() => { $(".searchAnywhere").css("opacity", "0"); setTimeout(() => { $(".searchAnywhere").css("display", "none"); }, 200); searchAnywhereOpen = false; }) $(".inputArea").unbind('click').click((event) => { event.stopPropagation(); }) }, 500); })();