Open Youtube video links in new tab

Opens Youtube video links in new tab

  1. // ==UserScript==
  2. // @name Open Youtube video links in new tab
  3. // @description Opens Youtube video links in new tab
  4. // @include https://www.youtube.com/*
  5. // @exclude https://www.youtube.com/watch*
  6. // @namespace https://greasyfork.org/users/14346
  7. // @author wOxxOm
  8. // @version 2.0.3
  9. // @license MIT License
  10. // @grant GM_openInTab
  11. // @run-at document-start
  12. // ==/UserScript==
  13.  
  14. var suppressing;
  15. window.addEventListener('mouseup', function(e) {
  16. if (e.button > 1 || e.altKey)
  17. return;
  18. var link = e.target.closest('[href^="/watch"]');
  19. if (!link ||
  20. (link.getAttribute('href') || '').match(/^(javascript|#|$)/) ||
  21. link.href.replace(/#.*/, '') == location.href.replace(/#.*/, '')
  22. )
  23. return;
  24.  
  25. GM_openInTab(link.href, e.button || e.ctrlKey);
  26. suppressing = true;
  27. prevent(e);
  28. }, true);
  29.  
  30. window.addEventListener('click', prevent, true);
  31. window.addEventListener('auxclick', prevent, true);
  32.  
  33. function prevent(e) {
  34. if (!suppressing)
  35. return;
  36. e.preventDefault();
  37. e.stopPropagation();
  38. e.stopImmediatePropagation();
  39. setTimeout(function() {
  40. suppressing = false;
  41. }, 100);
  42. }