Last active
July 20, 2021 09:41
-
-
Save ZeppLu/9b8f36602d428adf71554eadb3443585 to your computer and use it in GitHub Desktop.
清理知乎页面。清理内容:首页推荐的短视频、问题页面的“盐选推荐”回答。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Zhihu Cleaner | |
// @name:zh-CN 知乎界面清理 | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author You | |
// @match https://www.zhihu.com/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const logging = true; | |
const enabled = true; | |
// create and return a closure | |
const cleanerCreator = (selector) => { | |
return (items) => { | |
const ngItems = items.filter( item => item.querySelector(selector) ); | |
ngItems.forEach( item => {item.hidden = enabled;} ); | |
if (logging) { | |
console.log(`ZHIHU CLEANER: ${ngItems.length}/${items.length} items cleaned (${selector})`); | |
} | |
} | |
} | |
// modified from https://greasyfork.org/zh-CN/scripts/35275-知乎-我不感兴趣/code, thanks! | |
// listen on change events of a child list | |
const registerHook = (parent, rmSelector) => { | |
const cleaner = cleanerCreator(rmSelector); | |
if (!parent.children) { | |
setTimeout(() => {registerHook(parent, rmSelector);} , 500); | |
} | |
const ob = new MutationObserver((records) => { | |
const addedItems = records.reduce((r, e) => r.concat([...e.addedNodes]), []); | |
cleaner(addedItems); | |
}); | |
ob.observe(parent, {childList: true}); | |
cleaner([...parent.children]); | |
}; | |
// main page, remove zhihu short videos | |
if (/.*www\.zhihu\.com\/?$/.test(location.href)) { | |
const firstCard = document.querySelector("div.Card.TopstoryItem"); | |
registerHook(firstCard.parentElement, ".ZVideoItem"); | |
} | |
// question page, remove ad answer by 盐选 | |
if (/.*www\.zhihu\.com\/question\/\d+$/.test(location.href)) { | |
const yanxuanIDs = [ "zhujiangren" // 盐选推荐 | |
, "yan-xuan-ke-pu-28" // 盐选科普 | |
]; | |
const yanxuan = yanxuanIDs.map(id => `meta[content="https://www.zhihu.com/people/${id}"]`); | |
const firstAnswer = document.querySelector("div.List-item"); | |
registerHook(firstAnswer.parentElement, yanxuan); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment