animestc.net - Automatically set the highest quality for all items on page

Automatically set the highest quality for all items on page

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

// ==UserScript==
// @name        animestc.net - Automatically set the highest quality for all items on page
// @name:pt-BR  animestc.net - Seleciona a qualidade mais alta de download em todos os items da página
// @namespace   secretx_scripts
// @match       *://animestc.net/*
// @match       *://*.animestc.net/*
// @version     2022.03.12
// @author      SecretX
// @description Automatically set the highest quality for all items on page
// @description:pt-br Seleciona automaticamente a qualidade mais alta de download em todos os items da página
// @grant       none
// @icon        https://www.animestc.net/icons/favicon-32x32.png
// @license     GNU LGPLv3
// ==/UserScript==

function sortByQuality(map) {
    return new Map([...map].sort((a, b) => String(a[0]).localeCompare(b[0])));
}

const qualityRegex = /\s*(\d+)\s*p\s*/i;

function parseQuality(string) {
    let parsedQuality = string.match(qualityRegex);
    if (parsedQuality == null)
        return string;
    else
        return parsedQuality[1];
}

/**
 * Parses the quality options inside the `div` parameter, and return the "best" quality available.
 *
 * @param div a div that contains `a` html elements that represent the quality options of an episode
 * @returns {any} a map entry with the quality number (or string), e.g. 1080, 720, MP4 as key, and the highest
 * quality `a` html element as value
 */
function highestQualityFromDiv(div) {
    const qualityMap = new Map();
    for (const elementA of div) {
        const quality = parseQuality(elementA.innerText);
        qualityMap.set(quality, elementA);
    }
    return sortByQuality(qualityMap).entries().next().value;
}

function getHighestQualitiesFromDivColl(divHtmlCol) {
    const highestQualityElems = [];
    for (let div of divHtmlCol) {
        const children = div.children;
        const highestQualityElem = highestQualityFromDiv(children);
        highestQualityElems.push(highestQualityElem[1])
    }
    return highestQualityElems;
}

window.addEventListener("load", function() {
    'use strict';

    const qualityDivs = document.getElementsByClassName("episode-info-tabs");
    if (qualityDivs.length === 0) {
        console.info("There is no quality div on this page, skipping automatic quality selection for now.");
        return;
    }
    const highestQualityElems = getHighestQualitiesFromDivColl(qualityDivs);
    console.info(`highestQualityElems = ${highestQualityElems}`);
    for (const elementA of highestQualityElems) {
        console.info(`Clicking on ${elementA} element!`);
        elementA.click();
    }
}, false);