SubHD屏蔽机翻字幕

添加启用/禁用屏蔽机器翻译字幕的开关按钮,通过按钮样式区分状态

目前为 2024-12-10 提交的版本。查看 最新版本

// ==UserScript==
// @name         SubHD屏蔽机翻字幕
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  添加启用/禁用屏蔽机器翻译字幕的开关按钮,通过按钮样式区分状态
// @author       Allion
// @license      MIT
// @match        https://subhd.tv/d/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=subhd.tv
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    const blockKeyword = "机器翻译"; // 屏蔽关键字
    let isEnabled = true; // 默认启用屏蔽

    // 添加屏蔽按钮
    const addToggleButton = () => {
        const btnContainer = document.querySelector('.btn.btn-outline-light.btn-sm.f12.me-1');
        if (!btnContainer) return;

        // 插入屏蔽按钮
        const btnHTML = `
            <a class="btn btn-info btn-sm f12 me-1 fav" id="toggleTranslateFilter" role="button">
                <svg class="me-1" xmlns="http://www.w3.org/2000/svg" width="13" height="13" fill="currentColor" viewBox="0 0 16 16">
                    <path d="M3 3h10a1 1 0 0 1 1 1v7H2V4a1 1 0 0 1 1-1zm0 1v6h10V4H3z"/>
                    <path d="M0 12h16v1a1 0 0 1-1 1H1a1 0 0 1-1-1v-1zm2 1h12v-.5H2v.5z"/>
                </svg>
                屏蔽机翻
            </a>
        `;
        btnContainer.insertAdjacentHTML('beforeBegin', btnHTML);

        const toggleButton = document.getElementById('toggleTranslateFilter');
        toggleButton.addEventListener('click', () => {
            isEnabled = !isEnabled;
            updateButtonStyle(toggleButton);
            if (isEnabled) {
                hideTranslatedEntries(); // 启用屏蔽
            } else {
                document.querySelectorAll('.row.pt-2.mb-2').forEach(row => row.style.display = ''); // 显示所有条目
            }
        });
    };

    // 更新按钮样式
    const updateButtonStyle = (button) => {
        if (isEnabled) {
            button.className = "btn btn-info btn-sm f12 me-1 fav"; // 启用状态
            button.innerHTML = `
                <svg class="me-1" xmlns="http://www.w3.org/2000/svg" width="13" height="13" fill="currentColor" viewBox="0 0 16 16">
                    <path d="M3 3h10a1 1 0 0 1 1 1v7H2V4a1 0 0 1 1-1zm0 1v6h10V4H3z"/>
                    <path d="M0 12h16v1a1 0 0 1-1 1H1a1 0 0 1-1-1v-1zm2 1h12v-.5H2v.5z"/>
                </svg>
                屏蔽机翻
            `;
        } else {
            button.className = "btn btn-sm f12 me-1 fav btn-outline-info"; // 禁用状态
            button.innerHTML = `
                <svg class="me-1" xmlns="http://www.w3.org/2000/svg" width="13" height="13" fill="currentColor" viewBox="0 0 16 16">
                    <path d="M3 3h10a1 1 0 0 1 1 1v7H2V4a1 1 0 0 1 1-1zm0 1v6h10V4H3z"/>
                    <path d="M0 12h16v1a1 0 0 1-1 1H1a1 0 0 1-1-1v-1zm2 1h12v-.5H2v.5z"/>
                </svg>
                屏蔽机翻
            `;
        }
    };

    // 隐藏含关键字的条目
    const hideTranslatedEntries = () => {
        document.querySelectorAll('.row.pt-2.mb-2').forEach(row => {
            if (row.querySelector('.bg-secondary') && row.querySelector('.bg-secondary').textContent.includes(blockKeyword)) {
                row.style.display = 'none'; // 隐藏含“机器翻译”条目
            }
        });
    };

    // 监听页面变化,动态屏蔽新增条目
    const observer = new MutationObserver(() => {
        if (isEnabled) hideTranslatedEntries();
    });
    observer.observe(document.body, { childList: true, subtree: true });

    // 初始设置
    addToggleButton();
    if (isEnabled) hideTranslatedEntries(); // 默认启用时屏蔽
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址