Show Anime Duration With Seconds

This script will show the Duration Time with seconds for all animes.

目前為 2021-05-22 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Show Anime Duration With Seconds
// @namespace    DurationWithSeconds
// @version      0.7
// @description  This script will show the Duration Time with seconds for all animes.
// @author       hacker09
// @include      /^https:\/\/myanimelist\.net\/anime\/[\d]+(\/.*)?/
// @icon         https://www.google.com/s2/favicons?domain=myanimelist.net
// @run-at       document-end
// @grant        GM_deleteValue
// @grant        GM_listValues
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==
(async function() {
  'use strict';
  var animeid = location.pathname.match(/\d+/)[0]; //Detect the anime id
  var StoredAnimeIdsArray = []; //Creates a new blank array
  GM_listValues().forEach(a => StoredAnimeIdsArray.push('^' + a)); //Add all anime IDs on tampermonkey to the array
  var StoredAnimeIdsRegex = new RegExp(StoredAnimeIdsArray.join('$|')); //Create a new variable and regex containing all the values saved on tampermonkey and replace the , separator with the or $| regex symbols

  if (GM_listValues().length >= 100) //If there's 100 anime ids and durations stored on tampermonkey
  { //Starts the if condition
    GM_listValues().forEach(a => GM_deleteValue(a)); //Erase all the 100 stored anime IDs and their durations stored on tampermonkey
  } //Finishes the if condition

  function findTheDurationText() {
    const allInfo = [...[...document.querySelectorAll("h2")].find(h2 => h2.textContent === "Information").parentNode.querySelectorAll("div")]; //Select all divs inside the Information h2 element
    return allInfo.find(info => info.innerText.includes("Duration")); //Return the div that contains the Duration text
  } //Find the Duration text that's inside the information h2 element

  async function GetDurationWithSecs() { //Starts the if condition
    const response = await fetch('https://myanimelist.net/dbchanges.php?aid=' + animeid + '&t=duration'); //Get the duration with seconds of the anime
    const html = await response.text(); //Gets the fetch response
    const newDocument = new DOMParser().parseFromString(html, 'text/html'); //Parses the fetch response
    const DurationWithSeconds = newDocument.querySelectorAll("input.inputtext")[1].defaultValue; //Select the duration with seconds value, and creates the variable DurationWithSeconds to hold this value
    var hours = DurationWithSeconds.split(':')[0]; //Variable to hold the hours value
    var mins = DurationWithSeconds.split(':')[1]; //Variable to hold the minutes value
    var secs = DurationWithSeconds.split(':')[2]; //Variable to hold the seconds value

    if (hours !== '00') {
      var Duration = hours + ' hr. ' + mins + ' min. ' + secs + ' sec.';
    } //If hours is NOT = 00, show hours+minutes+seconds
    else if (hours === '00' && mins === '00') {
      var Duration = ' ' + secs + ' sec.';
    } //If hours and minutes are = 00, show only the seconds (keep as it is)
    else {
      var Duration = mins + ' min. ' + secs + ' sec.';
    } //If hours is = 00, but minutes is NOT = 00, show only the minutes with seconds

    findTheDurationText().querySelector("span").nextSibling.textContent = ' ' + Duration + ' per ep.'; //Change the text content of the Duration: Element
    GM_setValue(animeid, ' ' + Duration + ' per ep.'); //Get and save the anime id and Duration With Secs as a variable
  }; //Finishes the async function

  if (document.querySelector("#malLogin") === null) { //Detect the username to know if the user is logged in or not,if not then the script won't work,so it won't run

    if (animeid.match(StoredAnimeIdsRegex) !== null && StoredAnimeIdsRegex.toLocaleString() !== '/(?:)/') //If the current url anime id matches an anime id that is stored on tampermonkey, and if the Regex contains 1 or more anime ids
    { //Starts the if condition

      findTheDurationText().querySelector("span").nextSibling.textContent = GM_getValue(animeid); //Change the text content of the Duration: Element

      findTheDurationText().querySelector("span").onclick = function() { //When the bold Duration: text is clicked
        GetDurationWithSecs(); //Update the Duration With Secs
      }; //Finishes the onclick advent listener
      findTheDurationText().querySelector("span").style.cursor = 'pointer'; //Make the bold Duration: text look like it's clickable

    } //Finishes the if condition
    else //If the current url anime id does NOT match any anime id that is stored on tampermonkey
    { //Starts the else condition
      GetDurationWithSecs(); //Run the function to get the Duration With Secs
    } //Finishes the else condition
  } //Finishes the if condition
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址