移除知乎网页版首页广告

移除知乎首页广告

目前为 2022-01-13 提交的版本。查看 最新版本

// ==UserScript==
// @name         移除知乎网页版首页广告
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  移除知乎首页广告
// @author       chaochaogege
// @match        https://www.zhihu.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    const targetNode = document.querySelector('.Topstory-recommend > div');

    // Options for the observer (which mutations to observe)
    const config = {childList: true };

    // Callback function to execute when mutations are observed
    const callback = function(mutationsList, observer) {
        for (let i = 0 ; i < mutationsList.length ; i ++) {
            const mutation = mutationsList[i]
            const target = mutation.target
            const length = target.children.length
            const addedNodes = mutation.addedNodes
            if (addedNodes.length === 0 ) continue
            const c = addedNodes[0].classList[2]
            if ( c === "TopstoryItem--advertCard") {
                target.children[length - i - 2].remove()
            }
        }
    };

    // Create an observer instance linked to the callback function
    const observer = new MutationObserver(callback);

    // Start observing the target node for configured mutations
    observer.observe(targetNode, config)
    for (const n of targetNode.children){
        if (n.classList[2] === "TopstoryItem--advertCard" ) {
            n.remove()
        }
    }
})();

QingJ © 2025

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