知乎优化

Sets custom widths and hides specified elements on Zhihu

// ==UserScript==
// @name         知乎优化
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  Sets custom widths and hides specified elements on Zhihu
// @author       You
// @match        https://www.zhihu.com/*
// @match        http://www.zhihu.com/*
// @match        https://zhihu.com/*
// @match        http://zhihu.com/*
// @grant        none
// @license MIT
// ==/UserScript==
(function() {
    'use strict';
    // Function to apply width change
    function setMainColumnWidth() {
        const mainColumn = document.querySelector('.Topstory-mainColumn');
        if (mainColumn) {
            mainColumn.style.width = '100%';
            console.log('Zhihu Main Column width set to 100%');
        }
    }

    // Function to apply width change for Question pages
    function setQuestionMainColumnWidth() {
        const mainColumn = document.querySelector('.Question-mainColumn');
        if (mainColumn) {
            mainColumn.style.width = '100%';
            console.log('Zhihu Question Main Column width set to 100%');
        }
    }

    // Function to apply custom CSS styles
    function applyCustomStyles() {
        // Apply styles to .css-11p8nt5 elements
        const customElements = document.querySelectorAll('.css-11p8nt5');
        if (customElements.length > 0) {
            customElements.forEach(element => {
                element.style.maxWidth = '0px';
                element.style.minWidth = '950px';
                console.log('Applied custom styles to .css-11p8nt5');
            });
        }
        const customElements2 = document.querySelectorAll('.css-1kjxdzv');
        if (customElements2.length > 0) {
            customElements2.forEach(element => {
                element.style.maxWidth = '0px';
                element.style.minWidth = '950px';
                console.log('Applied custom styles to .css-1kjxdzv');
            });
        }


    }
  // Function to hide Question-sideColumn
    function hideQuestionSideColumn() {
        const sideColumn = document.querySelector('.Question-sideColumn');

        if (sideColumn) {
           sideColumn.remove();
            console.log('Zhihu Question Side Column hidden');
        }
    }
    // Function to hide the specified elements
    function hideSpecifiedElements() {
        // Original element to hide
        const elementsToHide1 = document.querySelectorAll('.css-1qyytj7 > div');
        if (elementsToHide1.length > 0) {
            elementsToHide1.forEach(element => {
                element.style.display = 'none';
                console.log('Hidden element with class .css-1qyytj7 > div');
            });
        }

        // New elements to hide
        // 1. Elements with class .css-29q9fa
        const elementsToHide2 = document.querySelectorAll('.css-29q9fa');
        if (elementsToHide2.length > 0) {
            elementsToHide2.forEach(element => {
                element.style.display = 'none';
                console.log('Hidden element with class .css-29q9fa');
            });
        }

        // 2. Third AppHeader-Tab
        const thirdTab = document.querySelector('li.Tabs-item--noMeta.AppHeader-Tab.Tabs-item:nth-of-type(3)');
        if (thirdTab) {
            thirdTab.style.display = 'none';
            console.log('Hidden third AppHeader Tab');
        }

        // 3. Fourth AppHeader-Tab
        const fourthTab = document.querySelector('li.Tabs-item--noMeta.AppHeader-Tab.Tabs-item:nth-of-type(4)');
        if (fourthTab) {
            fourthTab.style.display = 'none';
            console.log('Hidden fourth AppHeader Tab');
        }

        // 4. Button with complex class chain
        const buttons = document.querySelectorAll('.css-18vqx7l > .fEPKGkUK5jyc4fUuT0QP.Button--plain.FEfUrdfMIKpQDJDqkjte.css-79elbk.Button');
        if (buttons.length > 0) {
            buttons.forEach(button => {
                button.style.display = 'none';
                console.log('Hidden specified button');
            });
        }
    }

    // Apply immediately for already loaded elements
    function applyAllChanges() {
        setMainColumnWidth();
        setQuestionMainColumnWidth();
        hideSpecifiedElements();
        applyCustomStyles();
       hideQuestionSideColumn() ;
    }

    // Initial application
    applyAllChanges();

    // Create a MutationObserver to handle dynamically loaded content
    const observer = new MutationObserver(function(mutations) {
        applyAllChanges();
    });

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