Skip to content

Instantly share code, notes, and snippets.

@silver-mixer
Created October 31, 2023 12:46
Show Gist options
  • Save silver-mixer/6e1a84f1c6154a60bc1bd2657a349f01 to your computer and use it in GitHub Desktop.
Save silver-mixer/6e1a84f1c6154a60bc1bd2657a349f01 to your computer and use it in GitHub Desktop.
Memorizing tweet scroll position
// ==UserScript==
// @name Twitter Tweet Scroll Position Memorizer
// @namespace https://estar-lab.net/
// @version 1.0
// @description Memorizing tweet scroll position
// @author silver-mixer
// @match https://twitter.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=twitter.com
// @grant none
// ==/UserScript==
(() => {
'use strict';
const MAX_SCROLL_HISTORIES = 10;
let scrollPositionMap = new Map();
let lastUrl = null;
window.addEventListener('scroll', () => {
let url = location.href;
if(url.includes('/status/')){
if(scrollPositionMap.has(url)){
if(lastUrl !== url){
window.scrollBy(window.scrollX, scrollPositionMap.get(url));
}
scrollPositionMap.delete(url);
}
scrollPositionMap.set(url, window.scrollY);
if(scrollPositionMap.size > MAX_SCROLL_HISTORIES){
scrollPositionMap.delete(scrollPositionMap.keys().next().value);
}
lastUrl = url;
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment