Advertisement
Guest User

Untitled

a guest
Jul 16th, 2023
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name             Lemmy Infinite Scrolling and Comment Sort
  3. // @description      Implements infinite scrolling and changes default sorting behavior.
  4. // @version          0.2
  5. // @author           draivin@lemmy.world
  6. // @run-at           document-start
  7. // @grant            none
  8. // @match            https://sh.itjust.works/*
  9. // @match            https://burggit.moe/*
  10. // @match            https://lemmy.world/*
  11. // @match            https://lemm.ee/*
  12. // @match            https://lemmy.ml/*
  13. // @match            https://lemmynsfw.com/*
  14. // ==/UserScript==
  15.  
  16.  
  17. // Don't forget to add your home instance to the match list above.
  18.  
  19.  
  20. const defaultSort = 'Top';
  21.  
  22. function addScript(text) {
  23.   text = text.replaceAll(`commentSort:"Hot"`, `commentSort:"${defaultSort}"`);
  24.   text = text.replaceAll(
  25.     'this.state.postsRes.data.posts',
  26.     `(window.LEAKED_HOME=this,this.state.postsRes.data.posts)`
  27.   );
  28.   var newScript = document.createElement('script');
  29.   newScript.type = 'text/javascript';
  30.   newScript.textContent = text;
  31.   var head = document.getElementsByTagName('head')[0];
  32.   head.appendChild(newScript);
  33. }
  34.  
  35. window.addEventListener('beforescriptexecute', async function (e) {
  36.   const src = e.target.src;
  37.   if (src.endsWith('client.js')) {
  38.     e.preventDefault();
  39.     e.stopPropagation();
  40.     const response = await fetch(src);
  41.     addScript(await response.text());
  42.   }
  43. });
  44.  
  45. let currentPage = 1;
  46. let ongoingRequest = false;
  47.  
  48. let lastUrl = location.href;
  49. const observer = new MutationObserver(() => {
  50.   if (location.href !== lastUrl) {
  51.     lastUrl = location.href;
  52.     currentPage = Number(new URLSearchParams(location.search).get('page') ?? 1);
  53.     ongoingRequest = false;
  54.   }
  55.  
  56.   if (document.querySelector('.post-listings') && document.querySelector('.paginator button')) {
  57.     if (document.querySelectorAll('.post-listing').length < 20) {
  58.       showEndMessage();
  59.     } else {
  60.       clearPaginator();
  61.     }
  62.   }
  63. });
  64.  
  65. observer.observe(document.documentElement, { childList: true, subtree: true });
  66.  
  67. window.addEventListener('scroll', async function (e) {
  68.   const referenceBox = document.querySelector('.post-listings')?.getBoundingClientRect();
  69.  
  70.   if (scrollY == 0 || ongoingRequest || !referenceBox || !LEAKED_HOME) return;
  71.  
  72.   if (LEAKED_HOME.state.postsRes.data.posts.length < 20) {
  73.     showEndMessage();
  74.     return;
  75.   }
  76.  
  77.   if (innerHeight + 300 > referenceBox.bottom) {
  78.     ongoingRequest = true;
  79.     currentPage += 1;
  80.  
  81.     const authToken = document.cookie.split('=')[1];
  82.     const communityName = location.pathname.split('/')[2] ?? '';
  83.  
  84.     let { sort, listing } = getSortAndListing();
  85.  
  86.     let query = new URLSearchParams({
  87.       page: currentPage,
  88.       limit: 20,
  89.       sort: sort,
  90.       type_: listing,
  91.       auth: authToken,
  92.     });
  93.  
  94.     if (communityName) {
  95.       query.set('community_name', communityName);
  96.     }
  97.  
  98.     try {
  99.       const curHome = LEAKED_HOME;
  100.  
  101.       showSpinner();
  102.  
  103.       const response = await fetch(`${location.origin}/api/v3/post/list?${query.toString()}`);
  104.       const posts = (await response.json()).posts;
  105.  
  106.       if (LEAKED_HOME != curHome) {
  107.         return;
  108.       }
  109.  
  110.       clearPaginator();
  111.  
  112.       LEAKED_HOME.state.postsRes.data.posts.push(...posts);
  113.       LEAKED_HOME.forceUpdate();
  114.  
  115.       if (posts.length == 20) {
  116.         ongoingRequest = false;
  117.       } else {
  118.         showEndMessage();
  119.       }
  120.     } catch {
  121.       ongoingRequest = false;
  122.     }
  123.   }
  124. });
  125.  
  126. function getSortAndListing() {
  127.   const queryParams = new URLSearchParams(location.search);
  128.   const defaultSort =
  129.     isoData?.site_res?.my_user?.local_user_view?.local_user?.default_sort_type ?? 'Active';
  130.   const defaultListing =
  131.     isoData?.site_res?.my_user?.local_user_view?.local_user?.default_listing_type ?? 'Local';
  132.  
  133.   return {
  134.     sort: queryParams.get('sort') ?? defaultSort,
  135.     listing: queryParams.get('listingType') ?? defaultListing,
  136.   };
  137. }
  138.  
  139. function showEndMessage() {
  140.   let paginator = document.querySelector('.paginator');
  141.   if (document.querySelector('.post-listing')) {
  142.     paginator.innerHTML = 'There are no more posts!';
  143.   } else {
  144.     clearPaginator();
  145.   }
  146. }
  147.  
  148. function showSpinner() {
  149.   let paginator = document.querySelector('.paginator');
  150.   paginator.innerHTML =
  151.     '<svg class="icon spin spinner-large"><use xlink:href="/static/9f75d0a/assets/symbols.svg#icon-spinner"></use><div class="visually-hidden"><title>spinner</title></div></svg>';
  152. }
  153.  
  154. function clearPaginator() {
  155.   let paginator = document.querySelector('.paginator');
  156.   paginator.innerHTML = '';
  157. }
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement