Skip to content

Instantly share code, notes, and snippets.

@Youngv
Created September 13, 2017 09:37
Show Gist options
  • Save Youngv/c4680131886e64da8bb9cc070673ac1e to your computer and use it in GitHub Desktop.
Save Youngv/c4680131886e64da8bb9cc070673ac1e to your computer and use it in GitHub Desktop.
屏蔽知乎网页版首页中夹杂的广告
// ==UserScript==
// @name 屏蔽知乎网页版首页中夹杂的广告
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 屏蔽知乎网页版首页中夹杂的广告
// @author Victor Young
// @match https://*.zhihu.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
function onElementHeightChange(elm, callback){
var lastHeight = elm.clientHeight, newHeight;
(function run(){
newHeight = elm.clientHeight;
if( lastHeight != newHeight )
callback();
lastHeight = newHeight;
if( elm.onElementHeightChangeTimer )
clearTimeout(elm.onElementHeightChangeTimer);
elm.onElementHeightChangeTimer = setTimeout(run, 200);
})();
}
function findAncestor (el, cls) {
while ((el = el.parentNode) && el.className.indexOf(cls) < 0);
return el;
}
onElementHeightChange(document.body, function(){
var classNames = ['Advert', 'Advert--card'];
classNames.forEach(function(className) {
Array.from(document.getElementsByClassName(className)).forEach(
function(element, index, array) {
findAncestor(element, 'Card').style.display = 'none';
}
);
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment