ctr / Kamihime Project R - Display enemy hp

// ==UserScript==
// @name         Kamihime Project R - Display enemy hp
// @description  Displays enemy hp
// @license      MIT
// @match        https://cf.r.kamihimeproject.dmmgames.com/front/cocos2d-proj/components-pc/game/app.html
// @run-at       document-start
// ==/UserScript==

(function() {

    var interval = setInterval(function() {
        if (typeof kh !== 'undefined' && kh.Enemy && kh.EnemyStatusBar && kh.BattleWorld) {
            clearInterval(interval);

            var orig_ctor = kh.EnemyStatusBar.prototype.ctor;

            kh.EnemyStatusBar.prototype.ctor = function new_ctor(t, n, a, i) {
                orig_ctor.apply(this, new_ctor.arguments);
                var hp_text = this.ui.getChildByName("hp_text") || new ccui.Text();
                hp_text.setName("hp_text");
                hp_text.setFontSize(10);
                hp_text.enableOutline(cc.color.BLACK, 1);
                hp_text.setPosition([234, 145, 145][a-1], 42);
                this.ui.addChild(hp_text);
            }

            var formatNumber = function(num) {
                return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
            }

            var updateHPText = function(e) {
                ccui.helper.seekWidgetByName(e.statusPanel.ui, "hp_text").setText(formatNumber(e.hp) + "/" + formatNumber(e.maxHp));
            }

            kh.Enemy.prototype.adjustHP = function(t) {
                kh.Avatar.prototype.adjustHP.call(this, t);
                updateHPText(this);
            }

            var orig_start = kh.BattleWorld.prototype._start;

            kh.BattleWorld.prototype._start = async function new_start(sceneInstanceId) {
                this.enemyList.filter(t => t && Object.entries(t).length).forEach(updateHPText);
                return orig_start.apply(this, [sceneInstanceId]);
            }

        }
    }, 10);

})();