Drrr.com Chat Room Language Displayer

Show drrr.com chat room language on longue so you don't need to hover on each room just to check the language.

// ==UserScript==
// @name         Drrr.com Chat Room Language Displayer
// @namespace    https://greasyfork.org/users/700963
// @version      1.0.4
// @description  Show drrr.com chat room language on longue so you don't need to hover on each room just to check the language.
// @author       eterNEETy
// @match        https://drrr.com/lounge*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=drrr.com
// @grant        none
// @license      MIT
// ==/UserScript==

var swap_span_pos = true;

// =========================================================

function doCallback(callback=false) {
    if (typeof callback == "function") {
        callback();
    }
}
function checkExist(query,qid=0,hidden_ok=false) {
    let el_exist = false;
    if (document.querySelectorAll(query).length > qid){
        if (hidden_ok) {
            el_exist = true;
        } else if (document.querySelectorAll(query)[qid].getBoundingClientRect().width > 0 && document.querySelectorAll(query)[qid].getBoundingClientRect().height > 0) {
            el_exist = true;
        }
    }
    return el_exist;
}
function checkEl(query,qid=0,callback=false,hidden_ok=false) {
    let old_top = -1;
    let old_left = -1;
    let loop_checkEl = setInterval(function() {
        console.log("checkEl: "+query+"["+qid+"]");
        if (checkExist(query,qid,hidden_ok)) {
            if (hidden_ok) {
                clearInterval(loop_checkEl);
                doCallback(callback);
            } else if (old_top==document.querySelectorAll(query)[qid].getBoundingClientRect().top && old_left==document.querySelectorAll(query)[qid].getBoundingClientRect().left) {
                clearInterval(loop_checkEl);
                doCallback(callback);
            } else {
                old_top = document.querySelectorAll(query)[qid].getBoundingClientRect().top;
                old_left = document.querySelectorAll(query)[qid].getBoundingClientRect().left;
            }
        }
    }, 300);
}

// =========================================================

function clickRefresh() {
    var count_delay = 2;
    var loop_delay = setInterval(function() {
        count_delay -= 1;
        console.log(count_delay+1);
        if (count_delay == 0) {
            document.querySelectorAll("#lounge-refresh")[0].click();
            clearInterval(loop_delay);
        }
    }, 500);
}

function processRoom(room_obj) {
    // console.log(room_obj);
    var selector_str = "ul.rooms>li.name>form>button[value='"+room_obj.roomId+"']";
    // console.log(selector_str);
    var el_room = document.querySelector(selector_str);
    if (el_room != null) {
        var el_music = el_room.querySelector(".room-badge.room-badge-music");
        var el_description = el_room.querySelector(".room-badge.room-badge-description");
        var el_title = el_room.querySelector(".room-name");
        var lang_code = room_obj.language.slice(-2);
        var dom_lang = '<i class="region region-' + lang_code + '"></i>';
        var new_title = "[" + lang_code + "] " + room_obj.name;
        el_title.innerHTML = dom_lang + " " + new_title;
        el_title.title = new_title;
        var data_jets_val = el_room.parentElement.parentElement.parentElement.attributes["data-jets"].value;
        el_room.parentElement.parentElement.parentElement.attributes["data-jets"].value = new_title.toLowerCase() + data_jets_val.slice(room_obj.name.length);
        if (swap_span_pos) {
            el_title.parentElement.insertBefore(el_title, el_title.parentElement.children[0]);
            var width_int = el_room.getBoundingClientRect().width - el_music.getBoundingClientRect().width - el_description.getBoundingClientRect().width;
            var width_str = Math.ceil(width_int).toString() + 'px';
            el_title.style.width = width_str;
        }
    }
}

function processRooms(rooms_obj) {
    // console.log(rooms_obj);
    for (var i = 0; i < rooms_obj.rooms.length; i++) {
        processRoom(rooms_obj.rooms[i]);
    }
}


(function() {
    'use strict';
    let origOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function() {
        this.addEventListener('load', function() {
            var url_obj = new URL(this.responseURL);
            if (url_obj.pathname == "/lounge" && url_obj.search == "?api=json") {
                processRooms(JSON.parse(this.responseText));
            }
        });
        origOpen.apply(this, arguments);
    };
    checkEl("#lounge-refresh",0,clickRefresh);
})();