Youtube - No scroll to top on timestamps

Prevent Youtube scrolling to top when clicking timestamps in description or comments

  1. // ==UserScript==
  2. // @name Youtube - No scroll to top on timestamps
  3. // @namespace q1k
  4. // @version 1.3
  5. // @description Prevent Youtube scrolling to top when clicking timestamps in description or comments
  6. // @author q1k
  7. // @match *://www.youtube.com/*
  8. // @grant none
  9. // @run-at document-end
  10. // ==/UserScript==
  11.  
  12. var seconds=0;
  13.  
  14. function timestampToSeconds(t){
  15. let parts = t.split(':').reverse();
  16. if (parts.length<2){ return false; }
  17. seconds = 0;
  18. for(let i=0; i<parts.length; i++){
  19. switch (i) {
  20. case 0: seconds += (+parts[i]); break;
  21. case 1: seconds += (+parts[i])*60; break;
  22. case 2: seconds += (+parts[i])*60*60; break;
  23. case 3: seconds += (+parts[i])*60*60*24; break;
  24. }
  25. }
  26. return Number.isInteger(seconds);
  27. }
  28.  
  29. document.addEventListener("click", function(e){
  30. if(e.target.tagName=="A"){
  31. if(timestampToSeconds(e.target.innerText)){
  32. e.preventDefault();
  33. e.stopPropagation();
  34. e.stopImmediatePropagation();
  35. movie_player.seekTo(seconds);
  36. return;
  37. }
  38. } else if(e.target.closest("a#endpoint")){/*chapters*/
  39. if(timestampToSeconds(e.target.closest("a#endpoint").querySelector("#details #time").innerText)){
  40. e.preventDefault();
  41. e.stopPropagation();
  42. e.stopImmediatePropagation();
  43. movie_player.seekTo(seconds);
  44. return;
  45. }
  46. }
  47. }, {capture: true} );
  48.