sp1ti / Myanimelist.net links on trakt.tv

// ==UserScript==
// @name         Myanimelist.net links on trakt.tv
// @namespace    https://txm.huere.net
// @version      1.7.5.201810
// @description  Adds MAL backlinks on trakt.tv based on a mapping table (TxM).
// @author       sp1ti
// @supportURL   mailto:trakt.x.mal@gmail.com
// @icon         https://txm.huere.net/res/favicon/android-chrome-36x36.png
// @updateURL    https://openuserjs.org/meta/sp1ti/Myanimelist.net_links_on_trakt.tv.meta.js
// @license      MIT
// @match        https://trakt.tv/movies/*
// @match        https://trakt.tv/shows/*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @require      https://git.io/vMmuf
// @connect      txm.huere.net
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @grant        GM_xmlhttpRequest
// ==/UserScript==
function trakt() {
    'use strict';
    var id = 0;
    var mal_id = 0;
    
    var show_loop = 0;
    var show_index_id = 0;

    // download show index
    if(GM_getValue("tv") !== "ok_fall18"){
        GM_xmlhttpRequest({
            method: "GET",
            url: "https://txm.huere.net/data/index_shows",
            onload: function(response) {
                var index_shows = response.responseText;
                var index_shows_a = index_shows.split(",");
                index_shows_a.forEach(function(item) {
                    if(show_loop%2===0){
                        show_index_id = item;
                    } else {
                        GM_setValue(show_index_id, item);
                        show_index_id = 0;
                    }
                    show_loop++;
                });
            }
        });
        GM_setValue("tv", "ok_fall18");
    }
    
    
    var movie_loop = 0;
    var movie_index_id = 0;
    
    // download movie index
    if(GM_getValue("mv") !== "ok_fall18"){
        GM_xmlhttpRequest({
            method: "GET",
            url: "https://txm.huere.net/data/index_movies",
            onload: function(response) {
                var index_movies = response.responseText;
                var index_movies_a = index_movies.split(",");
                index_movies_a.forEach(function(item) {
                    if(movie_loop%2===0){
                        movie_index_id = item;
                    } else {
                        GM_setValue(movie_index_id, item);
                        movie_index_id = 0;
                    }
                    movie_loop++;
                });
            }
        });
        GM_setValue("mv", "ok_fall18");
    }

    // add button tv shows
    if(window.location.href.indexOf("/shows/") > -1) {
        id = $(".summary-user-rating").attr("data-show-id");

        // determine season
        if(window.location.href.indexOf("seasons/1") > -1) {
            show_index_id = (id + '-1');
            mal_id = GM_getValue(show_index_id);
        } else if(window.location.href.indexOf("seasons/2") > -1) {
            show_index_id = (id + '-2');
            mal_id = GM_getValue(show_index_id);
        } else if(window.location.href.indexOf("seasons/3") > -1) {
            show_index_id = (id + '-3');
            mal_id = GM_getValue(show_index_id);
        } else if(window.location.href.indexOf("seasons/4") > -1) {
            show_index_id = (id + '-4');
            mal_id = GM_getValue(show_index_id);
        } else if(window.location.href.indexOf("seasons/5") > -1) {
            show_index_id = (id + '-5');
            mal_id = GM_getValue(show_index_id);
        } else if(window.location.href.indexOf("seasons/6") > -1) {
            show_index_id = (id + '-6');
            mal_id = GM_getValue(show_index_id);
        } else if(window.location.href.indexOf("seasons/7") > -1) {
            show_index_id = (id + '-7');
            mal_id = GM_getValue(show_index_id);
        } else if(window.location.href.indexOf("seasons/8") > -1) {
            show_index_id = (id + '-8');
            mal_id = GM_getValue(show_index_id);
        } else if(window.location.href.indexOf("seasons/9") > -1) {
            show_index_id = (id + '-9');
            mal_id = GM_getValue(show_index_id);
        } else {
            show_index_id = (id + '-1');
            mal_id = GM_getValue(show_index_id);
        }

        // display button
        if(mal_id !== undefined && mal_id !== "0"){
            $('.external li').append('<a href="https://myanimelist.net/anime/' + mal_id + '" target="_blank" data-original-title>Myanimelist</a>');
        }

    // add button movies
    } else {
        id = $(".summary-user-rating").attr("data-movie-id");
        mal_id = GM_getValue(id);

        // display button
        if(mal_id !== undefined && mal_id !== "0"){
            $('.external li').append('<a href="https://myanimelist.net/anime/' + mal_id + '" target="_blank" data-original-title>Myanimelist</a>');
        }
    }
}
waitForKeyElements("body", trakt);