Only Follow

Redirect Bilibili/Zhihu homepages to follow feeds, even with URL parameters or new tab opens.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Only Follow
// @namespace    http://tampermonkey.net/
// @version      2.1
// @description  Redirect Bilibili/Zhihu homepages to follow feeds, even with URL parameters or new tab opens.
// @author       Rive
// @match        *://*.bilibili.com/*
// @match        *://*.zhihu.com/*
// @grant        none
// @license MIT
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    /**
     * SILENT MODE:
     * Hide the body immediately to prevent the homepage from flashing.
     */
    function hidePage() {
        if (document.documentElement) {
            document.documentElement.style.display = 'none';
        }
    }

    /**
     * REDIRECT LOGIC:
     * Checks if the current page is the actual homepage (ignoring search parameters).
     */
    function performRedirect() {
        const hostname = window.location.hostname;
        const pathname = window.location.pathname;

        // Bilibili: Match www.bilibili.com/ or www.bilibili.com/index.html
        if (hostname === "www.bilibili.com" && (pathname === "/" || pathname === "/index.html")) {
            hidePage();
            window.location.replace("https://t.bilibili.com/");
        }

        // Zhihu: Match www.zhihu.com/ (usually the feed or login page)
        if (hostname === "www.zhihu.com" && pathname === "/") {
            hidePage();
            window.location.replace("https://www.zhihu.com/follow");
        }
    }

    /**
     * CLICK INTERCEPTION:
     * Handles left-clicks on logo/home links within the same tab.
     */
    function setupClickInterception() {
        document.addEventListener('click', function(e) {
            const anchor = e.target.closest('a');
            if (!anchor) return;

            try {
                const url = new URL(anchor.href);
                // Check if the link points to Bilibili homepage
                if (url.hostname === "www.bilibili.com" && (url.pathname === "/" || url.pathname === "/index.html")) {
                    e.preventDefault();
                    window.location.href = "https://t.bilibili.com/";
                }
                // Check if the link points to Zhihu homepage
                if (url.hostname === "www.zhihu.com" && url.pathname === "/") {
                    e.preventDefault();
                    window.location.href = "https://www.zhihu.com/follow";
                }
            } catch (err) {
                // Ignore invalid URLs
            }
        }, true);
    }

    // Execute immediately to handle direct access and "Open in new tab"
    performRedirect();

    // Initialize click listener after DOM begins to load
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', setupClickInterception);
    } else {
        setupClickInterception();
    }

})();