Skip to content

Instantly share code, notes, and snippets.

@taikulawo
Created November 7, 2019 09:33
Show Gist options
  • Save taikulawo/9dd41c7089530b94fdc030216ae0817a to your computer and use it in GitHub Desktop.
Save taikulawo/9dd41c7089530b94fdc030216ae0817a to your computer and use it in GitHub Desktop.
移除知乎首页广告-油猴脚本
// ==UserScript==
// @name 移除知乎网页版首页广告
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 移除知乎首页广告
// @author You
// @match https://www.zhihu.com/
// @grant none
// ==/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()
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment