// ==UserScript==
// @name ALink
// @namespace Morimasa
// @author Morimasa
// @description Adds AniList links to mangadex manga pages
// @match https://mangadex.org/title/*
// @match https://mangadex.org/manga/*
// @version 0.3
// @downloadURL none
// ==/UserScript==
const mal = document.querySelector('a[href^="https://myanimelist.net"]');
const title = document.getElementsByClassName("card-header")[0].childNodes[2].textContent;
let searched_title = false;
const handle = data => {
if (data["data"]["Media"])
document.getElementsByClassName("list-inline")[3].innerHTML+=`
AniList`;
else{
console.log(`ALink: ${data["errors"][0]["message"]}`)
if(!searched_title)
search()
}
}
const search = mal => {
let query = '';
let vars = {};
if (mal){
query = `query($mal:Int){Media(idMal:$mal,type:MANGA){id}}`;
vars = {"mal": parseInt(mal.href.split('/').pop())};
}
else{
const isOneshot = document.querySelector('.badge[href$="/genre/21"]')
query = `query($s:String){Media(search:$s,format:${isOneshot?'ONE_SHOT':'MANGA'},type:MANGA){id}}`;
vars = {"s": title};
searched_title = true
}
const fetch_options = {
method: 'POST',
body: JSON.stringify({query: query, variables: vars}),
headers: new Headers({
'Content-Type': 'application/json'
})
};
return fetch('https://graphql.anilist.co/', fetch_options)
.then(res => res.json())
.then(res => handle(res))
.catch(error => console.error(`ALink: Error - ${error}`));
}
search(mal)