Mini DevTools Console

Console mô phỏng DevTools

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Advertisement:

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

Advertisement:

// ==UserScript==
// @name         Mini DevTools Console
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Console mô phỏng DevTools
// @author       You
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    const consoleBox = document.createElement('div');
    consoleBox.innerHTML = `
        <div id="tm-header">
            Mini Console
            <button id="tm-close">×</button>
        </div>
        <div id="tm-output"></div>
        <textarea id="tm-input" placeholder="Nhập JavaScript..."></textarea>
        <button id="tm-run">Run</button>
    `;

    Object.assign(consoleBox.style, {
        position: 'fixed',
        top: '20px',
        right: '20px',
        width: '500px',
        height: '350px',
        background: '#1e1e1e',
        color: '#fff',
        zIndex: '999999',
        border: '1px solid #555',
        fontFamily: 'monospace',
        display: 'flex',
        flexDirection: 'column'
    });

    document.body.appendChild(consoleBox);

    const output = consoleBox.querySelector('#tm-output');
    const input = consoleBox.querySelector('#tm-input');

    Object.assign(output.style, {
        flex: '1',
        overflow: 'auto',
        padding: '5px',
        borderBottom: '1px solid #444'
    });

    Object.assign(input.style, {
        height: '80px',
        background: '#252526',
        color: '#fff',
        border: 'none',
        resize: 'none'
    });

    function log(text, color = '#fff') {
        const div = document.createElement('div');
        div.style.color = color;
        div.textContent = text;
        output.appendChild(div);
        output.scrollTop = output.scrollHeight;
    }

    const originalLog = console.log;
    console.log = (...args) => {
        originalLog(...args);
        log(args.map(a =>
            typeof a === 'object'
                ? JSON.stringify(a, null, 2)
                : String(a)
        ).join(' '), '#4fc3f7');
    };

    document.getElementById('tm-run').onclick = () => {
        const code = input.value;

        try {
            const result = eval(code);
            log('> ' + String(result), '#90ee90');
        } catch (e) {
            log('Error: ' + e.message, '#ff6b6b');
        }
    };

    document.getElementById('tm-close').onclick = () => {
        consoleBox.remove();
    };

    input.addEventListener('keydown', e => {
        if (e.ctrlKey && e.key === 'Enter') {
            document.getElementById('tm-run').click();
        }
    });

})();