Youtube - Always loop playlists

Loop youtube playlists

  1. // ==UserScript==
  2. // @name Youtube - Always loop playlists
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.5
  5. // @description Loop youtube playlists
  6. // @author Jens Nordström
  7. // @match https://www.youtube.com/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  9. // @license MIT
  10. // @grant none
  11. // ==/UserScript==
  12. (function() {
  13. 'use strict';
  14.  
  15. function init() {
  16.  
  17. // Start interval to find elements on navigation
  18. var runInterval = setInterval(setLoop, 500);
  19.  
  20. function setLoop() {
  21. if (!document.querySelector('[aria-label="Loop video"]') || !document.querySelector('[aria-label="Turn off loop"]')) {
  22. document.querySelector('[aria-label="Loop playlist"]').click();
  23. }
  24. }
  25.  
  26. // Stop the interval after init or left mouse click
  27. setTimeout(() => {
  28. clearInterval(runInterval);
  29. }, "5000")
  30. }
  31.  
  32. // Wait for DOM to find elements
  33. function waitForDOM(selector) {
  34. return new Promise(resolve => {
  35. if (document.querySelector(selector)) {
  36. return resolve(document.querySelector(selector));
  37. }
  38.  
  39. const observer = new MutationObserver(mutations => {
  40. if (document.querySelector(selector)) {
  41. resolve(document.querySelector(selector));
  42. observer.disconnect();
  43. }
  44. });
  45.  
  46. observer.observe(document.body, {
  47. childList: true,
  48. subtree: true
  49. });
  50. });
  51. }
  52.  
  53. // Target element mapping
  54. waitForDOM('ytd-playlist-loop-button-renderer').then((target) => {
  55. var isMouseDown = false;
  56.  
  57. init();
  58.  
  59. // Detect left mouse click to run interval on navigation
  60. document.addEventListener('mousedown', function(event) {
  61. if (event.which) isMouseDown = true;
  62. init();
  63. }, true);
  64. });
  65. })();