SubHD屏蔽机翻字幕

添加启用/禁用屏蔽机器翻译字幕的开关按钮

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

// ==UserScript==
// @name         SubHD屏蔽机翻字幕
// @namespace    http://tampermonkey.net/
// @version      1.0
// @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';

    // 将CSS样式添加到页面
    const style = document.createElement('style');
    style.innerHTML = `
        /* 开关样式 */
        .switch {
          position: relative;
          display: inline-block;
          width: 34px;
          height: 20px;
        }

        .switch input {
          opacity: 0;
          width: 0;
          height: 0;
        }

        .slider {
          position: absolute;
          cursor: pointer;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          background-color: #ccc;
          transition: .4s;
          border-radius: 50px;
        }

        .slider:before {
          position: absolute;
          content: "";
          height: 12px;
          width: 12px;
          border-radius: 50px;
          left: 4px;
          bottom: 4px;
          background-color: white;
          transition: .4s;
        }

        input:checked + .slider {
          background-color: #4CAF50;
        }

        input:checked + .slider:before {
          transform: translateX(14px);
        }

        /* 保持按钮布局 */
        .toggle-container {
          display: inline-flex;
          align-items: center;
          margin-right: 15px;
        }
    `;
    document.head.appendChild(style);

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

    const createToggleButton = () => {
        const toggleContainer = document.createElement('div');
        toggleContainer.classList.add('toggle-container');

        // 添加文字说明
        const label = document.createElement('span');
        label.textContent = '机翻屏蔽:';
        label.style.marginRight = '10px';

        const toggle = document.createElement('label');
        toggle.classList.add('switch');
        toggle.innerHTML = `
            <input type="checkbox" id="toggleBtn" ${isEnabled ? 'checked' : ''}>
            <span class="slider round"></span>
        `;

        toggleContainer.appendChild(label);
        toggleContainer.appendChild(toggle);

        const container = document.querySelector('.shadow-sm.p-3.my-3.bg-white.rounded-3');
        const uploadBtnContainer = container ? container.querySelector('.float-end') : null;

        if (uploadBtnContainer) {
            uploadBtnContainer.style.display = 'flex'; // 确保按钮容器使用弹性布局
            uploadBtnContainer.style.alignItems = 'center';
            uploadBtnContainer.insertBefore(toggleContainer, uploadBtnContainer.firstChild);  // 在上传按钮左侧插入
        }

        toggle.querySelector('input').addEventListener('change', (e) => {
            isEnabled = e.target.checked;
            if (isEnabled) {
                hideTranslatedEntries();  // 启用时屏蔽
            } else {
                document.querySelectorAll('.row.pt-2.mb-2').forEach(row => row.style.display = ''); // 恢复显示
            }
        });
    };

    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'; // 隐藏含“机器翻译”条目
            }
        });
    };

    createToggleButton();
    if (isEnabled) hideTranslatedEntries();  // 默认启用时屏蔽

    const observer = new MutationObserver(() => {
        if (isEnabled) hideTranslatedEntries(); // 只在启用时执行检查
    });
    observer.observe(document.body, { childList: true, subtree: true });
})();