Add Encode Button for Voz

Add encode hex, base64 button for Voz

Tính đến 23-08-2024. Xem phiên bản mới nhất.

// ==UserScript==
// @name         Add Encode Button for Voz
// @namespace    0x4076696e63766e
// @version      0.5
// @description  Add encode hex, base64 button for Voz
// @author       0x4076696e63766e
// @match        https://voz.vn/t/*
// @grant        none
// @license     GNU General Public License (GPL)
// ==/UserScript==

(function () {
    'use strict';

    var $Editor = null;
    var $BBMode = false;
    function openDialog(encodeFunc) {
        const input = prompt("Nhập vào văn bản để mã hóa:");
        if (input === null) return; // Người dùng đã hủy
        const encoded = encodeFunc(input);
        pickEditor();
        if ($Editor) {
            if($Editor.tagName.toString() === 'DIV'){
              $Editor.innerHTML += `<p>${encoded}</p>`;
            }else{
              $Editor.value += `\n${encoded}`;
            }
        }
    }

    function hexEncode(str) {
        return Array.from(str).map(char => char.charCodeAt(0).toString(16).padStart(2, '0')).join('');
    }

    function base64Encode(str) {
        return btoa(unescape(encodeURIComponent(str)));
    }

    function pickEditor() {
        const editorContainers = document.querySelectorAll('div.message-editorWrapper');
        editorContainers.forEach(container => {
            const textareaEditor = container.querySelector('textarea.input');
            const bbEditor = container.querySelector('div.fr-element');
            const bbCodeMode = container.querySelector('#xfBbCode-1');
            if(bbCodeMode) $BBMode = bbCodeMode.classList.contains('fr-active')
            $Editor = ($BBMode) ? textareaEditor : bbEditor
            console.log($BBMode)
            console.log($Editor)
        });
    }

    function createButton(label, callback) {
        const button = document.createElement('div');
        button.className = 'button--primary button';
        button.style.marginRight = '5px';
        button.textContent = label;
        button.onclick = callback;
        return button;
    }

    function insertHelper(extraDivs) {
        if (document.querySelectorAll("div.encode-helper").length > 0) return;

        extraDivs.forEach(extraDiv => {
            const newDiv = document.createElement('div');
            newDiv.className = 'formButtonGroup encode-helper';
            newDiv.appendChild(createButton('Hex', () => openDialog(hexEncode)));
            newDiv.appendChild(createButton('Base64', () => openDialog(base64Encode)));
            extraDiv.parentNode.insertBefore(newDiv, extraDiv);
        });
    }

    function main() {
        const elements = document.querySelectorAll('div.formButtonGroup');
        insertHelper(elements);
    }

    function initializeObserver() {
        new MutationObserver(main).observe(document.documentElement, { childList: true, subtree: true });
    }

    document.readyState === 'loading'
        ? document.addEventListener('DOMContentLoaded', initializeObserver)
        : initializeObserver();
})();