知乎浏览体验优化

优化PC端浏览未登录状态下浏览知乎体验:动态移除登录窗口、规避新标签页打开链接、首页登录页重定向至热门页面;

当前为 2022-04-25 提交的版本,查看 最新版本

// ==UserScript==
// @name         知乎浏览体验优化
// @namespace    https://greasyfork.org/zh-CN/users/893587-limbopro
// @version      0.0.1
// @license      CC BY-NC-SA 4.0
// @description  优化PC端浏览未登录状态下浏览知乎体验:动态移除登录窗口、规避新标签页打开链接、首页登录页重定向至热门页面;
// @author       limbopro
// @match        https://zhuanlan.zhihu.com/*
// @match        https://www.zhihu.com/*
// @run-at       document-end
// @grant        none
// ==/UserScript==

//// 有使用 QuantumultX 和 surge 等代理工具的用户
// 请参阅 https://limbopro.com/archives/12904.html 配置去广告分流
/// 一起用 香喷喷

var zhihu_id = "zhihux";
var zhihuAds = "[class='Card AppBanner'],.Footer,.Banner-link,div.Pc-word {display:none ! important; pointer-events: none !important;}";
button_dynamicRemove("[class='Button Modal-closeButton Button--plain']", 10);
button_dynamicAppend("header[role='banner']", "清理中! ♻️", "undefined", "position:fixed; right:0px;", zhihu_id);
css_adsRemove(zhihuAds, 100, "hloyx");
indexLogin();
window.onload = href_attributeSet(500, zhihu_id);
window.onload = addListener("a[class*='css-'],button[class='Button ContentItem-action Button--plain Button--withIcon Button--withLabel']", () => { href_attributeSet(500, zhihu_id) });
// 循环判定整个页面 scrollHeight 是否变化
var body_scrollHeightCheck = setInterval(() => {
    var body_scrollHeight_then = document.body.scrollHeight;
    setTimeout(() => {
        var body_scrollHeight_now = document.body.scrollHeight;
        if (body_scrollHeight_now > body_scrollHeight_then) {
            href_attributeSet(500, zhihu_id);
        }
    }, 500);
}, 500);

// 循环判定评论框是否存在且 scrollHeight 是否有变化
var comment_scrollHeightCheck = setInterval(() => {
    let comment = document.querySelectorAll("div.CommentListV2");
    if (comment.length > 0) {
        var comment_scrollHeight_then = comment[0].scrollHeight;
        setTimeout(() => {
            var comment_scrollHeight_now = comment[0].scrollHeight;
            if (comment_scrollHeight_now > comment_scrollHeight_then) {
                href_attributeSet(500, zhihu_id);
            }
        }, 500)
    }
}, 500)

// 循环模拟模拟点击
function button_dynamicRemove(selector, times) {
    var initCount = 0;
    var loop = setInterval(() => {
        var ele = document.querySelectorAll(selector);
        if (ele.length > 0) {
            ele[0].click()
        }
        initCount += 1;
        if (initCount == times) {
            clearInterval(loop);
        }
    }, 0)
}

// 在页面动态插入按钮并赋予 onclick 属性
function button_dynamicAppend(ele, text, onclick, position, id) {
    var button = document.createElement("button");
    button.innerHTML = text;
    button.setAttribute("onclick", onclick);
    button.setAttribute("id", id);
    var button_style_values = position + "padding: 6px 6px 6px 6px; display: inline-block; " +
        "font-size: 15px; color:white; z-index:114154; border-right: 6px solid #38a3fd !important; " +
        "border-left: #292f33 !important; border-top: #292f33 !important; " +
        "border-bottom: #292f33 !important; background: black; " +
        "border-radius: 0px 0px 0px 0px; margin-bottom: 10px; " +
        "font-weight: 800 !important; " +
        "text-align: right !important;"
    button.setAttribute("style", button_style_values);
    var here = document.querySelectorAll(ele);
    if (here.length > 0) {
        here[0].insertBefore(button, here[0].childNodes[3])
        //here[0].appendChild(button);
        console.log("按钮已添加;")
    }
}

// 动态创建引用内部资源 内嵌式样式 内嵌式脚本
function css_adsRemove(newstyle, delaytime, id) {
    setTimeout(() => {
        var creatcss = document.createElement("style");
        creatcss.id = id;
        creatcss.innerHTML = newstyle;
        document.getElementsByTagName('head')[0].appendChild(creatcss)
        console.log("CSS样式新增完毕!");
    }, delaytime);
}

// 知乎循环跳转绕过登录页
function indexLogin() { // 跳转至热门话题 Explore 或 随机
    var url = document.location.href;
    var url_list = [
        "https://www.zhihu.com/knowledge-plan/hot-question/hot/",
    ]
    var rand = Math.floor(Math.random() * url_list.length);
    var url_random = url_list[rand];
    var reg = /^https:\/\/www.zhihu.com\/signin.*/gi;
    if (url.search(reg) !== -1) {
        window.location = url_random;
    }
}

// 禁止新页面跳转另一种实现 循环
function href_attributeSet(time, id) {
    document.getElementById(id).style.background = "black";
    document.getElementById(id).innerHTML = "清理中! ♻️";
    setTimeout(() => {
        // 监控页面是否有新的 button
        let selector = "button[class*='Button PaginationButton']";
        let ele_button = document.querySelectorAll(selector);
        if (ele_button.length > 0) {
            window.onload = addListener(selector, () => { href_attributeSet(time, id) });
        }
        let times = 0;
        let loop = setInterval(() => {
            // 修改属性
            times += 1;
            let href = document.querySelectorAll("a");
            let i;
            for (i = 0; i < href.length; i++) {
                if (href[i].target == "_blank") {
                    href[i].setAttribute("target", "_self");
                }
            }
            let href_Length = document.querySelectorAll("a[target='_blank']").length;
            if (href_Length === 0 && times >= 2) {
                clearInterval(loop);
                if (document.getElementById(id)) {
                    document.getElementById(id).innerHTML = "100%! ♻️";
                    document.getElementById(id).style.background = "green";
                    console.log("循环第" + times + "遍;")
                    console.log("清理完毕!");
                }
            }
        }, time)
    }, time)
}

/* 添加监听器 */
function addListener(selector, funx) {
    setTimeout(() => {
        var ele = document.querySelectorAll(selector);
        for (let index = 0; index < ele.length; index++) {
            ele[index].addEventListener("click", funx, false)
        }
    }, 1000)
}