AniDB add-episode button

Adds a button next to MyList entries and episodes for adding a new episode.

目前為 2018-09-03 提交的版本,檢視 最新版本

// ==UserScript==
// @name        AniDB add-episode button
// @namespace   AquaWolfGuy
// @description Adds a button next to MyList entries and episodes for adding a new episode.
// @icon        https://static.anidb.net/css/icons/touch/apple-touch-icon.png
// @author      AquaWolfGuy
// @copyright   2018, AquaWolfGuy
// @license     GPL-3.0-only
// @match       *://anidb.net/*
// @version     1.0.0
// @grant       none
// ==/UserScript==


/////////////////
// Style sheet //
/////////////////

const style = document.createElement("style");
style.innerHTML = `
	#layout-main div.mylist_all table.animelist > tbody > tr > td.action {
		min-width: 8.5em;
	}
	@media screen and (min-width: 1640px) {
		#layout-main div.mylist_all table.animelist > tbody > tr > td.action {
			min-width: 14.5em;
		}
	}
	.i_file_add::after {
		content: "\\f319";
		font-weight: normal;
	}
`;
document.head.appendChild(style);


////////////
// MyList //
////////////

const animelist = document.getElementById("animelist");
if (animelist !== null) {

	for (const row of animelist.querySelectorAll("#animelist>tbody>tr")) {
		const eps = row.getElementsByClassName("eps")[0].textContent.trim();
		const epsParsed = /([0-9]+)\/([0-9]+)/.exec(eps);
		if (epsParsed !== null && epsParsed[1] === epsParsed[2]) {
			 continue;
		}
		const aid = row.dataset.anidbAid;
		const action = row.getElementsByClassName("action")[0];
		const button = document.createElement("a");
		button.className = "i_icon i_file_add";
		button.title = "add a file for the next episode";
		button.innerHTML = "<span>add file</span>";
		button.addEventListener("click", addFileByAid.bind(null, aid));
		button.addEventListener("auxclick", addFileByAid.bind(null, aid));
		button.addEventListener("mousedown", preventAutoScroll);
		action.appendChild(button);
	}

	function addFileByAid(aid, event) {
		if (!(event.button === 0 || event.button === 1)) {
			return;
		}
		const newTab = event.button === 1 || event.ctrlKey || event.shiftKey;

		const xhr = new XMLHttpRequest();
		xhr.responseType = "document";
		xhr.open("GET", "https://anidb.net/perl-bin/animedb.pl?show=anime&aid=" + aid);
		xhr.addEventListener("load", addFileXhrCallback.bind(null, xhr, newTab));
		xhr.addEventListener("error", xhrErrorCallback.bind(null, xhr));
		xhr.send();

		let loading = document.getElementById("loading");
		if (loading === null) {
			loading = document.createElement("div");
			loading.id = "loading";
			document.body.appendChild(loading);
		}
		loading.classList.add("active");

		event.preventDefault();
	}

	function addFileXhrCallback(xhr, newTab, event) {
		const loading = document.getElementById("loading");
		if (loading !== null) {
			document.body.removeChild(loading);
		}

		if (xhr.status !== 200) {
			window.alert("Received \u201C" + xhr.status + " " + xhr.statusText + "\u201D response when loading episode list.");
			return;
		}

		let lastEpAdded = true;
		let nextEpRow = null;
		for (const row of xhr.response.querySelectorAll("#eplist>tbody>tr")) {
			const epNumber = row.getElementsByClassName("eid")[0].textContent.trim();
			if (!("0" <= epNumber[0] && epNumber[0] <= "9")) {
				 break;
			}
			const isAdded = row.getElementsByClassName("i_general_edit").length !== 0;
			if (!isAdded && lastEpAdded) {
				nextEpRow = row;
			}
			lastEpAdded = isAdded;
		}
		if (lastEpAdded === true) {
			window.alert("The last episode is in your MyList.");
			return;
		}
		const eid = nextEpRow.dataset.anidbEid;
		const uri = "https://anidb.net/perl-bin/animedb.pl?show=addfile&eid=" + eid;
		if (!newTab || window.open(uri) === null) {
			location.assign(uri);
		}
	}

	function xhrErrorCallback() {
		const loading = document.getElementById("loading");
		if (loading !== null) {
			document.body.removeChild(loading);
		}
		alert("Failed to load episodes list.");
	}

	function preventAutoScroll(event) {
		if (event.button === 1) {
			event.preventDefault();
		}
	}

}


////////////////
// Anime page //
////////////////

const eplist = document.getElementById("eplist");
if (eplist !== null) {
	for (const row of eplist.querySelectorAll("tbody>tr")) {
		const eid = row.dataset.anidbEid;
		const action = row.querySelector(".action.episode");
		const button = document.createElement("a");
		button.className = "i_icon i_file_add";
		button.title = "add a new file";
		button.innerHTML = "<span>add new file</span>";
		button.href = "https://anidb.net/perl-bin/animedb.pl?show=addfile&eid=" + eid;
		action.insertBefore(button, action.firstElementChild);
	}
}

QingJ © 2025

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