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 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name animestc.net - Automatically set the highest quality for all items on page
  3. // @name:pt-BR animestc.net - Seleciona a qualidade mais alta de download em todos os items da página
  4. // @namespace secretx_scripts
  5. // @match *://animestc.net/*
  6. // @match *://*.animestc.net/*
  7. // @version 2022.03.12
  8. // @author SecretX
  9. // @description Automatically set the highest quality for all items on page
  10. // @description:pt-br Seleciona automaticamente a qualidade mais alta de download em todos os items da página
  11. // @grant none
  12. // @icon https://www.animestc.net/icons/favicon-32x32.png
  13. // @license GNU LGPLv3
  14. // ==/UserScript==
  15.  
  16. function sortByQuality(map) {
  17. return new Map([...map].sort((a, b) => String(a[0]).localeCompare(b[0])));
  18. }
  19.  
  20. const qualityRegex = /\s*(\d+)\s*p\s*/i;
  21.  
  22. function parseQuality(string) {
  23. let parsedQuality = string.match(qualityRegex);
  24. if (parsedQuality == null)
  25. return string;
  26. else
  27. return parsedQuality[1];
  28. }
  29.  
  30. /**
  31. * Parses the quality options inside the `div` parameter, and return the "best" quality available.
  32. *
  33. * @param div a div that contains `a` html elements that represent the quality options of an episode
  34. * @returns {any} a map entry with the quality number (or string), e.g. 1080, 720, MP4 as key, and the highest
  35. * quality `a` html element as value
  36. */
  37. function highestQualityFromDiv(div) {
  38. const qualityMap = new Map();
  39. for (const elementA of div) {
  40. const quality = parseQuality(elementA.innerText);
  41. qualityMap.set(quality, elementA);
  42. }
  43. return sortByQuality(qualityMap).entries().next().value;
  44. }
  45.  
  46. function getHighestQualitiesFromDivColl(divHtmlCol) {
  47. const highestQualityElems = [];
  48. for (let div of divHtmlCol) {
  49. const children = div.children;
  50. const highestQualityElem = highestQualityFromDiv(children);
  51. highestQualityElems.push(highestQualityElem[1])
  52. }
  53. return highestQualityElems;
  54. }
  55.  
  56. window.addEventListener("load", function() {
  57. 'use strict';
  58.  
  59. const qualityDivs = document.getElementsByClassName("episode-info-tabs");
  60. if (qualityDivs.length === 0) {
  61. console.info("There is no quality div on this page, skipping automatic quality selection for now.");
  62. return;
  63. }
  64. const highestQualityElems = getHighestQualitiesFromDivColl(qualityDivs);
  65. console.info(`highestQualityElems = ${highestQualityElems}`);
  66. for (const elementA of highestQualityElems) {
  67. console.info(`Clicking on ${elementA} element!`);
  68. elementA.click();
  69. }
  70. }, false);