Twitter Endless Scroll [X]

Endless scrolling between image posts with mousewheel

ของเมื่อวันที่ 03-07-2024 ดู เวอร์ชันล่าสุด

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

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