My First Userscript

Highlights all input fields, adds a floating button that toggles dark mode, and logs key presses.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         My First Userscript
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Highlights all input fields, adds a floating button that toggles dark mode, and logs key presses.
// @author       PianoMan0
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function highlightInputs() {
        const inputs = document.querySelectorAll('input, textarea');
        inputs.forEach(input => {
            input.style.border = '2px solid #4CAF50';
            input.style.backgroundColor = '#e8f5e9';
        });
    }
    highlightInputs();

    const button = document.createElement('button');
    button.textContent = 'Toggle Dark Mode';
    button.style.position = 'fixed';
    button.style.bottom = '24px';
    button.style.right = '24px';
    button.style.zIndex = '10000';
    button.style.padding = '12px 20px';
    button.style.background = '#222';
    button.style.color = '#fff';
    button.style.border = 'none';
    button.style.borderRadius = '8px';
    button.style.boxShadow = '0 2px 8px rgba(0,0,0,0.2)';
    button.style.cursor = 'pointer';
    button.style.opacity = '0.85';

    document.body.appendChild(button);

    let dark = false;
    button.addEventListener('click', () => {
        dark = !dark;
        if (dark) {
            document.body.style.background = '#222';
            document.body.style.color = '#eee';
            button.style.background = '#fff';
            button.style.color = '#222';
        } else {
            document.body.style.background = '';
            document.body.style.color = '';
            button.style.background = '#222';
            button.style.color = '#fff';
        }
    });

    document.addEventListener('keydown', function(e) {
        console.log(`Key pressed: ${e.key}`);
    });

    setTimeout(() => {
        const msg = document.createElement('div');
        msg.textContent = 'Userscript loaded! Inputs highlighted. Try toggling dark mode.';
        msg.style.position = 'fixed';
        msg.style.top = '10px';
        msg.style.left = '50%';
        msg.style.transform = 'translateX(-50%)';
        msg.style.background = '#4CAF50';
        msg.style.color = '#fff';
        msg.style.padding = '8px 24px';
        msg.style.borderRadius = '6px';
        msg.style.zIndex = '10001';
        msg.style.boxShadow = '0 2px 8px rgba(0,0,0,0.1)';
        document.body.appendChild(msg);
        setTimeout(() => msg.remove(), 3000);
    }, 500);
})();