Greasy Fork

Greasy Fork is available in English.

知乎-我不感兴趣

隐藏掉你可能感兴趣

当前为 2018-02-18 提交的版本,查看 最新版本

// ==UserScript==
// @name         知乎-我不感兴趣
// @namespace
// @version      2.0
// @description  隐藏掉你可能感兴趣
// @author       MQ
// @match        https://www.zhihu.com/
// @grant        none
// @namespace http://greasyfork.icu/users/159603
// ==/UserScript==

(function() {
    'use strict';
    let GET = (url, headers) => {
        let xhr = new XMLHttpRequest();
        xhr.open("GET", url, false);
        if (headers) {
            Object.keys(headers).forEach((key) => { xhr.setRequestHeader(key, headers[key]); });
        }
        xhr.send();
        return xhr.response;
    };

    //以下部分获取用户ID
    //获取右上角头像按钮
    const avatar = document.querySelector("button.Button.AppHeader-profileEntry.Button--plain");
    //模拟点击
    avatar.click();
    //获取弹出菜单
    const homeElm = document.querySelector("a.Button.Menu-item.AppHeaderProfileMenu-item.Button--plain");
    console.log(homeElm);
    //获取主页链接
    const homeLink = homeElm.href;
    console.log(homeLink);
    //从链接中匹配用户ID
    const userId = homeLink.match(/(?<=people\/).*/).toString();
    console.log("user id:", userId);
    //关闭弹出菜单
    avatar.click();

    //以下部分获取用户关注话题,知乎这前端太辣鸡了
    //获取话题页面
    const rawTopicsPage = GET(`/people/${userId}/following/topics`);
    //得到的是一坨raw的html,parse一下
    const topicsPageDOM = new DOMParser().parseFromString(rawTopicsPage, "text/html");
    //里面的这个attribute的值是一个json
    const rawTopicsPageData = topicsPageDOM.querySelector("div#data").getAttribute("data-state");
    //parse一下
    const userData = JSON.parse(rawTopicsPageData);
    //里面有你关注的所有话题
    const followedTopics = Object.values(userData.entities.topics).map(e=>e.name);
    console.log("your followed topics: ", followedTopics);

    const mainFrame = document.querySelector(".TopstoryMain");

    var ob = new MutationObserver(function () {
        //console.log("change triggered");
        document.querySelectorAll(".Card.TopstoryItem").forEach(e => {
            try{
                let feature = e.querySelector(".FeedSource-firstline");
                let doHide = false;
                if (feature.textContent.match(/你可能感兴趣|热门内容/)) {
                    doHide = true;
                    console.log(feature.textContent, "hide");
                } else if (feature.textContent.match(/来自话题/)) {
                    var topicDivs = [...feature.querySelectorAll("div.Popover div")];
                    var topicNames = topicDivs.map(e=>e.textContent);
                    doHide = true;
                    for(let topicName of topicNames) {
                        if(followedTopics.includes(topicName)) {
                            doHide = false;
                            break;
                        }
                    }
                    console.log(topicNames, doHide ? "hide" : "not hide");
                } else {
                    console.warn("unknown: ", e);
                }
                if (doHide) {
                    e.hidden = true;
                    //[...e.children].forEach(inner => inner.hidden = true);
                }
            }catch(x){
                console.log(x, e);
            }
        });
    });

    var config = { attributes: true, childList: true };

    ob.observe(mainFrame, config);

    console.log("hooked");
}());