Skip to content

Instantly share code, notes, and snippets.

@remixer-dec
Last active September 13, 2025 02:46
Show Gist options
  • Select an option

  • Save remixer-dec/c0e4a8ef4cf4b1c600f9ccea4e0f8c80 to your computer and use it in GitHub Desktop.

Select an option

Save remixer-dec/c0e4a8ef4cf4b1c600f9ccea4e0f8c80 to your computer and use it in GitHub Desktop.
[bookmarklet] Sort recent youtube videos by popularity
javascript:(function(){if(window.__ytOBV){window.__ytOBV.tog();return}var s={d:true,c:null,mo:null,bmo:null,tm:0};function p(t){if(!t)return 0;t=t.replace(/\u00A0|\u202F/g,' ').toLowerCase();var m=1;if(/тыс|k\b/.test(t))m=1e3;else if(/млн|m\b/.test(t))m=1e6;else if(/млрд|b\b/.test(t))m=1e9;var n=(t.match(/[\d.,]+/)||['0'])[0];if(n.indexOf(',')>-1&&n.indexOf('.')>-1)n=n.replace(/,/g,'');else if(n.indexOf(',')>-1)n=n.replace(',','.');n=parseFloat(n)||0;return n*m}function find(){return document.querySelector('#contents.ytd-rich-grid-renderer,ytd-rich-grid-renderer #contents,#contents')}function Gtext(el){var spans=el.querySelectorAll('span.inline-metadata-item');for(var i=0;i<spans.length;i++){var tt=spans[i].innerText||'';if(/view|просм|тыс|млн|млрд|k|m|b/i.test(tt))return tt}return spans[0]&&spans[0].innerText||''}function app(){var c=s.c=find();if(!c)return;var items=Array.from(c.querySelectorAll('ytd-rich-item-renderer'));var arr=items.map(function(el){var node=el;while(node.parentElement&&node.parentElement!==c)node=node.parentElement;return{node:node,v:p(Gtext(el))}});arr.sort(function(a,b){return s.d?b.v-a.v:a.v-b.v});var base=-999999;for(var i=0;i<arr.length;i++){try{arr[i].node.style.setProperty('order',String(base+i),'important');arr[i].node.dataset._v=Math.round(arr[i].v)}catch(e){}}}function obs(){var c=find();if(c){s.c=c;if(s.mo)s.mo.disconnect();s.mo=new MutationObserver(function(){clearTimeout(s.tm);s.tm=setTimeout(app,100)});s.mo.observe(c,{childList:true,subtree:true});app()}if(!s.bmo){s.bmo=new MutationObserver(function(){clearTimeout(s.tm);s.tm=setTimeout(function(){var n=find();if(n&&n!==s.c){if(s.mo)s.mo.disconnect();obs()}else app()},150)});s.bmo.observe(document.body,{childList:true,subtree:true})}}obs();window.__ytOBV={tog:function(){s.d=!s.d;app()},destroy:function(){if(s.mo)s.mo.disconnect();if(s.bmo)s.bmo.disconnect();var c=find();if(c)Array.from(c.children).forEach(function(ch){ch.style.removeProperty('order');delete ch.dataset._v});delete window.__ytOBV}}})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment