nyaablue

brings blue to nyaav2 with seadex api

目前為 2024-01-23 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        nyaablue
// @namespace   427738.xyz
// @match       https://nyaa.si/*
// @grant       GM.xmlHttpRequest
// @grant       GM.setValue
// @grant       GM.getValue
// @version     0.3.2
// @author      garret
// @description brings blue to nyaav2 with seadex api
// ==/UserScript==


/* get best from seadex api, use api/search for the nyaa search value
get all nyaa links from ^
if link is present in page, make tr element class="info" (blue) */

var dex = "https://sneedex.moe/";

function actual_main(ids) {
    if (window.location.href.match("view")) {
        set_view_blue(ids)
    } else {
        set_search_blue(ids)
    }
}

function set_view_blue(ids) {
    let page_id = window
        .location
        .href
        .match("view/([0-9]+)")[1]
    if (ids.has(page_id)) {
        document.getElementsByClassName("panel-title")[0].parentNode.parentNode.className = "panel panel-info";
    }
}

function set_search_blue(ids) {
    let torrentlist = document.getElementsByClassName("torrent-list")[0].tBodies[0].children;
    for (const i of torrentlist) {
        let id = i
            .cells[1]
            .children[0]
            .attributes
            .href
            .value
            .match("view/([0-9]+)")[1]
        if (ids.has(id)) {
            i.className = "info"
        }
    }
}

function main() {
    GM.xmlHttpRequest({
        headers: {
            "Accept": "application/json",
            "User-Agent": "nyaablue.user.js"
        },
        method: "GET",
        url: encodeURI(dex + "api/public/nyaa"),
        onload: function(response) {
            let entries = JSON.parse(response.responseText);
            let ids = new Array()
            for (const i of entries) {
                for (const j of i.nyaaIDs) {
                  ids.push(String(j))
                }
            }
            GM.setValue("ids", ids)
            GM.setValue("last_update", Date.now())
            ids = new Set(ids)
            actual_main(ids) // i would've much preferred to just get the output as a variable in actual_main but GM_xhr says no
        }
    })
}

async function check_cache() {
    try {
        const last_update = await GM.getValue("last_update")
        if (typeof last_update !== "number") {
            throw "no_time"
        }
        if (Date.now() > last_update + 3600000) { // not updated in 1 hour
            ("cache old")
            main()
        } else {
            console.log("cache fine")
            const cached_ids = await GM.getValue("ids")
            if (typeof cached_ids !== "object") {throw "bad data"}
            actual_main(new Set(cached_ids))
        }
    } catch {
        console.log("ooer")
        main()
    }
}

// dark theme "support" (thanks olli)
document.head.insertAdjacentHTML('beforeend', '<style id="css_blue" type="text/css">body.dark .torrent-list > tbody > tr.info > td {color: inherit; background-color: rgba(0, 172, 255, 0.12);} body.dark .torrent-list > tbody > tr.info:hover > td {background-color: rgba(0, 172, 255, 0.18);} body.dark div.panel-info, body.dark div.panel-info > .panel-heading {border-color: #2c414b;} body.dark div.panel-info > .panel-heading {background-color: #2a3f4a;}</style>');

// yes its jank
// i blame greasemonkey
check_cache()