SHARE
TWEET

Youtube scroll to last watched video

a guest May 7th, 2017 25 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Youtube Scroll 'scrollToNew'
  3. // @namespace    http://your.homepage/
  4. // @version      0.1
  5. // @description  enter something useful
  6. // @author       You
  7. // @match        https://www.youtube.com/feed/subscriptions*
  8. // @grant        none
  9. // @require      https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js
  10. // @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js
  11. // ==/UserScript==
  12.  
  13. var navBar = document.getElementsByClassName("appbar-nav-menu")[0];
  14. var sample = document.getElementsByClassName("appbar-nav-menu")[0].firstChild.firstElementChild.classList;
  15.  
  16. var listclass = "";
  17. for(var i = 0 ; i < sample.length ; i ++){
  18.     listclass += sample[i] + " ";
  19. }
  20.  
  21. var newdiv = document.createElement('li');
  22. newdiv.innerHTML = '<input type="button" class="' + listclass+ '" onclick="scrollToNew();" value="Scroll To New" />';
  23. navBar.appendChild(newdiv);
  24.  
  25. scrollToNew = function(){
  26.     var elems = $(".watched, .resume-playback-progress-bar");
  27.     elems = _.flatMap(elems, function(e) {
  28.        return $(e).closest('.item-section').get(0);
  29.     });
  30.     elems = _.sortBy(elems, function(e) {
  31.        return e.offsetTop;
  32.     });
  33.    
  34.     console.log("Got " + elems.length + " elements.");
  35.  
  36.     if(elems.length === 0){
  37.         $(".browse-items-load-more-button")[0].click();
  38.         setTimeout(function(){scrollToNew();}, 100);
  39.     } else {
  40.         var index = -1;
  41.         var scroll = -1;
  42.         while(scroll <= body.scrollTop){
  43.             index++;
  44.             if(index >= elems.length){
  45.                 $(".browse-items-load-more-button")[0].click();
  46.                 setTimeout(function(){scrollToNew();},100);
  47.                 break;
  48.             }
  49.             var elem = elems[index];
  50.             scroll = elem.offsetTop;
  51.         }
  52.  
  53.         setTimeout(function(){
  54.             console.log("Scroll to " + scroll + " from " + body.scrollTop) ;
  55.             body.scrollTop = scroll;
  56.             if(body.scrollTop != scroll){
  57.                 document.getElementsByClassName("browse-items-load-more-button")[0].click();
  58.                 setTimeout(function(){scrollToNew();},100);
  59.             }
  60.         },10);
  61.     }
  62. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top