Sort TikTok profile videos by number of views

Click on the "Videos" tab header on a TikTok profile page to sort loaded videos by descending number of views. Click again to restore the default sort.

  1. // ==UserScript==
  2. // @name Sort TikTok profile videos by number of views
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.1
  5. // @description Click on the "Videos" tab header on a TikTok profile page to sort loaded videos by descending number of views. Click again to restore the default sort.
  6. // @match https://www.tiktok.com/*
  7. // @icon https://www.google.com/s2/favicons?sz=64&domain=tiktok.com
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11. const multiplierMap = {
  12. k: 10 ** 3,
  13. m: 10 ** 6,
  14. b: 10 ** 9,
  15. }
  16. const sort = () => {
  17. const box = document.querySelector('[data-e2e="user-post-item-list"]');
  18.  
  19. const items = Array.from(box.children).map(v => {
  20. const viewsString = v.querySelector('[data-e2e="video-views"]').textContent.trim();
  21. const number = parseFloat(viewsString)
  22. const multiplier = multiplierMap[viewsString.at(-1).toLowerCase()] || 1;
  23. const views = number * multiplier;
  24. return [v, views]
  25. }).sort((a, b) => b[1] - a[1]);
  26. const sorted = items.every(v => v[0].style.order);
  27. items.forEach((v, i) => {
  28. v[0].style.order = sorted
  29. ? ''
  30. : String(i + 1)
  31. });
  32. }
  33. const sleep = time => new Promise(rs => setTimeout(rs, time))
  34.  
  35. const main = async () => {
  36. while(true) {
  37. await sleep(500)
  38. const videoTab = document.querySelector('[data-e2e="videos-tab"]')
  39.  
  40. if (!videoTab || videoTab.sortByViewsListenerAdded) continue;
  41.  
  42. videoTab.addEventListener('click', sort);
  43. videoTab.title += "Sort by number of views / Restore default sort";
  44. videoTab.sortByViewsListenerAdded = true;
  45. }
  46. }
  47. main()

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址