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.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name               Sort TikTok profile videos by number of views
// @namespace          http://tampermonkey.net/
// @version            0.1.1
// @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.
// @match              https://www.tiktok.com/*
// @icon               https://www.google.com/s2/favicons?sz=64&domain=tiktok.com
// @grant              none
// @license            MIT
// ==/UserScript==
 
const multiplierMap = {
  k: 10 ** 3,
  m: 10 ** 6,
  b: 10 ** 9,
}
 
const sort = () => {
  const box = document.querySelector('[data-e2e="user-post-item-list"]');

  const items = Array.from(box.children).map(v => {
      const viewsString = v.querySelector('[data-e2e="video-views"]').textContent.trim();
      const number = parseFloat(viewsString)
      const multiplier = multiplierMap[viewsString.at(-1).toLowerCase()] || 1;
      const views = number * multiplier;
      return [v, views]
    }).sort((a, b) => b[1] - a[1]);
 
    const sorted = items.every(v => v[0].style.order);
 
    items.forEach((v, i) => {
      v[0].style.order = sorted
        ? ''
        : String(i + 1)
    });
}
 
const sleep = time => new Promise(rs => setTimeout(rs, time))

const main = async () => {
  while(true) {
    await sleep(500)
    const videoTab = document.querySelector('[data-e2e="videos-tab"]')

    if (!videoTab || videoTab.sortByViewsListenerAdded) continue;

    videoTab.addEventListener('click', sort);
    videoTab.title += "Sort by number of views / Restore default sort";
    videoTab.sortByViewsListenerAdded = true;
  }
}
main()