隐藏知乎问题标题

隐藏知乎页面中的问题标题

  1. // ==UserScript==
  2. // @name 隐藏知乎问题标题
  3. // @namespace https://example.com/
  4. // @version 0.1
  5. // @description 隐藏知乎页面中的问题标题
  6. // @author lyx
  7. // @match https://www.zhihu.com/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (async () => {
  13. // 目标 DOM 路径
  14. const targetXPath = '/html/body/div[1]/div/div[2]/header/div[2]/div/div/div[1]/h1';
  15.  
  16. // 使用 XPath 查找目标元素
  17. const hideElementByXPath = (xpath) => {
  18. const targetElement = document.evaluate(
  19. xpath,
  20. document,
  21. null,
  22. XPathResult.FIRST_ORDERED_NODE_TYPE,
  23. null
  24. ).singleNodeValue;
  25.  
  26. if (targetElement) {
  27. targetElement.style.display = 'none';
  28. }
  29. };
  30.  
  31. // 初始化时隐藏元素
  32. hideElementByXPath(targetXPath);
  33.  
  34. // 监听 DOM 变化,实时隐藏新生成的标题
  35. const observer = new MutationObserver((mutationsList) => {
  36. mutationsList.forEach((mutation) => {
  37. if (mutation.type === 'childList' || mutation.type === 'subtree') {
  38. hideElementByXPath(targetXPath);
  39. }
  40. });
  41. });
  42.  
  43. // 配置 observer,监听整个文档的 DOM 变化
  44. observer.observe(document.body, {
  45. childList: true,
  46. subtree: true,
  47. });
  48. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址