Twitter Endless Scroll [X]

Endless scrolling between image posts with mousewheel

Verze ze dne 03. 07. 2024. Zobrazit nejnovější verzi.

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         Twitter Endless Scroll [X]
// @namespace    Twitter
// @version      1.5
// @description  Endless scrolling between image posts with mousewheel
// @author       NightLancerX
// @match        https://x.com/*
// @match        https://twitter.com/*
// @match        https://mobile.twitter.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=twitter.com
// @license      CC-BY-NC-SA
// @grant        none
// @require      https://code.jquery.com/jquery-3.3.1.min.js
// @run-at       document-end
// @noframes
// ==/UserScript==

(function() {
	'use strict';

	let nextPost, shifted = false;

	function changePost(shift){
		let selector = 'img[src*="https://pbs.twimg.com/media/"]';
		let posts = [...document.querySelectorAll('[data-testid="cellInnerDiv"]')];
		let index = posts.indexOf($(`[href='${location.pathname}']`).parents('[data-testid="cellInnerDiv"]')[0]);

		if (shift<0)
			nextPost = [...posts[index+shift]?.querySelectorAll(selector)||[]].pop();
		else
			nextPost = posts[index+shift]?.querySelector(selector);

		nextPost?.scrollIntoView();
		nextPost?.click();
		shifted = true;
	}

	$('body').on('wheel', '[aria-labelledby="modal-header"]', function(e){
		e.preventDefault();
		e.stopPropagation();

		let left;
		if (e.originalEvent.deltaY < 0){
			if (left = document.querySelector('[data-testid="Carousel-NavLeft"]'))
				left.click();
			else
				changePost(-1);
		}

		let right;
		if (e.originalEvent.deltaY > 0){
			if (right = document.querySelector('[data-testid="Carousel-NavRight"]'))
				right.click();
			else
				changePost(+1);
		}
	});

	$('body').on('click', '[aria-labelledby="modal-header"] [data-testid="swipe-to-dismiss"]', function(e){
		if (shifted) setTimeout(()=>{
			let offset = nextPost?.closest('[data-testid="cellInnerDiv"]').style.transform.match(/\d+/)?.[0];
			window.scroll(0, offset);
			shifted = false;
		}, 500);
	})
})();