X (Twitter) Bookmark Scroll Position Keeper

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

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği indirebilmeniz için ayrıca Tampermonkey gibi bir eklenti kurmanız gerekmektedir.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

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.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==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);
})();