JGY
「创建博客」 「编辑」 「本文源码」

完美输入



// ==UserScript==
// @name         完美输入
// @namespace    https://viayoo.com/
// @version      2.3
// @description  短按切换输入框,长按修改三处文字
// @author       You
// @run-at       document-end
// @match        https://*/*
// @grant        none
// ==/UserScript==
(function() {
    'use strict';

    // 创建顶部固定容器
    const container = document.createElement('div');
    container.id = 'fixed-container';
    Object.assign(container.style, {
        position: 'fixed',
        top: '0',
        left: '50%',
        transform: 'translateX(-50%)',
        zIndex: '999999',
        width: '60vw',
        maxWidth: '400px'
    });
    document.body.appendChild(container);

    // 可编辑文字配置
    let textConfig = {
        btnText: '༺来撩我༻',
        tip1: '旧爱🌹 YF',
        tip2: '新欢🌹❤️🌹ZYL'
    };
    const heightConfig = ['10px', '40px'];
    let currentIndex = 0;
    let mainTextarea = null;

    // 创建输入框通用方法,优化文字辨识度
    function createTextarea(idx) {
        const textarea = document.createElement('textarea');
        textarea.placeholder = idx === 0 ? textConfig.tip1 : textConfig.tip2;
        Object.assign(textarea.style, {
            marginTop: '20px',
            padding: '8px',
            border: 'none',
            borderRadius: '4px',
            boxSizing: 'border-box',
            backgroundColor: 'transparent',
            resize: 'none',
            wordWrap: 'break-word',
            minHeight: heightConfig[idx],
            overflow: 'hidden',
            width: '100%',
            fontSize: '16px',
            color: '#000',
            textShadow: '0 0 1px #fff, 0 0 2px #fff'
        });
        return textarea;
    }

    // 自动高度自适应
    function autoExpandTextarea() {
        this.style.height = 'auto';
        this.style.height = this.scrollHeight + 'px';
    }

    // 刷新当前输入框
    function refreshTextarea() {
        const newArea = createTextarea(currentIndex);
        if (mainTextarea) {
            container.replaceChild(newArea, mainTextarea);
        } else {
            container.appendChild(newArea);
        }
        mainTextarea = newArea;
        mainTextarea.addEventListener('input', autoExpandTextarea);
    }

    // 初始化界面
    refreshTextarea();

    // 切换按钮 保留原本样式不变
    const toggleBtn = document.createElement('button');
    toggleBtn.textContent = textConfig.btnText;
    Object.assign(toggleBtn.style, {
        display: 'block',
        backgroundColor: 'transparent',
        marginTop: '4px',
        border: 'none',
        padding: '4px 8px',
        color: 'rgba(0, 0, 0, 0.2)',
        cursor: 'pointer',
        fontSize: '14px'
    });
    container.appendChild(toggleBtn);

    // 长按判定变量
    let pressTimer = null;
    const longPressTime = 500;

    // 按下监听
    toggleBtn.addEventListener('mousedown', startPress);
    toggleBtn.addEventListener('touchstart', startPress);
    toggleBtn.addEventListener('mouseup', endPress);
    toggleBtn.addEventListener('mouseleave', endPress);
    toggleBtn.addEventListener('touchend', endPress);

    // 长按修改三处文字
    function editAllText() {
        // 修改按钮文字
        let newBtn = prompt('修改按钮显示文字', textConfig.btnText);
        if (newBtn !== null) {
            textConfig.btnText = newBtn;
            toggleBtn.textContent = newBtn;
        }
        // 修改第一个输入框提示
        let newTip1 = prompt('修改第一处占位文字', textConfig.tip1);
        if (newTip1 !== null) {
            textConfig.tip1 = newTip1;
        }
        // 修改第二个输入框提示
        let newTip2 = prompt('修改第二处占位文字', textConfig.tip2);
        if (newTip2 !== null) {
            textConfig.tip2 = newTip2;
        }
        // 刷新输入框展示新文字
        refreshTextarea();
    }

    function startPress(e) {
        if (e.type === 'touchstart') e.preventDefault();
        pressTimer = setTimeout(() => {
            pressTimer = null;
            editAllText();
        }, longPressTime);
    }

    function endPress() {
        if (pressTimer) {
            clearTimeout(pressTimer);
            // 短按切换输入框
            currentIndex = currentIndex === 0 ? 1 : 0;
            refreshTextarea();
            pressTimer = null;
        }
    }
})();


如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

¥ 打赏博主

类似帖子

上一篇 软键盘

发布评论