Cpt_mathix / MyAnimeList(MAL) - Last updates "Edit Button"

// ==UserScript==
// @name         MyAnimeList(MAL) - Last updates "Edit Button"
// @version      1.3.0
// @description  Brings the edit button back to your profile
// @author       Cpt_mathix
// @include      *://myanimelist.net/profile*
// @exclude      *://myanimelist.net/profile/*/reviews*
// @exclude      *://myanimelist.net/profile/*/recommendations*
// @exclude      *://myanimelist.net/profile/*/clubs*
// @exclude      *://myanimelist.net/profile/*/friends*
// @run-at       document-start
// @licence      GPL-2.0+; http://www.gnu.org/licenses/gpl-2.0.txt
// @grant        none
// @namespace https://greasyfork.org/users/16080
// ==/UserScript==

window.addEventListener('load', function() {
    var you = document.getElementsByClassName('header-profile-link')[0].textContent;
    var yourAnimelist = getUserList(you, "anime");
    var yourMangalist = getUserList(you, "manga");
    if (yourAnimelist !== false || yourMangalist !== false)
        createEdit(you, yourAnimelist, yourMangalist);

    if (typeof jQuery == 'undefined') $ = unsafeWindow.$;

    $('.lightbox').fancybox({
        'width'			: 700,
        'height'		: '85%',
        'overlayShow'	: false,
        'titleShow'     : false,
        'type'          : 'iframe'
    });
}, false);

function createEdit(you, yourAnimelist, yourMangalist) {
    var data = document.getElementsByClassName("statistics-updates");
    for(var i = 0; i < data.length; i++){
        var itemUrl = data[i].getElementsByTagName('a');
        var id = itemUrl[0].href.match(/\d+/)[0];

        var edit = document.createElement('div');
        var title = itemUrl[1];
        title.parentNode.insertBefore(edit, title.parentNode.childNodes[2]);
        title.className += 'pr4';

        var url;
        if (data[i].parentNode.className.indexOf('manga') == -1) {
            if (checkAnimeInYourList(yourAnimelist, id)) {
                url = '"/editlist.php?type=anime&amp;id=' + id + '&hideLayout"';
                edit.outerHTML = '<a href=' + url + ' title="Edit this entry" class="lightbox button_edit">edit</a>';
            } else {
                url = '"/panel.php?go=add&selected_series_id=' + id + '&hideLayout"';
                edit.outerHTML = '<a href=' + url + ' title="Add this entry" class="lightbox button_add">add</a>';
            }
        } else {
            if (checkMangaInYourList(yourMangalist, id)) {
                url = '"/panel.php?go=editmanga&id=' + id + '&hideLayout"';
                edit.outerHTML = '<a href=' + url + ' title="Edit this entry" class="lightbox button_edit">edit</a>';
            } else {
                url = '"/panel.php?go=addmanga&selected_manga_id=' + id + '&hideLayout"';
                edit.outerHTML = '<a href=' + url + ' title="Add this entry" class="lightbox button_add">add</a>';
            }
        }
    }
}

function getUserList(user, type) {
    var xhr = new XMLHttpRequest();
    var url = '/malappinfo.php?u=' + user + '&status=all&type=' + type;
    xhr.open("GET", url, false);
    xhr.setRequestHeader('Content-Type', 'text/xml');
    xhr.send();
    var xmlDocument = xhr.responseXML;
    if (xmlDocument !== null) {
        return xmlDocument;
    } else {
        return false;
    }
}

function checkMangaInYourList(list, id) {
    var entry = list.getElementsByTagName('manga');
    var entryid = list.getElementsByTagName('series_mangadb_id');
    for(var k = 0; k < entryid.length; k++) {
        if (entryid[k].textContent == id) {
            return true;
        }
    }
    return false;
}

function checkAnimeInYourList(list, id) {
    var entry = list.getElementsByTagName('anime');
    var entryid = list.getElementsByTagName('series_animedb_id');
    for(var k = 0; k < entryid.length; k++) {
        if (entryid[k].textContent == id) {
            return true;
        }
    }
    return false;
}