Version 2.0

- "intelligent" AnimePahe search logic
- independent design
- search Animes without year in name
This commit is contained in:
2021-07-28 00:48:39 +02:00
parent d82d6ccc35
commit 285d9d031a

View File

@@ -1,7 +1,7 @@
// ==UserScript== // ==UserScript==
// @name aniSearch Enhancer // @name aniSearch Enhancer
// @namespace http://git.nivram.io/marvinlehmann/ani-search-enhancer // @namespace http://git.nivram.io/marvinlehmann/ani-search-enhancer
// @version 1.90 // @version 2.0
// @description Adds buttons to the info site of an anime on aniSearch which links you directly to various streaming portals. // @description Adds buttons to the info site of an anime on aniSearch which links you directly to various streaming portals.
// @author Marvin Lehmann // @author Marvin Lehmann
// @grant GM_xmlhttpRequest // @grant GM_xmlhttpRequest
@@ -20,8 +20,16 @@
content:"\\f002"; content:"\\f002";
} }
#page-action-stream {
background-color: #ee8600 !important;
}
#page-action-stream:hover { #page-action-stream:hover {
background-color: #58626b !important; background-color: #ee8600 !important;
}
#page-action-stream span {
line-height: 26px !important;
} }
#stream-links { #stream-links {
@@ -29,23 +37,70 @@
-webkit-appearance: none; -webkit-appearance: none;
appearance: none; appearance: none;
border: none; border: none;
background-color: #ee8600;
background-color: #58626b;
color: #e6e6e6; color: #e6e6e6;
box-shadow: none; box-shadow: none;
padding: 0 5px;
} }
#stream-links > option { #stream-links > option {
direction: ltr; direction: ltr;
font-weight: bold;
} }
</style>`); </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; const animeTitle = document.getElementById("htitle").textContent;
const animeReleaseYear = document.querySelector("header .release_year").textContent.match(/\d+/); /** @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) { if (animeTitle) {
const encodedTitle = encodeURI(animeTitle).replace(/'/g, "%27"); const encodedTitle = encodeURI(animeTitle).replace(/'/g, "%27");
const encodedTitleWithoutYear = encodeURI(animeTitle.replace(/ \(\d{4}\)/g, "")).replace(/'/g, "%27");
if (!document.getElementById("page-action")) { if (!document.getElementById("page-action")) {
document.getElementById("htitle").insertAdjacentHTML("beforeend", /*html*/ `<ul id="page-action" class="inline"></ul>`); document.getElementById("htitle").insertAdjacentHTML("beforeend", /*html*/ `<ul id="page-action" class="inline"></ul>`);
} }
@@ -79,28 +134,64 @@
return option; return option;
} }
stream_links.insertAdjacentElement("beforeend", stylized(new Option("Piracy Index", "https://piracy.moe/"), { direction: "rtl", fontWeight: "bold" })); stream_links.insertAdjacentElement("beforeend", stylized(new Option("Piracy Index", "https://piracy.moe/"), {
stream_links.insertAdjacentElement("beforeend", new Option("AniCloud", "https://anicloud.io/search?q=" + encodedTitle)); direction: "rtl",
stream_links.insertAdjacentElement("beforeend", new Option("AnimeUltima", "https://www16.animeultima.eu/search?search=" + encodedTitle)); fontWeight: "bold"
stream_links.insertAdjacentElement("beforeend", stylized(new Option("Twist.moe", "http://twist.moe"), { fontStyle: "italic", color: "lightgray" })); }));
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({ GM_xmlhttpRequest({
method: "GET", method: "GET",
url: "https://animepahe.com/api?m=search&l=3&q=" + encodedTitle, url: "https://animepahe.com/api?m=search&l=8&q=" + encodedTitleWithoutYear,
responseType: "json", responseType: "json",
onload: function (resp) { onload: function (resp) {
if (resp.response.total > 0) { const typeMapping = {
const firstResult = resp.response.data[0]; "TV-Serie": "TV",
const targetPage = "https://animepahe.com/anime/" + firstResult.session; "OVA": "OVA",
stream_links.insertAdjacentElement("beforeend", new Option("AnimePahe", targetPage)); "Film": "Movie",
"Bonus": "Special"
};
// Did we possibly get the wrong one? const statusMapping = {
// if (!firstResult.started_airing_date.includes(animeReleaseYear)) { "Abgeschlossen": "Finished Airing",
// $masteraniButton.prop('title', 'Target page may be incorrect.'); "Laufend": "Currently Airing",
// $masteraniButton.css('opacity', 0.5); "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 { } else {
stream_links.insertAdjacentElement("beforeend", stylized(new Option("AnimePahe", "https://animepahe.com"), { fontStyle: "italic", color: "gray" })); stream_links.insertAdjacentElement("beforeend", stylized(new Option("AnimePahe", "https://animepahe.com"), {
fontStyle: "italic",
color: "#DDD"
}));
} }
} }
}); });