知乎去广告

去除广告和快速折叠

目前為 2021-07-16 提交的版本,檢視 最新版本

// ==UserScript==
// @name         知乎去广告
// @namespace    https://gitee.com/linhq1999/OhMyScript
// @version      0.5
// @description  去除广告和快速折叠
// @author       LinHQ
// @match        https://www.zhihu.com/*
// @grant        none
// ==/UserScript==
'use strict';

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

var ads = document.getElementsByClassName("TopstoryItem--advertCard"),
    side_ads = document.getElementsByClassName("Pc-card Card"),
    btns = document.getElementsByTagName("button"),
    modal = document.getElementsByClassName('Button Modal-closeButton Button--plain');

function scan() {
  var _document$querySelect;

  // 移除侧边栏广告
  var _iterator = _createForOfIteratorHelper(side_ads),
      _step;

  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var sad = _step.value;
      sad.remove();
    } // 移除左侧广告

  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }

  var _iterator2 = _createForOfIteratorHelper(ads),
      _step2;

  try {
    for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
      var adv = _step2.value;
      adv.remove();
    } // 移除 “范围” 上的图片

  } catch (err) {
    _iterator2.e(err);
  } finally {
    _iterator2.f();
  }

  (_document$querySelect = document.querySelector(".GlobalSideBar-category img")) === null || _document$querySelect === void 0 ? void 0 : _document$querySelect.remove();
}

(function () {
  document.body.addEventListener("keydown", function (e) {
    if (e.key == 'c' && e.altKey) {
      // 先检查弹出式评论
      if (modal.length != 0) {
        modal[0].click();
        return;
      }

      if (btns.length != 0) {
        var _iterator3 = _createForOfIteratorHelper(btns),
            _step3;

        try {
          for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
            var _btn$textContent;

            var btn = _step3.value;

            if (btn != null && (_btn$textContent = btn.textContent) !== null && _btn$textContent !== void 0 && _btn$textContent.includes("收起")) {
              btn.click();
            }
          }
        } catch (err) {
          _iterator3.e(err);
        } finally {
          _iterator3.f();
        }
      }
    }
  });

  try {
    new MutationObserver(function (cl, ob) {
      scan();
    }).observe(document.body, {
      childList: true
    });
  } catch (error) {
    // 某些网站覆盖了 api,可以解决但我不想写了
    console.warn("Fallback to interval!");
    setInterval(scan, 1000);
  }
})();