Files
ani-search-enhancer/ani-search-enhancer.user.js
marvinlehmann 285d9d031a Version 2.0
- "intelligent" AnimePahe search logic
- independent design
- search Animes without year in name
2021-07-28 00:48:39 +02:00

199 lines
7.5 KiB
JavaScript

// ==UserScript==
// @name aniSearch Enhancer
// @namespace http://git.nivram.io/marvinlehmann/ani-search-enhancer
// @version 2.0
// @description Adds buttons to the info site of an anime on aniSearch which links you directly to various streaming portals.
// @author Marvin Lehmann
// @grant GM_xmlhttpRequest
// @include /^https?://www.anisearch.de/anime/.*$/
// @connect animepahe.com
// @connect twist.moe
// @downloadURL http://git.nivram.io/marvinlehmann/ani-search-enhancer/raw/master/ani-search-enhancer.user.js
// @supportURL http://git.nivram.io/marvinlehmann/ani-search-enhancer/issues
// ==/UserScript==
(function () {
'use strict';
document.head.insertAdjacentHTML("beforeend", //html
`<style>
#page-action-stream:before {
content:"\\f002";
}
#page-action-stream {
background-color: #ee8600 !important;
}
#page-action-stream:hover {
background-color: #ee8600 !important;
}
#page-action-stream span {
line-height: 26px !important;
}
#stream-links {
-moz-appearance: none;
-webkit-appearance: none;
appearance: none;
border: none;
background-color: #ee8600;
color: #e6e6e6;
box-shadow: none;
padding: 0 5px;
}
#stream-links > option {
direction: ltr;
font-weight: bold;
}
</style>`);
class AnimePaheSearchResult {
constructor() {
/** @type {number} */
this.episodes = 0;
/** @type {number} */
this.id = 0;
/** @type {string} */
this.poster = "";
/** @type {string} */
this.relevance = "";
/** @type {number} */
this.score = 0;
/** @type {string} */
this.season = "";
/** @type {string} */
this.slug = "";
/** @type {string} */
this.session = "";
/** @type {string} */
this.status = "";
/** @type {string} */
this.title = "";
/** @type {string} */
this.type = "";
/** @type {number} */
this.year = 0;
}
}
function getElementByXpath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
const animeTitle = document.getElementById("htitle").textContent;
/** @type {number} */
let animeReleaseYear = null;
let anime_release_year = document.querySelector("header .release_year");
if (anime_release_year) {
animeReleaseYear = parseInt(anime_release_year.textContent.match(/\d{4}/));
} else {
let tmp = document.getElementById("htitle").textContent.match(/\((\d{4})\)/);
if (tmp) {
animeReleaseYear = parseInt(tmp[1]);
}
}
let animeStatus = document.querySelector('#information [lang=ja] ~ .status').textContent.split(": ")[1];
let animeType = getElementByXpath('//*[@id="infodetails"]//span[text()="Typ"]/../text()').textContent;
if (animeTitle) {
const encodedTitle = encodeURI(animeTitle).replace(/'/g, "%27");
const encodedTitleWithoutYear = encodeURI(animeTitle.replace(/ \(\d{4}\)/g, "")).replace(/'/g, "%27");
if (!document.getElementById("page-action")) {
document.getElementById("htitle").insertAdjacentHTML("beforeend", /*html*/ `<ul id="page-action" class="inline"></ul>`);
}
document.getElementById("page-action").insertAdjacentHTML("beforeend", //html
`<li id="page-action-stream">
<span>
<select id="stream-links">
<option value="">Stream wählen</option>
</select>
</span>
</li>`);
const stream_links = document.getElementById("stream-links");
const page_action_stream = document.getElementById("page-action-stream");
const clickHandler = function (e) {
if (e.target === e.currentTarget) {
const targetUrl = stream_links.value;
if (targetUrl) {
window.open(targetUrl, "_blank");
}
}
};
page_action_stream.onclick = clickHandler;
/** @param {HTMLOptionElement} option */
const stylized = (option, styles) => {
Object.assign(option.style, styles);
return option;
}
stream_links.insertAdjacentElement("beforeend", stylized(new Option("Piracy Index", "https://piracy.moe/"), {
direction: "rtl",
fontWeight: "bold"
}));
stream_links.insertAdjacentElement("beforeend", new Option("AniCloud", "https://anicloud.io/search?q=" + encodedTitleWithoutYear));
stream_links.insertAdjacentElement("beforeend", new Option("AnimeUltima", "https://www16.animeultima.eu/search?search=" + encodedTitleWithoutYear));
stream_links.insertAdjacentElement("beforeend", stylized(new Option("Twist.moe", "http://twist.moe"), {
fontStyle: "italic",
color: "#DDD"
}));
GM_xmlhttpRequest({
method: "GET",
url: "https://animepahe.com/api?m=search&l=8&q=" + encodedTitleWithoutYear,
responseType: "json",
onload: function (resp) {
const typeMapping = {
"TV-Serie": "TV",
"OVA": "OVA",
"Film": "Movie",
"Bonus": "Special"
};
const statusMapping = {
"Abgeschlossen": "Finished Airing",
"Laufend": "Currently Airing",
"Zukünftig": ""
}
if (resp.response.total > 0) {
/** @type {AnimePaheSearchResult[]} */
const data = resp.response.data;
/** @type {AnimePaheSearchResult} */
let chosenResult = data[0];
/** @type {number[]} */
let potential = Array(data.length);
for (let [index, value] of data.entries()) {
potential[index] = 0;
if (value.year === animeReleaseYear || (value.season === "Winter" && value.year - 1 === animeReleaseYear)) {
potential[index]++;
}
if (value.type === typeMapping[animeType]) {
potential[index] += 2;
}
if (value.status === statusMapping[animeStatus]) {
potential[index]++;
}
}
chosenResult = data[potential.indexOf(Math.max(...potential))];
const targetPage = "https://animepahe.com/anime/" + chosenResult.session;
stream_links.insertAdjacentElement("beforeend", new Option("AnimePahe", targetPage));
} else {
stream_links.insertAdjacentElement("beforeend", stylized(new Option("AnimePahe", "https://animepahe.com"), {
fontStyle: "italic",
color: "#DDD"
}));
}
}
});
}
})();