Greasy Fork is available in English.

牛客网Web端首页->"推荐"栏目,免登录点击帖子标题查看帖子

牛客网Web端首页->"推荐"栏目,免登录点击帖子标题查看帖子。

// ==UserScript==
// @name         牛客网Web端首页->"推荐"栏目,免登录点击帖子标题查看帖子
// @name:en     Nowcoder Web - View Posts in "Recommended" Without Login
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description:en    Nowcoder Web home page -> "Recommended" column, click the title of the post to view the post without logging-in
// @author       aspen138
// @match        https://www.nowcoder.com/*
// @license      MIT
// @grant        none
// @description 牛客网Web端首页->"推荐"栏目,免登录点击帖子标题查看帖子。
// ==/UserScript==

(function() {
    'use strict';


    // 隐藏登录弹窗
    function hideLoginModal() {
        // 假设弹窗有特定的class name "login-dialog"
        // 这个selector可能需要根据实际弹窗的HTML结构进行调整
        var modal = document.querySelector('.login-dialog');
        if (modal) {
            modal.style.display = 'none';
            console.log('登录弹窗已隐藏');
        }
    }

    // 页面加载完成后隐藏登录弹窗
    window.addEventListener('load', hideLoginModal);

    // 如果弹窗是动态添加的,可能需要使用MutationObserver来监听DOM的变化
    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.addedNodes.length) {
                hideLoginModal();
            }
        });
    });

    // 监听document.body的变化
    observer.observe(document.body, { childList: true, subtree: true });


    // 通用函数用于移除URL中的特定查询参数
    function removeQueryString(url, parameter) {
        var urlParts = url.split('?');
        if (urlParts.length >= 2) {
            // 参数键值对数组
            var params = urlParts[1].split(/[&;]/g);
            // 搜索特定参数并移除它
            for (var i = params.length; i-- > 0; ) {
                if (params[i].split('=')[0] === parameter) {
                    params.splice(i, 1);
                }
            }
            url = urlParts[0] + (params.length > 0 ? '?' + params.join('&') : "");
            return url;
        } else {
            return url;
        }
    }

    // 监听所有点击事件
    document.addEventListener('click', function(e) {
        console.log("点击事件")
        // 确保点击事件是针对a标签
        var target = e.target.closest('a');
        if (target) {
            console.log("点击a标签")
            var href = target.getAttribute('href');
            // 如果链接包含 '?sourceSSR=home',则移除它并导航到新的链接
            if (href && href.includes('?sourceSSR=home')) {
                console.log("捕获成功")
                e.preventDefault();
                var newHref = removeQueryString(href, 'sourceSSR');
                window.location.href = newHref;
            }
        }
    },true);
})();