MAL set finish date from history

Shortcut to set finish watch date based on watch history

目前為 2023-08-24 提交的版本,檢視 最新版本

// ==UserScript==
// @name        MAL set finish date from history
// @namespace   Violentmonkey Scripts
// @match       https://myanimelist.net/animelist/*
// @grant       none
// @version     1.0.1
// @author      rias75
// @license     MIT
// @description Shortcut to set finish watch date based on watch history
// ==/UserScript==

/* jshint esversion: 8 */

(async () => {
  const list = await waitForElement('.list-table');
  list.addEventListener(
    'click',
    event => {
      if (event.target.innerHTML === 'Edit') {
        addInsertFromHistoryButton();
      }
    },
    true
  );
  console.info('Listener set');
})();

async function setFinishDate(popup) {
  const historyButton = popup.querySelector('.thickbox');
  if (!historyButton) {
    throw new Error("Didn't find historyButton");
  }
  const response = await fetch(historyButton.href);
  const html = await response.text();
  const dom = new DOMParser().parseFromString(html, 'text/html');
  const episode = dom.querySelector('.spaceit_pad');
  if (!episode) {
    return alert('No info');
  }
  console.info('Episode info:', episode.innerText);
  const [, month, day, year] = episode.innerHTML.match(/watched on (\d+)\/(\d+)\/(\d+)/);

  const monthSelect = popup.querySelector('#add_anime_finish_date_month');
  if (!monthSelect) {
    throw new Error("Didn't find monthSelect");
  }
  monthSelect.value = parseInt(month).toString();

  const daySelect = popup.querySelector('#add_anime_finish_date_day');
  if (!daySelect) {
    throw new Error("Didn't find daySelect");
  }
  daySelect.value = parseInt(day).toString();

  const yearSelect = popup.querySelector('#add_anime_finish_date_year');
  if (!yearSelect) {
    throw new Error("Didn't find yearSelect");
  }
  yearSelect.value = parseInt(year).toString();
}

async function addInsertFromHistoryButton() {
  const iframe = await waitForElement('#fancybox-frame');
  const popupWindow = iframe.contentWindow;
  const insertTodayButton = await waitForElement('#end_date_insert_today', popupWindow);
  const popup = popupWindow.document;
  const insertFromHistoryButton = popup.createElement('a');
  insertFromHistoryButton.innerHTML = 'Insert from history';
  insertFromHistoryButton.setAttribute('href', '#');
  insertFromHistoryButton.addEventListener('click', async event => {
    event.preventDefault();
    setFinishDate(popup);
  });
  insertTodayButton.parentNode.insertBefore(insertFromHistoryButton, insertFromHistoryButton.nextSibling);
}

async function waitForElement(selector, win = window) {
  return new Promise(resolve => {
    const interval = setInterval(() => {
      const node = win.document.querySelector(selector);
      if (node) {
        clearInterval(interval);
        resolve(node);
      }
    }, 250);
  });
}

QingJ © 2025

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