Greasy Fork

nyaablue

brings blue to nyaav2 with seadex api

目前为 2022-03-18 提交的版本。查看 最新版本

// ==UserScript==
// @name        nyaablue
// @namespace   garret's bad scripts
// @match       https://nyaa.si/
// @match       https://nyaa.si/*q=*
// @match       https://nyaa.si/user/*
// @match       https://nyaa.si/view/*
// @grant       GM.xmlHttpRequest
// @grant       GM.setValue
// @grant       GM.getValue
// @version     0.3.1
// @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 get_ids(){let ids=await 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=[];console.log("got");for(let i in entries){ids.push(String(entries[i].nyaaIDs))}return new Set(ids)}})}

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].childNodes
    let i
    for (i in torrentlist) {
        let id;
        try {
            id = torrentlist[i]
                .cells[1]
                .childNodes[1]
                .attributes
                .href
                .value
                .match("view/([0-9]+)")[1]
        } catch {
            continue;
        }
        if (ids.has(id)) {
            torrentlist[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 (let i in entries) {
                for (let j in entries[i].nyaaIDs) {
                  ids.push(String(entries[i].nyaaIDs[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()