Twitter Endless Scroll [X]

Endless scrolling between image posts with mousewheel

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Twitter Endless Scroll [X]
// @namespace    Twitter
// @version      1.7
// @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, img_src;
	let img = new Image();
	let block_selector = 'article'
	let content_selector = 'img[src*="https://pbs.twimg.com/media/"]'; //, img[src*="pbs.twimg.com/tweet_video_thumb/"]';//, img[src*="pbs.twimg.com/amplify_video_thumb/"]';
	let offset_selector = '[data-testid="cellInnerDiv"]'

	function changePost(shift, useFirst = true){
		let posts = [...document.querySelectorAll(`${block_selector}:has(${content_selector})`)];
		if (DEBUG) console.log(posts);
		let index = posts.indexOf(document.querySelector(`${block_selector}:has([href='${location.pathname}'])`));
		if (DEBUG) console.log(`lastIndex: ${lastIndex}`);
		if (DEBUG) console.log(`index: ${index}`);
		if (index<0){ //needed anymore?
			index = lastIndex;
		}

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

		if (nextPost){
			nextPost.scrollIntoView();
			let elements = nextPost.querySelectorAll(content_selector);
			((elements.length==1 || useFirst) ? elements[0] : elements[elements.length - 1])?.click();
			//nextPost.querySelector(content_selector)?.click();
			shifted = true;
			lastIndex = index+shift;
			//pre-cache
			// img_src = posts[index+shift*2]?.querySelector(content_selector)?.src;
			// if (DEBUG) console.log(`pre img_src: ${img_src}`); //absolutely random and chaotic
			//findValidImage(img_src, ["large", "4096x4096"]);
		}
	}

// 	async function findValidImage(img_src, qualityList) {
// 		const prefix = img_src.split("name=")[0];
// 		let i = 0;

// 		function checkImage(url) {
// 			return new Promise(resolve => {
// 				const img = new Image();
// 				img.onload = () => resolve(true);
// 				img.onerror = () => {
// 					if (DEBUG) console.log(`img.onerror`);
// 					resolve(false);
// 				}
// 				img.src = url;
// 			});
// 		}

// 		for (let name of qualityList) {
// 			const testUrl = prefix + "name=" + name;
// 			const isValid = await checkImage(testUrl);
// 			if (isValid) {
// 				if (DEBUG) console.log(`img_src: ${testUrl}`);
// 				return testUrl;
// 			}
// 		}
// 	}

	function image_count(){
		return document.querySelectorAll(`${block_selector} [href*='${location.pathname.replace(/\/\d+$/, "")}']`)?.length ?? 0
	}

	$('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"]')) && image_count() > 1){
				left.click();
			}
			else
				changePost(-1, false);
		}

		let right;
		if (e.originalEvent.deltaY > 0){
			if ((right = document.querySelector('[data-testid="Carousel-NavRight"]')) && image_count() > 1){
				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(`${offset_selector}`).style.transform.match(/\d+/)?.[0];
			window.scroll(0, offset);
			shifted = false;
		}, 500);
	})
})();