Suppenbiatch

TikTokViewSorter

Dec 16th, 2021 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         TikTokViewSorter
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.1
  5. // @description  Sorting a TikTok Users Page by Views
  6. // @author       Suppe
  7. // @match        https://www.tiktok.com/*
  8. // @icon         https://www.google.com/s2/favicons?domain=tiktok.com
  9. // @run-at       document-idle
  10. // @grant        none
  11. // ==/UserScript==
  12.  
  13.  
  14. (function () {
  15.     'use strict'
  16.     add_buttons_to_page()
  17.  
  18.     function addButton(text, onclick, cssObj, parentElement) {
  19.         cssObj = cssObj || {position: 'absolute', bottom: '7%', left:'4%', 'z-index': 99}
  20.         parentElement = parentElement || document.body
  21.         let button = document.createElement('button'), btnStyle = button.style
  22.         parentElement.appendChild(button)
  23.         button.innerHTML = text
  24.         button.onclick = onclick
  25.         Object.keys(cssObj).forEach(key => btnStyle[key] = cssObj[key])
  26.         return button
  27.     }
  28.  
  29.     function add_buttons_to_page() {
  30.         var buttonList = document.createElement('ul');
  31.         buttonList.setAttribute('id', 'sorter-header');
  32.         buttonList.style.listStyleType = 'none'
  33.         buttonList.style.margin = '4%';
  34.         buttonList.style.padding = '2px';
  35.         buttonList.style.textAlign = 'center';
  36.         buttonList.style.position = 'fixed';
  37.         buttonList.style.bottom = '7%';
  38.         buttonList.style.zIndex = '99';
  39.  
  40.  
  41.         var button_1 = document.createElement('li');
  42.         window.addEventListener('load', () => {
  43.             addButton('sort by views', sort_by_views, {}, button_1)
  44.         })
  45.         buttonList.appendChild(button_1);
  46.  
  47.         var button_2 = document.createElement('li');
  48.         window.addEventListener('load', () => {
  49.             addButton('scroll to bottom', scroll_to_bottom, {}, button_2)
  50.         })
  51.         buttonList.appendChild(button_2);
  52.         document.body.appendChild(buttonList);
  53.  
  54.     }
  55.  
  56.     function add_num_element(num_videos) {
  57.         var parentElement = document.getElementById('sorter-header');
  58.         var newDiv = document.getElementById('video-count');
  59.         if (newDiv == null) {
  60.             newDiv = document.createElement('div');
  61.         } else {
  62.             newDiv.innerHTML = null;
  63.         }
  64.         newDiv.style.textAlign = 'center';
  65.         newDiv.setAttribute('id', 'video-count');
  66.         var newContent = document.createTextNode(`${Number(num_videos).toLocaleString()} Videos`);
  67.  
  68.         newDiv.appendChild(newContent);
  69.         parentElement.appendChild(newDiv);
  70.     }
  71.  
  72.     async function scroll_to_bottom() {
  73.         var scrollElement = document.scrollingElement;
  74.         var scrollHeight = scrollElement.scrollHeight;
  75.         var lastHeight = 0;
  76.         console.log('Scrolling to bottom of the page');
  77.         while (lastHeight < scrollHeight) {
  78.             scrollElement.scrollTo({top: scrollHeight, left: 0, behavior: 'smooth'});
  79.             await new Promise(r => setTimeout(r, 690));
  80.             lastHeight = scrollHeight;
  81.             scrollHeight = scrollElement.scrollHeight;
  82.         }
  83.         console.log('scrolled to bottom of the page');
  84.         scrollElement.scrollTo({top: 0, left: 0, behavior: 'smooth'});
  85.     }
  86.  
  87.  
  88.     async function sort_by_views() {
  89.         console.log('Started Sorting by Views');
  90.         const pattern = /([\d.]+)([A-Z])?/;
  91.         const node_list = [];
  92.         const feed_element = document.querySelector('div[data-e2e="user-post-item-list"]');
  93.         if (feed_element == null) {
  94.             console.log('No Feed found');
  95.             return
  96.         }
  97.         const nodes = feed_element.childNodes;
  98.         console.log(`found ${nodes.length} videos`);
  99.         add_num_element(nodes.length);
  100.  
  101.         for (var i=0; i<nodes.length; i++) {
  102.             const element = nodes[i];
  103.             const views_element = element.querySelector('strong[data-e2e="video-views"]');
  104.             if (views_element == null) {
  105.                 console.log('view count not found in video item')
  106.                 continue;
  107.             }
  108.             const views_str = String(views_element.innerText);
  109.             const views_array = pattern.exec(views_str);
  110.  
  111.             var multiplier = views_array[2];
  112.             switch (multiplier) {
  113.                 case 'K':
  114.                     multiplier = 1000;
  115.                     break
  116.                 case 'M':
  117.                     multiplier = 1000000;
  118.                     break
  119.             default:
  120.                 multiplier = 1;
  121.             }
  122.             const views = parseInt(parseFloat(views_array[1]) * multiplier);
  123.             node_list.push({node: element, views: views});
  124.         }
  125.         node_list.sort((a, b) => (a.views > b.views) ? -1 : 1);
  126.  
  127.         feed_element.innerHTML = null
  128.         for (i=0; i<node_list.length; i++) {
  129.             feed_element.appendChild(node_list[i].node);
  130.         }
  131.         document.scrollingElement.scrollTo({top: 0, left: 0, behavior: 'smooth'});
  132.         console.log('Sorted by Views!');
  133.         return
  134.     }
  135. }());
Add Comment
Please, Sign In to add comment