YouTube Popout Button

Provides a button to pop out the YouTube video in a separate window.

当前为 2020-04-24 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube Popout Button
  3. // @description Provides a button to pop out the YouTube video in a separate window.
  4. // @version 1.1.1
  5. // @author joeytwiddle
  6. // @contributor Alek_T, tehnicallyrite
  7. // @license ISC
  8. // @include http://*.youtube.com/watch*
  9. // @include http://youtube.com/watch*
  10. // @include https://*.youtube.com/watch*
  11. // @include https://youtube.com/watch*
  12. // @grant none
  13. // @namespace https://greasyfork.org/users/8615
  14. // ==/UserScript==
  15.  
  16. // This is a combination of two scripts I found:
  17. // - http://userscripts-mirror.org/scripts/show/75815#YouTube:_Pop-out_Video
  18. // - http://userscripts-mirror.org/scripts/show/69687#YouTube_Popout
  19. // For a while I think I hosted it at:
  20. // - http://userscripts-mirror.org/scripts/source/150631.user.js
  21.  
  22. // Need to delay, or the target div won't be rendered yet
  23. setTimeout(function() {
  24. // Create Button
  25. var divWatchHeadline = document.evaluate("//div[@id='watch-actions']", document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue;
  26. divWatchHeadline = divWatchHeadline || document.getElementById("watch7-secondary-actions");
  27. divWatchHeadline = divWatchHeadline || document.getElementById("watch8-secondary-actions");
  28. divWatchHeadline = divWatchHeadline || document.querySelector("#menu .ytd-video-primary-info-renderer");
  29. //divWatchHeadline = divWatchHeadline || document.querySelector("#top-level-buttons");
  30.  
  31. var buttonPopout = document.createElement("button");
  32. buttonPopout.title = "Pop-out Video";
  33. //buttonPopout.setAttribute("class", "yt-uix-button yt-uix-button-default yt-uix-tooltip");
  34. buttonPopout.style.background = 'transparent';
  35. buttonPopout.style.border = 'none';
  36. buttonPopout.style.cursor = 'pointer';
  37. buttonPopout.setAttribute("data-tooltip-title", "Pop-out Video");
  38. var popoutImage = document.createElement("img");
  39. var offButton = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAMAAAAolt3jAAABKVBMVEVmZmZaWlp1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0O/u9v+QveoANv8AZf8AaP+lyer1+f9IhP8AR//C3P9KkP8ANv6gyP+20/+rze8FZf8RU/9PiP8ASf84fP8AVP8sf+UAWf9Qkv9Wlv8ALP9/sv+oyv+Muv+bw/+51eosdv9ZmP9bnf+AsuU5iNhkouAAV8dbnd4oguOqzOu92Os/i9mDtd2AsP8AVv9Rkv/U6f+WuP+61v9CiQWZAAAANnRSTlPyAAAAAPLyAMXyAAAANvIA8nzy7fJfAIa7xeXFxeoAAPL08u6QxABqZMkAAMoAygDaAOXy8sAxRV+aAAAAw0lEQVR4Xh3OxXLDUBBE0XlCW7LMzA4z50kyMzOGmZm/P1O+u1PViwZZ5AhxmaMzmNUPjLTOEduwl9R1/e4YWHRYcXxSrG8HlmVMbovn7BJ5ZQSvz2AIBEMncWTeCIWHx4+Xyjfil/45QRjQn9EYdaqdX8SAv45rN6jb2bmvag34eqPZatNOdz6iLC4BX6Y0MTk4PFpeWCEE+BQu05lsbs20yhEQihQr0fsNRhI34elZVdXXN/V9C/9tw84uTNvbx3/yP8AgJKm6HHSaAAAAAElFTkSuQmCC";
  41. popoutImage.src = offButton;
  42. popoutImage.setAttribute("alt", "External link icon");
  43. popoutImage.setAttribute("style", "padding:0px 0px 2px 1px;");
  44. buttonPopout.appendChild(popoutImage);
  45. // The other buttons don't change these days, so we won't either
  46. //buttonPopout.addEventListener("mouseover", function() { popoutImage.src = overButton; }, false);
  47. //buttonPopout.addEventListener("mouseout", function() { popoutImage.src = offButton; }, false);
  48.  
  49. //divWatchHeadline.appendChild(document.createTextNode("\n"));
  50. //divWatchHeadline.appendChild(buttonPopout);
  51. divWatchHeadline.insertBefore(buttonPopout, divWatchHeadline.lastElementChild);
  52. //divWatchHeadline.appendChild(document.createTextNode("\n"));
  53.  
  54. buttonPopout.addEventListener("click", popOutVideo, false);
  55.  
  56. function popOutVideo() {
  57. // Grabbing Video Id
  58. function gup(name) {
  59. name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  60. var regexS = "[\\?&]" + name + "=([^&#]*)";
  61. var regex = new RegExp(regexS);
  62. var results = regex.exec(window.location.href);
  63. return results && results[1];
  64. }
  65.  
  66. var ytvidid = gup('v');
  67.  
  68. if (ytvidid) {
  69. //var link = "http://www.youtube.com/watch_popup?v=";
  70. //var flink = link+ytvidid;
  71. // The above URL gets redirected to https://www.youtube.com/embed/bNcWVUfwmS4&autoplay=1#at=6
  72. // And the redirect causes autoplay to not work. So let's go directly to the target URL.
  73. var flink = "https://www.youtube.com/embed/" + ytvidid + "?autoplay=1";
  74. var lcheck = location.href;
  75. // I think this used to prevent infinite loops when the script was auto-forwarding
  76. if (lcheck !== flink) {
  77. try {
  78. var player = window.document.getElementById('movie_player');
  79. if (player) {
  80. // If we are in Greasemonkey's sandbox, we need to get out!
  81. if (player.wrappedJSObject) {
  82. player = player.wrappedJSObject;
  83. }
  84. player.pauseVideo();
  85. var time = player.getCurrentTime();
  86. flink += "#at=" + (time | 0);
  87. }
  88. } catch (e) {
  89. console.error("" + e);
  90. }
  91.  
  92. // window.location = flink;
  93. // Change "YoutubePopout" to "_blank" if you want new popouts to appear in a separate window from the existing popout.
  94. window.open(flink, "YoutubePopout", "menubar=no,location=no,resizable=yes,status=no,toolbar=no,personalbar=no");
  95. }
  96. }
  97. }
  98. }, 4000);