// ==UserScript==
// @name Better Buttons To Edit Anime/Manga Information
// @namespace Better Buttons To Edit Anime Information
// @version 14
// @description The script removes the actual "Edit Anime/Manga Information" drop down menu, and replaces that with Better Buttons To edit the Anime/Manga information.
// @author hacker09
// @include /^https:\/\/myanimelist\.net\/((anime|manga)(id=)?(\.php\?id=)?)(\/)?([\d]+)/
// @icon https://t3.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=http://myanimelist.net&size=64
// @run-at document-end
// @grant none
// @downloadURL https://update.greasyfork.icu/scripts/407729/Better%20Buttons%20To%20Edit%20AnimeManga%20Information.user.js
// @updateURL https://update.greasyfork.icu/scripts/407729/Better%20Buttons%20To%20Edit%20AnimeManga%20Information.meta.js
// ==/UserScript==
(async function() {
var MarginLeft = -7; //Creates a new variable
const $ = window.jQuery; //Defines That The Symbol $ Is A jQuery
const id = location.pathname.match(/\d+/)[0]; //Get the entry id
var type, remove, sourceortype, airingdatesorpubdates, ratingorchapvol; //Create new variables
const CSSPart = 'height: 10px; width: 10px; background-size: cover; display: inline-block; transform: scale(1.8); vertical-align: top; margin-top: -10px;'; //Store parts of the CSS codes
location.pathname.split('/')[1] === 'anime' ? (type = 'aid', remove = 'anime', sourceortype = 'source', airingdatesorpubdates = 'airingdates', ratingorchapvol = 'rating') : (type = 'mid', remove = 'manga', sourceortype = 'type', airingdatesorpubdates = 'pubdates', ratingorchapvol = 'chapvol'); //Set up the variables
document.getElementsByClassName('js-' + remove + '-edit-info-button')[0].remove(); //Remove the default Edit Anime/Manga Information Button
const BroadCastButton = '`; //Create a BTN const
const ProducersButton = '`; //Create a BTN const
const RatingButton = '`; //Create a BTN const
const RelationsButton = '`; //Create a BTN const
const SourceButton = '`; //Create a BTN const
const AiringDatesButton = '`; //Create a BTN const
const BackgroundButton = '`; //Create a BTN const
const AlternativeTitlesButton = '`; //Create a BTN const
const SynopsisButton = '`; //Create a BTN const
const PictureButton = '`; //Create a BTN const
const DurationButton = '`; //Create a BTN const
const Infoh2Elem = [...[...document.querySelectorAll("h2")].find(h2 => h2.textContent === "Information").parentNode.querySelectorAll("h2")].find(info => info.outerHTML.includes("Information")); //Save the information div element
Infoh2Elem.insertAdjacentHTML("afterbegin", '
'); //Add a space before the Information h2 header
const BTNsArray = [BroadCastButton, ProducersButton, RatingButton, RelationsButton, SourceButton, AiringDatesButton, BackgroundButton, AlternativeTitlesButton, SynopsisButton, PictureButton, DurationButton]; //Creates an array of BTNs
BTNsArray.forEach(function(BTNs) { //ForEach button in the array
document.querySelector('.header-right').parentElement.insertAdjacentHTML("beforeend", BTNs); //Add the BTNs on the page
Infoh2Elem.insertAdjacentHTML("afterbegin", BTNs); //Add the BTNs on the page
}); //Finishes the ForEach function
if (type === 'mid') //If the user is on a manga entry
{ //Starts the if condition
MarginLeft = 5; //Centralize the icons
document.querySelectorAll("#SourceButton").forEach(BTN => BTN.setAttribute("title", "Edit Manga Type")); //Change txt on mouse hover
document.querySelectorAll("#PictureButton").forEach(BTN => BTN.setAttribute("title", "Edit Manga Picture")); //Change txt on mouse hover
document.querySelectorAll("#SynopsisButton").forEach(BTN => BTN.setAttribute("title", "Edit Manga Synopsis")); //Change txt on mouse hover
document.querySelectorAll("#RelationsButton").forEach(BTN => BTN.setAttribute("title", "Edit Manga Relations")); //Change txt on mouse hover
document.querySelectorAll("#BackgroundButton").forEach(BTN => BTN.setAttribute("title", "Edit Manga Background")); //Change txt on mouse hover
document.querySelectorAll("#AiringDatesButton").forEach(BTN => BTN.setAttribute("title", "Edit Manga Publishing Dates")); //Change txt on mouse hover
document.querySelectorAll("#RatingButton").forEach(BTN => BTN.setAttribute("title", "Edit Manga Chapters/Volumes")); //Change txt on mouse hover
document.querySelectorAll("#RatingButton").forEach(BTN => BTN.style.backgroundImage = `url(${document.querySelector(".dark-mode") !== null ? 'https://i.imgur.com/UoGgWiw.png' : 'https://i.imgur.com/py5QNtd.png'}`); //Change rating BTN IMG
document.querySelectorAll("#AlternativeTitlesButton").forEach(BTN => BTN.setAttribute("title", "Edit Manga Alternative Titles")); //Change txt on mouse hover
document.querySelectorAll("#BroadCastButton,#ProducersButton,#DurationButton").forEach(BTN => BTN.setAttribute("style", "display: none !important;")); //Hide BTNs
} //Finishes the if condition
document.querySelectorAll(".BetterBTN").forEach(function(BTNs, i) { //ForEach Information BTNs
if (i > 10) { //Only for the Information BTNs
BTNs.style.marginLeft = MarginLeft + 'px'; //Gradualy increase the left margin
BTNs.style.display = 'block'; //Change the display mode
MarginLeft += 22; //Increase the left margin
} //Finishes the if condition
}); //Finishes the ForEach loop
function Format(el, href) { //Creates a function to add the link elements
if ($(el).length > 0) { //If the element exists
if ($(el).parent().html().match(/span>\s+?None\sfound,/)) //If the element is none found
{ //Starts the if condition
$(el).parent().html($(el).parent().html().replace(/span>\s+?None\sfound,[\s\S]+?$/, 'span> Unknown')); //Add a none found span element
} //Finishes the if condition
var text = $('span', $(el).parent()).first().text().replace(/:$/, ''); //Save the element text to an array
$(el).empty().append(`${text}`).append(':'); //Add a link to the element and the text for the element
} //Finishes the if condition
} //Finishes the Format function
if ($('#addtolist ~ h2:contains(Alternative Titles)').length > 0) { //If the Alternative Titles element exists
$('#addtolist ~ h2:contains(Alternative Titles)').prepend(''); //Add an edit button next to it
} //Finishes the if condition
if (type === 'aid') { //If the entry is an anime
Format('span.dark_text:contains(Aired:)', '/dbchanges.php?aid=' + id + '&t=airingdates'); //Call the Format function
Format('span.dark_text:contains(Broadcast:)', '/dbchanges.php?aid=' + id + '&t=broadcast'); //Call the Format function
Format('span.dark_text:contains(Producers:)', '/dbchanges.php?aid=' + id + '&t=producers'); //Call the Format function
Format('span.dark_text:contains(Licensors:)', '/dbchanges.php?aid=' + id + '&t=producers'); //Call the Format function
Format('span.dark_text:contains(Studios:)', '/dbchanges.php?aid=' + id + '&t=producers'); //Call the Format function
Format('span.dark_text:contains(Source:)', '/dbchanges.php?aid=' + id + '&t=source'); //Call the Format function
Format('span.dark_text:contains(Duration:)', '/dbchanges.php?aid=' + id + '&t=duration'); //Call the Format function
Format('span.dark_text:contains(Rating:)', '/dbchanges.php?aid=' + id + '&t=rating'); //Call the Format function
} else { //If the entry is manga
Format('span.dark_text:contains(Type:)', '/dbchanges.php?mid=' + id + '&t=type'); //Call the Format function
Format('span.dark_text:contains(Volumes:)', '/dbchanges.php?mid=' + id + '&t=chapvol'); //Call the Format function
Format('span.dark_text:contains(Chapters:)', '/dbchanges.php?mid=' + id + '&t=chapvol'); //Call the Format function
Format('span.dark_text:contains(Published:)', '/dbchanges.php?mid=' + id + '&t=pubdates'); //Call the Format function
} //Finishes the else condition
})();