DeepL UI Cleaner

Streamlines DeepL's interface for translation-focused use by removing footers, cookie banners, and adding toggle buttons for sidebar and header

目前為 2024-10-30 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         DeepL UI Cleaner
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  Streamlines DeepL's interface for translation-focused use by removing footers, cookie banners, and adding toggle buttons for sidebar and header
// @match        https://www.deepl.com/*
// @license      Unlicense
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function () {
    "use strict";

    // Define selectors for target elements
    const TARGETS = {
        remove: {
            footer: [
                "footer",
                ".relative.bg-neutral-next-50 > .mobile\\:hidden",
                '.bg-white[class*="px-0"][class*="xl:px-"][class*="md:px-"][class*="min-"][class*="px"]',
            ],
            cookieBanner: ['[id*="cookieBanner"]', '[class*="cookieBanner"]'],
            writePageElements: [
                '.bg-white.px-0[class*="xl:px-"][class*="px"][class*="md:px-"][class*="px"][class*="min-"][class*="px"][class*="px"]',
                '.mobile\\:hidden.p-8.px-0[class*="xl:px-"][class*="px"][class*="md:px-"][class*="px"][class*="min-"][class*="px"][class*="px"]',
                '.bg-white.min-\\[1280px\\]\\:px-\\[70px\\]'
            ],
        },
        toggle: {
            /*leftSidebar: '[class*="md:block"].bg-white.border-e[class*="w-["][class*="px]"].h-full.hidden[class*="start-0"].border-neutral-next-100',*/
            topHeader: 'header'
        },
    };

    // CSS styles for toggle buttons and initial header state
    const BUTTON_STYLES = `
        .toggle-button {
            position: fixed;
            z-index: 10000;
            width: 2em;
            height: 2em;
            background-color: rgba(240, 240, 240, 0.7);
            border: 1px solid #ccc;
            border-radius: 50%;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 16px;
            transition: all 0.3s;
        }
        .toggle-button:hover {
            background-color: rgba(220, 220, 220, 0.9);
        }
        ${TARGETS.toggle.topHeader} {
            display: none !important;
        }
    `;

    // Function to inject CSS styles into the page
    function injectStyles() {
        const style = document.createElement("style");
        const hideSelectors = [
            ...Object.values(TARGETS.remove).flat(),
        ].join(", ");
        style.textContent = `
            ${hideSelectors} { display: none !important; }
            ${BUTTON_STYLES}
        `;
        (document.head || document.documentElement).appendChild(style);
    }

    // Function to remove unwanted elements from the page
    function removeElements() {
        Object.values(TARGETS.remove)
            .flat()
            .forEach((selector) => {
                document
                    .querySelectorAll(selector)
                    .forEach((element) => element.remove());
            });
    }

    // Function to create a toggle button
    function createToggleButton(
        showIcon,
        hideIcon,
        onClick,
        position,
        ariaLabel
    ) {
        const button = document.createElement("button");
        button.className = "toggle-button";
        button.textContent = showIcon;  // Starting with show icon since header is hidden
        button.setAttribute("aria-label", ariaLabel);
        button.addEventListener("click", onClick);
        Object.assign(button.style, position);
        return button;
    }

    // Function to adjust button positions based on header visibility
    function adjustButtonPositions() {
        const header = document.querySelector(TARGETS.toggle.topHeader);
        const topPosition =
            header && getComputedStyle(header).display !== "none"
                ? `${header.offsetHeight}px`
                : "0.5em";
        document.querySelectorAll(".toggle-button").forEach((button) => {
            button.style.top = topPosition;
        });
    }

    // Function to toggle visibility of elements
    function toggleElementVisibility(selector, button, showIcon, hideIcon) {
        const elements = document.querySelectorAll(selector);
        const isVisible = [...elements].some(
            (el) => getComputedStyle(el).display !== "none"
        );
        elements.forEach((el) =>
            el.style.setProperty("display", isVisible ? "none" : "block", "important")
        );
        button.textContent = isVisible ? showIcon : hideIcon;
        adjustButtonPositions();
    }

    // Function to initialize the script
    function init() {
        try {
            removeElements();

            const buttons = [
                /*{
                    selector: TARGETS.toggle.leftSidebar,
                    icons: ["≡", "×"],
                    position: { left: "0.5em" },
                    ariaLabel: "Toggle sidebar visibility",
                },*/
                {
                    selector: TARGETS.toggle.topHeader,
                    icons: ["▼", "▲"],
                    position: { left: "0.5em" },
                    ariaLabel: "Toggle header visibility",
                },
            ];

            buttons.forEach(
                ({ selector, icons: [showIcon, hideIcon], position, ariaLabel }) => {
                    const button = createToggleButton(
                        showIcon,
                        hideIcon,
                        () => toggleElementVisibility(selector, button, showIcon, hideIcon),
                        position,
                        ariaLabel
                    );
                    document.body.appendChild(button);
                }
            );

            adjustButtonPositions();

            // Set up MutationObserver to handle dynamically added elements
            const observer = new MutationObserver(() => {
                removeElements();
                adjustButtonPositions();
            });
            observer.observe(document.body, { childList: true, subtree: true });
        } catch (error) {
            console.error("DeepL UI Cleaner encountered an error:", error);
        }
    }

    // Inject styles immediately
    injectStyles();

    // Run initialization when DOM is ready
    if (document.readyState === "loading") {
        document.addEventListener("DOMContentLoaded", init);
    } else {
        init();
    }
})();