Better Luogu Problem Searcher

洛谷题目跳转器优化

目前為 2019-07-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 Problem Searcher
// @namespace    n/a
// @version      0.0.2
// @description  洛谷题目跳转器优化
// @author       iotang
// @match        https://www.luogu.org
// @match        https://www.luogu.org/
// @match        https://www.luogu.org/#feed
// @match        https://www.luogu.org/#feed/
// @match        http://www.luogu.org
// @match        http://www.luogu.org/
// @match        http://www.luogu.org/#feed
// @match        http://www.luogu.org/#feed/
// @grant        none
// ==/UserScript==

(function()
{
    'use strict';

    var hrefOld = "https://www.luogu.org/problemnew";
    var hrefNew = "https://www.luogu.org/fe/problem";

    function jumpfn()
    {
        var target = document.getElementsByClassName("am-form-field")[0].value;
        if(target === "")return;

        var checked = document.getElementById("switch_to_fe").checked;

        var go;
        if(checked === true)go = hrefNew + "/list?keyword=" + target + "&content=true";
        else go = hrefOld + "/lists?name=" + target + "&orderitem=pid&tag=&content=1&type=";

        location.href = go;
    }

    function searchfn()
    {
        var target = document.getElementsByClassName("am-form-field")[0].value;
        if(target === "")return;

        var targetu = target.toUpperCase();
        var checked = document.getElementById("switch_to_fe").checked;
        var go = "";

        if(checked === true)go = hrefNew;
        else go = hrefOld + "/show";

        if(target.match(/^[1-9][0-9][0-9][0-9]+$/) == target)go = go + "/P" + target;
        else if(targetu.match(/^[0-9]+[A-Z][0-9]?$/) == targetu)go = go + "/CF" + targetu;
        else if(targetu.match(/^(?:U|P|CF|SP|AT|UVA)[0-9]+[A-Z]?[0-9]?$/) == targetu)go = go + "/" + targetu;
        else
        {
            if(checked === true)go = hrefNew + "/list?keyword=" + target + "&content=true";
            else go = hrefOld + "/lists?name=" + target + "&orderitem=pid&tag=&content=1&type=";
        }

        location.href = go;
    }

    var locations =
        document.getElementsByClassName("lg-index-content")[0]
    .getElementsByClassName("lg-article lg-index-stat")[0]
    .getElementsByClassName("am-btn am-btn-primary am-btn-sm")[0]
    .parentNode;

    var button = document.createElement("button");
    button.className = "am-btn am-btn-success am-btn-sm";
    button.name = "gotosearch";
    button.id = "gotosearch";
    button.innerHTML = "搜索";
    button.onclick = function(){jumpfn();};

    var checkbox = document.createElement("input");
    checkbox.type = "checkbox";
    checkbox.class = "switch_to_fe";
    checkbox.id = "switch_to_fe";
    checkbox.defaultChecked = "true";

    locations.appendChild(button);
    locations.appendChild(checkbox);

    document.getElementsByClassName("am-form-field")[0].placeholder = "输入题号或题目名,按回车确认";
    document.getElementsByClassName("am-form-field")[0].onkeyup = function(event){if(event.keyCode === 13){searchfn();}};

    document.getElementsByClassName("lg-article lg-index-stat")[0].getElementsByTagName("h2")[0].innerHTML = "问题搜索";

    document.getElementsByName("gotorandom")[0].innerHTML = "随机";
})();