Skip to content

Instantly share code, notes, and snippets.

@dichen-cd
Created September 1, 2025 03:08
Show Gist options
  • Select an option

  • Save dichen-cd/3c2de36fe69082466c0b1d0fdeb0e1e7 to your computer and use it in GitHub Desktop.

Select an option

Save dichen-cd/3c2de36fe69082466c0b1d0fdeb0e1e7 to your computer and use it in GitHub Desktop.
Zhihu Right Sidebar Remover 知乎侧边栏去除
// ==UserScript==
// @name Zhihu Right Sidebar Remover
// @namespace http://tampermonkey.net/
// @version 0.5
// @description Robustly finds the sidebar by its data-attribute and removes it, watching for all DOM changes.
// @author Gemini
// @match https://www.zhihu.com/*
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
const sidebarSelector = 'div[data-za-detail-view-path-module="RightSideBar"]';
const contentSelectors = '.Topstory-mainColumn, .Question-mainColumn';
// 1. First line of defense: CSS to hide it immediately and expand content.
GM_addStyle(`
${contentSelectors} {
width: 100% !important;
}
${sidebarSelector} {
display: none !important;
}
`);
function removeRightSidebar() {
const sidebar = document.querySelector(sidebarSelector);
if (sidebar) {
sidebar.remove();
}
}
// 2. Second line of defense: Run removal on an interval for the first second.
const initialCheck = setInterval(removeRightSidebar, 100);
setTimeout(() => clearInterval(initialCheck), 1000);
document.addEventListener('DOMContentLoaded', removeRightSidebar);
// 3. Final and most robust line of defense: A MutationObserver watching everything.
const observer = new MutationObserver(() => {
removeRightSidebar();
});
// Observe the entire document body for any changes to the element tree or attributes.
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: true
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment