TOOLS为非官方社区,欢迎一起建设,答疑群:717421103

版本260427.1

全站通知:

用户:5205017/Wiki CSS 快速测试工具

来自ToolsWIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

一个油猴脚本,在BWIKI页面右下角生成一个悬浮可视面板,可用于快速注入并测试CSS样式。

// ==UserScript==
// @name         Wiki CSS 快速测试工具
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  在当前页面提供一个半透明悬浮面板,用于快速注入并测试CSS样式
// @author       Wiki Editor
// @match        *://*.wiki.biligame.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 检查是否已经存在面板,防止重复注入
    if (document.getElementById('tm-css-tester')) return;

    // --- 1. 创建主容器 (使用 BEM 命名法: tm-tester) ---
    var panel = document.createElement('div');
    panel.id = 'tm-css-tester';
    panel.className = 'tm-tester';

    // 面板样式:半暗色圆润 UI 与毛玻璃效果
    var panelStyle = [
        'position: fixed;',
        'bottom: 30px;',
        'right: 30px;',
        'width: 320px;',
        'background-color: rgba(51, 65, 85, 0.9);',
        'border-radius: 12px;',
        'box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);',
        'padding: 16px;',
        'z-index: 999999;',
        'backdrop-filter: blur(10px);',
        'box-sizing: border-box;',
        'display: flex;',
        'flex-direction: column;',
        'gap: 12px;',
        'transition: box-shadow 0.3s ease;'
    ].join('');
    panel.style.cssText = panelStyle;

    // --- 2. 创建拖拽标题 (tm-tester__header) ---
    var header = document.createElement('div');
    header.className = 'tm-tester__header';
    header.innerHTML = 'CSS 快速测试面板';
    header.style.cssText = 'color: #e2e8f0; font-weight: bold; font-size: 14px; text-align: center; cursor: move; user-select: none; padding-bottom: 4px; border-bottom: 1px solid rgba(255, 255, 255, 0.1);';
    panel.appendChild(header);

    // --- 3. 创建输入框 (tm-tester__textarea) ---
    var textarea = document.createElement('textarea');
    textarea.className = 'tm-tester__textarea';
    textarea.placeholder = '/* 在此输入CSS代码进行测试喵~ */\n.example__block {\n    color: red;\n}';
    textarea.style.cssText = 'width: 100%; height: 180px; background-color: #1e293b; color: #e2e8f0; border: 1px solid rgba(255, 255, 255, 0.15); border-radius: 8px; padding: 10px; box-sizing: border-box; font-family: "Consolas", monospace; font-size: 13px; resize: vertical; outline: none;';
    panel.appendChild(textarea);

    // --- 4. 创建按钮组 (tm-tester__controls) ---
    var controls = document.createElement('div');
    controls.className = 'tm-tester__controls';
    controls.style.cssText = 'display: flex; gap: 10px; justify-content: space-between;';

    // 应用按钮 (tm-tester__btn tm-tester__btn--apply)
    var applyBtn = document.createElement('button');
    applyBtn.className = 'tm-tester__btn tm-tester__btn--apply';
    applyBtn.innerHTML = '应用样式';
    applyBtn.style.cssText = 'flex: 1; padding: 8px 0; background-color: #88c0d0; color: #1e293b; border: none; border-radius: 6px; cursor: pointer; font-weight: bold; transition: opacity 0.2s;';
    controls.appendChild(applyBtn);

    // 清除按钮 (tm-tester__btn tm-tester__btn--clear)
    var clearBtn = document.createElement('button');
    clearBtn.className = 'tm-tester__btn tm-tester__btn--clear';
    clearBtn.innerHTML = '清除';
    clearBtn.style.cssText = 'flex: 1; padding: 8px 0; background-color: #d08770; color: #1e293b; border: none; border-radius: 6px; cursor: pointer; font-weight: bold; transition: opacity 0.2s;';
    controls.appendChild(clearBtn);

    panel.appendChild(controls);
    document.body.appendChild(panel);

    // --- 5. 创建用于挂载测试样式的 <style> 标签 ---
    var styleTag = document.createElement('style');
    styleTag.id = 'tm-css-tester-style';
    styleTag.type = 'text/css';
    document.head.appendChild(styleTag);

    // --- 6. 绑定交互事件 (ES5 语法) ---

    // Hover 效果补全
    applyBtn.onmouseover = function() { applyBtn.style.opacity = '0.8'; };
    applyBtn.onmouseout = function() { applyBtn.style.opacity = '1'; };
    clearBtn.onmouseover = function() { clearBtn.style.opacity = '0.8'; };
    clearBtn.onmouseout = function() { clearBtn.style.opacity = '1'; };

    // 注入 CSS 逻辑
    applyBtn.onclick = function() {
        var cssContent = textarea.value;
        if (styleTag.styleSheet) {
            styleTag.styleSheet.cssText = cssContent; // 兼容极老版本 IE
        } else {
            styleTag.innerHTML = '';
            styleTag.appendChild(document.createTextNode(cssContent));
        }

        // 视觉反馈
        var originalText = applyBtn.innerHTML;
        applyBtn.innerHTML = '已应用喵!';
        setTimeout(function() {
            applyBtn.innerHTML = originalText;
        }, 1000);
    };

    // 清除逻辑
    clearBtn.onclick = function() {
        textarea.value = '';
        if (styleTag.styleSheet) {
            styleTag.styleSheet.cssText = '';
        } else {
            styleTag.innerHTML = '';
        }
    };

    // --- 7. 简易拖拽功能 (支持在页面上任意移动面板避免遮挡) ---
    var isDragging = false;
    var offsetX = 0;
    var offsetY = 0;

    header.onmousedown = function(e) {
        isDragging = true;
        // 计算鼠标点击位置与面板左上角的偏移量
        offsetX = e.clientX - panel.offsetLeft;
        offsetY = e.clientY - panel.offsetTop;

        // 拖拽时提升层级和增加阴影
        panel.style.boxShadow = '0 8px 24px rgba(0, 0, 0, 0.4)';

        document.onmousemove = onMouseMove;
        document.onmouseup = onMouseUp;
        // 阻止默认文本选中行为
        return false;
    };

    function onMouseMove(e) {
        if (isDragging) {
            // 将 bottom 和 right 转换为 left 和 top,防止布局冲突
            panel.style.bottom = 'auto';
            panel.style.right = 'auto';
            panel.style.left = (e.clientX - offsetX) + 'px';
            panel.style.top = (e.clientY - offsetY) + 'px';
        }
    }

    function onMouseUp() {
        if (isDragging) {
            isDragging = false;
            panel.style.boxShadow = '0 4px 16px rgba(0, 0, 0, 0.3)';
            document.onmousemove = null;
            document.onmouseup = null;
        }
    }

})();