eileen12 / Anilist Bugfix

// ==UserScript==
// @name         Anilist Bugfix
// @namespace    anilist_scripts
// @version      0.21
// @description  fix people's profiles!
// @author       eileen12
// @match        http*://*anilist.co/user/*
// @grant        none
// @copyright    2020, eileen12 (https://openuserjs.org/users/eileen12)
// @license      MIT
// @updateURL    https://openuserjs.org/meta/eileen12/Anilist_Bugfix.meta.js
// ==/UserScript==

var fix = (function() {
    'use strict';
    if(/anilist.co\/user/g.test(document.URL))
    {
        console.debug("Fixing profile page.")
        // Fix profile actions dropdown
        waitCondition(500, () => document.querySelector(".user .actions .dropdown") !== null, 5000)
        .then(()=>{
            document.querySelector(".user .el-icon-arrow-down").style["line-height"] = "unset"
            var dd = document.querySelector(".user .actions .dropdown.el-dropdown")
            dd.style["padding-right"] = "0px"
            dd.style.top = "1px"
        })
    }
    else
    {
        // Nothing to fix here.
        console.debug("Nothing to fix here.")
        return;
    }
});


// https://stackoverflow.com/questions/6390341/how-to-detect-if-url-has-changed-after-hash-in-javascript
function inject_location_listeners()
{
    history.pushState = ( f => function pushState(){
        var ret = f.apply(this, arguments);
        window.dispatchEvent(new Event('pushstate'));
        window.dispatchEvent(new Event('locationchange'));
        return ret;
    })(history.pushState);

    history.replaceState = ( f => function replaceState(){
        var ret = f.apply(this, arguments);
        window.dispatchEvent(new Event('replacestate'));
        window.dispatchEvent(new Event('locationchange'));
        return ret;
    })(history.replaceState);

    window.addEventListener('popstate',()=>{
        window.dispatchEvent(new Event('locationchange'))
    });
}

var waitCondition = (checkDelayMilli, conditionFunc, timeoutMilli=1000) => new Promise(
    (resolve, reject) => {
        var flag = false
        var temp_loop = ()=>{
            if(flag==true) return;
            else if(conditionFunc()==true)
            {
                flag = true
                resolve()
            }
            else{
                setTimeout(temp_loop, checkDelayMilli)
            }
        }
        setTimeout(()=>{if(flag==true)return;flag=true;reject("Wait condition timed out.");},timeoutMilli)
        temp_loop()
    }
)

waitCondition(500, ()=> document.readyState === 'complete', 5000)
    .then(fix)
    .then(inject_location_listeners)
    .then(() => window.addEventListener('locationchange', fix))
    .catch(err=>console.error(err))