Gemini Canvas Shared Page Optimization

Gemini Canvas shared page layout optimization, including removal of the Gemini logo in the header, Google avatar, Google "content may be unsafe" warning at the bottom, etc.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name                Gemini Canvas Shared Page Optimization
// @description        Gemini Canvas shared page layout optimization, including removal of the Gemini logo in the header, Google avatar, Google "content may be unsafe" warning at the bottom, etc.
// @name:zh-CN          Gemini Canvas 共享页面布局优化
// @description:zh-CN   Gemini Canvas 共享页面布局优化,包括删除头部gemini logo、google头像、底部google 不安全内容提示等。
// @version      1.3
// @author       Stuart.Z
// @match        https://gemini.google.com/share/*
// @grant        none
// @run-at       document-end
// @license      GPL-3.0 License
// @namespace https://greasyfork.org/users/324739
// ==/UserScript==




(function() {
    'use strict';

    // 检查是否为共享页面(不包含 side-nav-menu-button 的页面)
    const isSharedPage = () => {
        const sideNavButton = document.querySelector('side-nav-menu-button');
        return !sideNavButton;
    };

    const optimizeWidth = () => {
        // 如果不是共享页面,跳过
        if (!isSharedPage()) return;

        // 需要修改宽度的元素选择器
        const selectors = [
            'mat-sidenav-content',
            '.main-content',
            '.input-area-container',
            '.bottom-bar-container',
            '.conversation-container'
        ];

        selectors.forEach(selector => {
            const elements = document.querySelectorAll(selector);
            elements.forEach(el => {
                // 强制设置为 98%
                el.style.setProperty('width', '98%', 'important');
                el.setAttribute('data-width-optimized', 'true');
            });
        });
    };

    const removeElements = () => {
        // 如果不是共享页面,跳过
        if (!isSharedPage()) return;

        // 需要删除的元素选择器列表
        const removeSelectors = [
            '.footer.footer-desktop.ng-star-inserted',           // 1. 删除 footer
            '.right-section',                                    // 2. 删除 right-section
            '.gb_Cd[style*="display:block"]',                   // 3. 删除 gb_Cd (带特定样式)
            '.side-nav-menu-button.with-pill-ui',                // 4. 删除 side-nav-menu-button
            '.desktop-ogb-buffer'                                // 5. 删除 desktop-ogb-buffer
        ];

        removeSelectors.forEach(selector => {
            const elements = document.querySelectorAll(selector);
            elements.forEach(el => {
                if (el && el.parentNode) {
                    el.parentNode.removeChild(el);
                }
            });
        });
    };

    const runAllOptimizations = () => {
        optimizeWidth();
        removeElements();
    };

    // 防抖函数
    function debounce(fn, delay) {
        let timer;
        return function() {
            clearTimeout(timer);
            timer = setTimeout(() => fn.apply(this, arguments), delay);
        };
    }

    const debouncedOptimize = debounce(runAllOptimizations, 100);

    // 使用 MutationObserver 监听 DOM 变化
    const observer = new MutationObserver((mutations) => {
        const shouldOptimize = mutations.some(mutation => {
            return mutation.type === 'childList' ||
                   (mutation.type === 'attributes' && mutation.attributeName === 'style');
        });

        if (shouldOptimize) {
            debouncedOptimize();
        }
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: true,
        attributeFilter: ['style', 'class']
    });

    // 初始执行
    runAllOptimizations();

    // 监听路由变化(SPA 页面)
    const _pushState = history.pushState;
    history.pushState = function() {
        _pushState.apply(this, arguments);
        setTimeout(runAllOptimizations, 100);
    };

    const _replaceState = history.replaceState;
    history.replaceState = function() {
        _replaceState.apply(this, arguments);
        setTimeout(runAllOptimizations, 100);
    };

    // 监听 popstate 事件(浏览器前进后退)
    window.addEventListener('popstate', () => {
        setTimeout(runAllOptimizations, 100);
    });

})();