知乎页面优化

对知乎内容进行优化展示

当前为 2023-01-31 提交的版本,查看 最新版本

// ==UserScript==
// @name         知乎页面优化
// @namespace    http://tampermonkey.net/
// @version      0.41
// @description  对知乎内容进行优化展示
// @author       xy
// @match        *://www.zhihu.com/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @license      MIT
// ==/UserScript==

console.log('Hi ZhiHu ~~');

const frequency = 100;

// 去除相关推荐等广告
const removeList = [
    // 「推荐关注」
    '.Card.css-173vipd',
    // 「相关推荐」
    'div[data-za-detail-view-path-module_name="相关推荐"]',
    // 「Footer」
    '.Footer'
];

const intervalIdList = [];
removeList.forEach(item => {
    let intervalID = setInterval(() => {
        let removeElement = document.querySelector(item)
        if (removeElement) {
            console.log("移除", removeElement);
            removeElement.remove();
        }
    }, frequency);
    intervalIdList.push(intervalID);
});

// 10s后去除相关定时器
const durationTime = 10000;
setTimeout(() => {
    removeList.forEach(item => clearInterval(item));
}, durationTime);

/**
 * 移除热榜右边元素
 */
function removeHotPageRight() {
    let rightElement = document.querySelector('.css-1qyytj7');
    if (rightElement) {
        rightElement.innerHTML = '';
    }
}

function reconstructHotPageRight(titleList) {
    let rightElement = document.querySelector('.css-1qyytj7');
    let div = document.createElement('div');
    rightElement.appendChild(div);

    titleList.forEach(title => {
        let p = document.createElement('p');
        p.addEventListener('click', () => title.element.scrollIntoView());
        p.innerText = title.index + ' ' + title.title;
        div.appendChild(p);
    })
}

// 热榜
let regx = /战争|乌克兰|俄罗斯/
function getHotContent() {
    const hotItemList = document.querySelectorAll('.HotItem-content');
    const titleList = [];
    hotItemList.forEach((hotItem, index) => {
        let title = hotItem.querySelector('h2[class="HotItem-title"]').innerText;
        let excerpt = hotItem.querySelector('p[class="HotItem-excerpt"]');
        excerpt = excerpt && excerpt.innerText;

        let content = title + excerpt;

        if (content.match(regx)) {
            console.log(index + 1, title);
            console.log(excerpt);

            let hotItemObj = {};
            hotItemObj.index = index + 1;
            hotItemObj.title = title;
            hotItemObj.element = hotItem;

            titleList.push(hotItemObj);
        }
    })
    reconstructHotPageRight(titleList);
}

removeHotPageRight();
getHotContent();

QingJ © 2025

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