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.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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);
    });

})();