Jules Auto-Collapse Diffs

Automatically collapses diffs on Jules.google.com upon page load

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Jules Auto-Collapse Diffs
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Automatically collapses diffs on Jules.google.com upon page load
// @author       BCRTVKCS
// @license      MIT
// @match        https://jules.google.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let lastUrl = location.href;
    let hasCollapsed = false;

    function attemptCollapse() {
        const btn = document.querySelector('button.collapse-all-button');

        // Button found and not yet collapsed on this page
        if (btn && !hasCollapsed) {
            hasCollapsed = true; // Lock immediately to prevent re-triggering

            // Wait 200 milliseconds for the framework to attach the event listener before clicking
            setTimeout(() => {
                const currentBtn = document.querySelector('button.collapse-all-button');
                if (currentBtn) {
                    currentBtn.click();
                    console.log("Jules Auto-Collapse: Diffs successfully collapsed.");
                }
            }, 200);
        }
    }

    // Reset state if URL changes in SPA (Single Page Application) architecture
    const observer = new MutationObserver(() => {
        if (location.href !== lastUrl) {
            lastUrl = location.href;
            hasCollapsed = false; // Navigated to a new file, allow collapsing again
        }
        attemptCollapse();
    });

    // Start observing DOM changes
    observer.observe(document.body, { childList: true, subtree: true });
})();