HH Unlock Images

This script unlocks images of both recruited girls and non-recruited girls.

// ==UserScript==
// @name         HH Unlock Images
// @namespace    hhUnlock
// @version      1.01
// @description  This script unlocks images of both recruited girls and non-recruited girls.
// @author       YanDee
// @match        *://nutaku.haremheroes.com/harem*
// @match        *://*.hentaiheroes.com/harem*
// @match        *://*.gayharem.com/harem*
// @grant        unsafeWindow
// ==/UserScript==

(function() {
    if (document.visibilityState == 'hidden'){
        return;
    };
    var curGID, curImg, totalIncome, incomeNode, vanilNode, GT = unsafeWindow.GT;
    var girlsDataList = unsafeWindow.girlsDataList, number_format_lang = unsafeWindow.number_format_lang, number_reduce = unsafeWindow.number_reduce;
    var toggleMode = function(){
        sessionStorage.haremLevDispMode = (sessionStorage.haremLevDispMode != 'income') ? 'income' : 'default';
        applyMode();
    };
    var applyMode = function(){
        $('div.girls_levels > *').remove();
        if (sessionStorage.haremLevDispMode != 'default'){
            $('div.girls_levels').append(incomeNode);
            $('div.girls_levels > p:has(br)').css('margin', 0);
            return 'income';
        } else {
            $('div.girls_levels').append(vanilNode);
        };
    };
    var treatment = function(){
        curGID = $('#harem_right div.opened').attr('girl');
        if (girlsDataList[curGID].own){
            curImg = (girlsDataList[curGID].fav_graded >= 0) ? girlsDataList[curGID].fav_graded : girlsDataList[curGID].graded;
            $('#harem_right div.diamond:not([grade="' + curImg + '"])').on('click.hhUnlock', clickHandler);
            $('#harem_right div.diamond.locked').css('cursor', 'pointer');
            var totIn = (totalIncome >= 1e6) ? number_reduce(totalIncome) : number_format_lang(totalIncome);
            var totDayIn = (24 * totalIncome >= 1e6) ? number_reduce(24 * totalIncome) : number_format_lang(24 * totalIncome);
            incomeNode = '<span class="hudSC_mix_icn"></span><p><span class="pompous_text">' + GT.design.haremdex_income + ': </span></p>';
            incomeNode += '<p><span class="focus_text" hh_title="' + number_format_lang(totalIncome) + ' / ' + GT.time.h + '" tooltip-id="tooltip_1"> ';
            incomeNode += totIn + ' / ' + GT.time.h + '</span><br><span class="focus_text" hh_title="' + number_format_lang(24 * totalIncome);
            incomeNode += ' / ' + GT.time.d + '" tooltip-id="tooltip_1">' + totDayIn + ' / ' + GT.time.d + '</span></p>';
            vanilNode = $('div.girls_levels > *').clone();
            $('div.girls_levels').on('click', toggleMode).css('cursor', 'pointer');
            sessionStorage.haremLevDispMode = (sessionStorage.haremLevDispMode != 'default') ? applyMode() : 'default';
        } else {
            curImg = 0;
            $('#harem_right img.avatar').css('filter', 'none');
            replaceAva(0);
            $('#harem_right div.middle_part.missing_girl').css('margin-top', '0');
            $('#harem_right img.avatar').before(getHeader()).after(getDiamondBar());
            $('#harem_right div.diamond.locked').on('click.hhUnlock', clickHandler);
        };
    };
    var clickHandler = function(){
        var nextSel = $(this);
        if (nextSel.attr('grade') != curImg){
            if (girlsDataList[curGID].own){
                curImg = nextSel.attr('grade');
                $('#harem_right div.diamond').off('click.hhUnlock');
                var favImg = (girlsDataList[curGID].fav_graded >= 0) ? girlsDataList[curGID].fav_graded : girlsDataList[curGID].graded;
                if (curImg > girlsDataList[curGID].graded || curImg == favImg){
                    replaceAva(curImg);
                };
                $('#harem_right div.diamond').off('click.hhUnlock');
                $('#harem_right div.diamond:not([grade="' + curImg + '"])').on('click.hhUnlock', clickHandler).css('cursor', 'pointer');
                nextSel.css('cursor', 'default');
            } else {
                $('#harem_right div.diamond[grade="' + curImg + '"]').removeClass('selected').addClass('locked');
                curImg = nextSel.attr('grade');
                replaceAva(curImg);
                $(this).removeClass('locked').addClass('selected');
                $('#harem_right div.diamond.locked').css('cursor', 'pointer');
                $('#harem_right div.diamond.selected').css('cursor', 'default');
                $('#harem_right div.diamond').off('click.hhUnlock');
                $('#harem_right div.diamond.locked').on('click.hhUnlock', clickHandler);
            };
        };
    };
    var getDiamondBar = function(){
        var body = '<div class="diamond-bar"><div class="diamond selected" style="cursor:default" grade="0"></div>';
        for (var i = 1; i <= (girlsDataList[curGID].Graded2.match(/<g[^>]*>[^<>]*<[^>]*\/g>/gi) || []).length; i++) {
            body += '<div class="diamond locked" style="cursor:pointer" grade="' + i + '"></div>';
        };
        body += '</div>';
        return body;
    };
    var getHeader = function(){
        var g_name = (girlsDataList[curGID].ref.full_name != undefined) ? girlsDataList[curGID].ref.full_name : girlsDataList[curGID].Name
        var g_rar = girlsDataList[curGID].rarity;
        var g_class = $('div.girls_list div[id_girl="' + curGID + '"] div.icon').attr('carac');
        var body = '<h3>' + g_name + '<span carac="class' + g_class + '"></span></h3>';
        body += '<p class="girl-rarity ' + g_rar + '"> ' + GT.design["girls_rarity_" + g_rar] + '</p>';
        return body;
    };
    var replaceAva = function(num){
        $('#harem_right img.avatar').attr('src', (i, src) => src.replace(/av(a|b)\d/g, 'ava' + num));
    };
    var calcTotalIncome = function() {
        totalIncome = 0;
        for (var id in girlsDataList) {
            if(girlsDataList[id].own){
                totalIncome += Math.round(girlsDataList[id].salary_per_hour);
            };
        };
    };

    var watcher = new MutationObserver(function(){
        treatment();
    });
    watcher.observe(document.querySelector('#harem_right'), {childList: true});
    calcTotalIncome();
    treatment();
})();