Anubys / Forum Tool

// ==UserScript==
// @name         Forum Tool
// @namespace    Anubys
// @version      0.0.7
// @description  Divers outils pour la modération du forum.
// @author       Anubys
// @copyright    2022, Anubys (https://openuserjs.org/users/Anubys)
// @license      MIT
// @updateURL   https://openuserjs.org/meta/Anubys/Forum_Tool.meta.js
// @downloadURL https://openuserjs.org/install/Anubys/Forum_Tool.user.js
// @match        https://board.fr.ogame.gameforge.com/index.php?thread/*
// @match        https://board.fr.ogame.gameforge.com/wcf/index.php?conversation/*
// @match        https://board.fr.ogame.gameforge.com/wcf/index.php?conversation-add/&userID=*
// @match        https://board.fr.ogame.gameforge.com/wcf/index.php?conversation/*/&messageID=*
// @match        https://board.fr.ogame.gameforge.com/index.php?thread-add/*
// @match        https://board.fr.ogame.gameforge.com/wcf/index.php?conversation-add/
// @grant        none
// ==/UserScript==

(function () {
    var NumberRank,
        TeamPage;

    function parseFunction(data) {
        let DOMparser = new DOMParser();
        TeamPage = DOMparser.parseFromString(data, "text/html");
        NumberRank = TeamPage.querySelectorAll('.section.sectionContainerList');

        // Si des rangs existent.
        if (NumberRank) {
            Anubys();
        }

        // On créé le bouton.
        function Anubys() {

            let button = document.createElement('select'),
                doc = document.querySelector('.messageTabMenuNavigation.jsOnly > ul'),
                k = 0;

            button.style.background = '#708090';
            button.style.height = '30px';
            button.style.marginTop = '5px';
            button.style.marginLeft = '15px';

            doc.appendChild(button);
            addOption("0", "Selectionnez le rang", button);

            while (k < NumberRank.length) {
                let Ranks = TeamPage.querySelectorAll('.section.sectionContainerList > .sectionHeader > .sectionTitle')[k].firstChild.textContent;
                addOption(k + 1, Ranks, button);
                k = k + 1;
            }

            // Evènement sur selection du bouton.

            button.addEventListener("change", function () {
                let selected = button.options[button.selectedIndex].value;

                pseudo(selected - 1);
            });

        }

        // Fonction pour création d'option sur un bouton select.

        function addOption(optionName, optionText, selectElement) {
            let newOption = document.createElement("option");
            newOption.value = optionName;
            newOption.innerText = optionText;
            selectElement.appendChild(newOption);
        }

        function pseudo(Rank) {
            let Board = document.querySelector('#redactor-uuid-0'),
                NumberPseudo = TeamPage.querySelectorAll('.containerList.userList')[Rank].querySelectorAll('.username.userLink'),
                Insert = Board.querySelectorAll('p'),
                a = 0;

            while (a < NumberPseudo.length) {
                let pseudo = NumberPseudo[a].textContent,
                    Textbalise = document.createTextNode('@' + pseudo + ' ');

                Insert[Insert.length - 1].appendChild(Textbalise);
                a = a + 1;
            };

        }

    }

    // Réception de la page Team.

    fetch('https://board.fr.ogame.gameforge.com/wcf/index.php?team/')
        .then(response => response.text())
        .then(data => parseFunction(data))
        .catch(e => console.log('GOT ERROR : ' + e.message));
})();

// Vérification de l'URL.

var preview = document.querySelectorAll('.formSubmit'),
    url = window.location.href,
    WordUrl = String(url),
    TestUrl = /thread-add/.test(WordUrl);

// Mise en place du bouton si le preview est présent et si la possibilité de lock l'est également.

if (document.querySelector('#buttonMessagePreview')) {
    if (document.querySelector('[name=closeThread]')){
        let button = document.createElement('button');
        button.innerHTML ='Lock';
        button.type ='button'; // Permet de ne pas activer le subit des autres bouttons du formulaire avec celui-ci
        button.style.background ='#FF0000';
        button.addEventListener('click', lock);
        preview[preview.length - 1].append(button);
    }
};

function lock() {

    document.querySelector('[name=closeThread]').checked = true;
    AutoSubmit();
}

function AutoSubmit(){
    if (TestUrl){
        if (document.querySelector('#buttonBeautify')){
            let Beautify = document.querySelector('#buttonBeautify');
            Beautify.click();
        } else {
            let submit = document.querySelector('.formSubmit').getElementsByTagName('input')[0];
            submit.click();
        }

    } else if (document.querySelector('#buttonBeautify')){
        let Beautify = document.querySelector('#buttonBeautify');
        Beautify.click();
    } else {
        let submit = document.querySelectorAll('.buttonPrimary');
        submit[1].click();
    }
}