知乎增强

去除广告和快速折叠

  1. // ==UserScript==
  2. // @name 知乎增强
  3. // @namespace https://gitee.com/linhq1999/OhMyScript
  4. // @version 1.1
  5. // @description 去除广告和快速折叠
  6. // @author LinHQ
  7. // @match https://www.zhihu.com/*
  8. // @grant GM_setClipboard
  9. // ==/UserScript==
  10. 'use strict';
  11. let ads = document.getElementsByClassName("TopstoryItem--advertCard"), side_ads = document.getElementsByClassName("Pc-card Card"), btns = document.getElementsByTagName("button"), modal = document.getElementsByClassName('Button Modal-closeButton Button--plain');
  12. function scan() {
  13. var _a;
  14. // 移除 “范围” 上的图片
  15. (_a = document.querySelector(".GlobalSideBar-category img")) === null || _a === void 0 ? void 0 : _a.remove();
  16. // 移除侧边栏广告
  17. for (const sad of side_ads) {
  18. sad.remove();
  19. }
  20. // 移除主要广告
  21. for (const adv of ads) {
  22. adv.remove();
  23. }
  24. }
  25. (function () {
  26. var _a;
  27. document.body.addEventListener("keydown", e => {
  28. var _a, _b, _c;
  29. if (e.ctrlKey && e.key == 'c') {
  30. GM_setClipboard((_b = (_a = window.getSelection()) === null || _a === void 0 ? void 0 : _a.toString().trim()) !== null && _b !== void 0 ? _b : "");
  31. }
  32. if (e.key == 'c' && e.altKey) {
  33. // 先检查弹出式评论
  34. if (modal.length != 0) {
  35. modal[0].click();
  36. return;
  37. }
  38. if (btns.length != 0) {
  39. for (const btn of btns) {
  40. if (btn != null && ((_c = btn.textContent) === null || _c === void 0 ? void 0 : _c.includes("收起"))) {
  41. btn.click();
  42. }
  43. }
  44. }
  45. }
  46. e.stopImmediatePropagation();
  47. });
  48. try {
  49. new MutationObserver((cl, ob) => {
  50. scan();
  51. }).observe((_a = document.querySelector("#root")) !== null && _a !== void 0 ? _a : document.body, { subtree: true, childList: true });
  52. }
  53. catch (error) {
  54. // 某些网站覆盖了 api,可以解决但我不想写了
  55. console.warn("Fallback to interval!");
  56. setInterval(scan, 1000);
  57. }
  58. })();