X (Twitter) Bookmark Scroll Position Keeper

Fixes automatic scroll to top after clicking on a bookmarked post in X (formerly Twitter) Bookmarks page

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         X (Twitter) Bookmark Scroll Position Keeper
// @name:zh-CN   X (推特) 书签滚动位置保持器
// @namespace    http://tampermonkey.net/
// @author       Roy WU
// @version      0.4
// @description  Fixes automatic scroll to top after clicking on a bookmarked post in X (formerly Twitter) Bookmarks page
// @description:zh-CN 修复X(前Twitter)书签页面点击书签后自动滚动到顶部的问题,保持滚动位置
// @match        https://x.com/*
// @match        https://twitter.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    
    let posY = 0;
    let isBookmarksPage = false;
    
    // 检查当前是否在书签页面
    function checkIfBookmarksPage() {
        isBookmarksPage = window.location.pathname.includes('/i/bookmarks');
        console.log('Is bookmarks page:', isBookmarksPage);
    }
    
    // 监听滚动事件
    function handleScroll() {
        if (!isBookmarksPage) return;
        
        const scrollPosition = window.scrollY;
        
        if (window.scrollY === 0) {
            console.log("back to zero!");
            window.scrollTo(0, posY);
        } else {
            posY = scrollPosition;
        }
        
        console.log(scrollPosition);
    }
    
    // 初始化
    function init() {
        checkIfBookmarksPage();
        window.addEventListener('scroll', handleScroll);
    }
    
    // 监听 URL 变化
    function observeUrlChanges() {
        const observer = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                if (location.href !== mutation._oldURL) {
                    checkIfBookmarksPage();
                }
            });
        });
        
        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }
    
    // 启动监听
    init();
    observeUrlChanges();
    
    // 使用 history API 的监听作为后备方案
    window.addEventListener('popstate', checkIfBookmarksPage);
    window.addEventListener('pushstate', checkIfBookmarksPage);
    window.addEventListener('replacestate', checkIfBookmarksPage);
})();