Twitter Endless Scroll [X]

Endless scrolling between image posts with mousewheel

// ==UserScript==
// @name         Twitter Endless Scroll [X]
// @namespace    Twitter
// @version      1.6
// @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';
	const DEBUG = false;

	let nextPost, shifted = false, lastIndex = 0;
	let img = new Image();
	let selector = 'img[src*="pbs.twimg.com/media/"]';//, img[src*="pbs.twimg.com/tweet_video_thumb/"]';//, img[src*="pbs.twimg.com/amplify_video_thumb/"]';

	function changePost(shift){
		let posts = [...document.querySelectorAll(`[data-testid="cellInnerDiv"] [href*="${location.pathname.match(/^\/[^\/]+/)[0]}"] ${selector}`)];
		if (DEBUG) console.log(posts);
		let index = posts.indexOf(document.querySelector(`[href='${location.pathname}']`)?.querySelector('img[src*="https://pbs.twimg.com/media/"]'));
		if (index<0){
			index = lastIndex;
		}
		//let index = posts.indexOf(document.querySelectorAll(`#layers`)[0].querySelector('img[src*="https://pbs.twimg.com/"]'))
		//let t1 = document.querySelector('#layers').querySelector(`${selector}`).src.split('&')[0];
		//console.log(t1);
		//let index = posts.indexOf(document.querySelector('#layers').querySelector(`${selector}`));
		//let index = posts.indexOf(document.querySelector(`[data-testid="cellInnerDiv"] img[src*="${t1}"`));

		if (DEBUG) console.log(index);
		nextPost = posts[index+shift];
		if (DEBUG) console.log(nextPost);

		if (nextPost){
			nextPost.scrollIntoView();
			nextPost.click();
			shifted = true;
			lastIndex = index+shift;
			//pre-cache
			img.src = posts[index+shift*2]?.src?.replace('=small','=large');
		}
	}

	$('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];
			//let offset = nextPost?.closest(`[data-testid="cellInnerDiv"] [href*="${location.pathname.match(/^\/[^\/]+/)[0]}"] ${selector}`).style.transform.match(/\d+/)?.[0];
			window.scroll(0, offset);
			shifted = false;
		}, 500);
	})
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址