Twitter Endless Scroll [X]

Endless scrolling between image posts with mousewheel

  1. // ==UserScript==
  2. // @name Twitter Endless Scroll [X]
  3. // @namespace Twitter
  4. // @version 1.5
  5. // @description Endless scrolling between image posts with mousewheel
  6. // @author NightLancerX
  7. // @match https://x.com/*
  8. // @match https://twitter.com/*
  9. // @match https://mobile.twitter.com/*
  10. // @icon https://www.google.com/s2/favicons?sz=64&domain=twitter.com
  11. // @license CC-BY-NC-SA
  12. // @grant none
  13. // @require https://code.jquery.com/jquery-3.3.1.min.js
  14. // @run-at document-end
  15. // @noframes
  16. // ==/UserScript==
  17.  
  18. (function() {
  19. 'use strict';
  20.  
  21. let nextPost, shifted = false;
  22.  
  23. function changePost(shift){
  24. let selector = 'img[src*="https://pbs.twimg.com/media/"]';
  25. let posts = [...document.querySelectorAll('[data-testid="cellInnerDiv"] img[src*="https://pbs.twimg.com/media/"]')];
  26. //console.log(posts);
  27. let index = posts.indexOf(document.querySelector(`[href='${location.pathname}']`)?.querySelector('img[src*="https://pbs.twimg.com/media/"]'));
  28. //console.log(index);
  29.  
  30. nextPost = posts[index+shift];
  31. //console.log(nextPost);
  32.  
  33. nextPost?.scrollIntoView();
  34. nextPost?.click();
  35. shifted = true;
  36. }
  37.  
  38. $('body').on('wheel', '[aria-labelledby="modal-header"]', function(e){
  39. e.preventDefault();
  40. e.stopPropagation();
  41.  
  42. let left;
  43. if (e.originalEvent.deltaY < 0){
  44. if (left = document.querySelector('[data-testid="Carousel-NavLeft"]'))
  45. left.click();
  46. else
  47. changePost(-1);
  48. }
  49.  
  50. let right;
  51. if (e.originalEvent.deltaY > 0){
  52. if (right = document.querySelector('[data-testid="Carousel-NavRight"]'))
  53. right.click();
  54. else
  55. changePost(+1);
  56. }
  57. });
  58.  
  59. $('body').on('click', '[aria-labelledby="modal-header"] [data-testid="swipe-to-dismiss"]', function(e){
  60. if (shifted) setTimeout(()=>{
  61. let offset = nextPost?.closest('[data-testid="cellInnerDiv"]').style.transform.match(/\d+/)?.[0];
  62. window.scroll(0, offset);
  63. shifted = false;
  64. }, 500);
  65. })
  66. })();