Youtube Embed Tweak

Modifies Youtube embed videos, Forces all videos to a larger size (640x385) or (1024x576), With options for: Video Size, HD Playback, Https, Autohide, Theme, Hide Annotations and Hide Related.

当前为 2014-12-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Youtube Embed Tweak
  3. // @namespace embedtweak
  4. // @grant none
  5. // @icon http://i.imgur.com/JyEgdDl.png
  6. // @description Modifies Youtube embed videos, Forces all videos to a larger size (640x385) or (1024x576), With options for: Video Size, HD Playback, Https, Autohide, Theme, Hide Annotations and Hide Related.
  7. // @version 1.51 (02 November 2014)
  8. // @include http*
  9. // @exclude *liveleak.com*
  10. // @exclude *.youtube.com/*
  11. // ==/UserScript==
  12. //
  13. // Set variables below
  14. //
  15. // Set Video Size, large or medium. Large (1024x576) medium (640x385) or Set size in percentage, for a video size based off the container size.
  16. var videosize = 'medium';
  17. // or
  18. //var videosize = '50%';
  19. // nochangeurl must be set to no for player settings to work. yes = default url and no = modified url, size is always modified.
  20. var nochangeurl = 'no';
  21. // hd = 0 or 1 or 2. 0 = default, 1 = 720p , 2 = 1080p. if available.
  22. var hd = 1;
  23. // theme, options: light or dark
  24. var ytheme = 'light';
  25. // color, options: red or white
  26. var ycolor = 'white';
  27. // Set annotation = 0 or 1, 0 disables annotations
  28. var annotation = 0;
  29. // Show Related videos at end of playback, option: 1 or 0,
  30. var related = 0;
  31. // Force https option, 1 or 0, 1 enables https
  32. var ssl = 1;
  33. // Set autohide = 0 or 1, 1 enables auto hide of player controls. (0 is default behaviour)
  34. var autohide = 1;
  35. //
  36. //
  37. //
  38. //
  39. ////////////////////////////////////////////////
  40. // No need to modify anything past this point //
  41. ////////////////////////////////////////////////
  42.  
  43. var ob = new MutationObserver(function(mutations){
  44. for (var m, i=0; i<mutations.length && (m=mutations[i]); i++)
  45. for (var nodes=m.addedNodes, n, j=0; j<nodes.length && (n=nodes[j]); j++) // code thanks to wOxxOm
  46. if (n.nodeType == Node.ELEMENT_NODE)
  47. embedTweak(n);
  48. });
  49. ob.observe(document, {subtree:true, childList:true});
  50.  
  51. if (document.body)
  52. embedTweak(document.body);
  53.  
  54. function embedTweak(node) {
  55.  
  56. var i,j,k,index;
  57. var video_id,video_url,video_link;
  58. var risky_elements,risky_attributes,risky_node;
  59. var risky_tags = [
  60. 'object',
  61. 'embed',
  62. 'iframe'
  63. ];
  64. var bad_elements = [
  65. ];
  66. var bad_ids = [
  67. ];
  68. for (i = 0; i < risky_tags.length; i++) {
  69. risky_elements = node.getElementsByTagName(risky_tags[i]);
  70. for (j = 0; j < risky_elements.length; j++) {
  71. index = 0;
  72. risky_attributes = risky_elements[j].attributes;
  73. for (k = 0; k < risky_attributes.length; k++) {
  74. risky_node = risky_attributes[k].nodeValue;
  75. if ((risky_node.indexOf('youtube.com') >= 0) || (risky_node.indexOf('youtube-nocookie.com') >= 0)) {
  76. risky_elements[j].style.display = 'none';
  77. if (risky_node.indexOf('/v/') >= 0) {
  78. index = risky_node.indexOf('/v/') + 3;
  79. } else if (risky_node.indexOf('?v=') >= 0) {
  80. index = risky_node.indexOf('?v=') + 3;
  81. } else if (risky_node.indexOf('/embed/') >= 0) {
  82. index = risky_node.indexOf('/embed/') + 7;
  83. }
  84. if (index > 0) {
  85. video_id = risky_node.substring(index, index + 11);
  86. bad_elements.push(risky_elements[j]);
  87. bad_ids.push(video_id);
  88. }
  89. break;
  90. }
  91. }
  92. }
  93. }
  94. for (i = 0; i < bad_ids.length; i++) {
  95. video_id = bad_ids[i];
  96. if (nochangeurl == 'yes') {
  97. video_url = 'http://www.youtube.com/embed/' + video_id;
  98. }
  99. else {
  100. if (ssl == 1) {
  101. protid = 'https://'
  102. }
  103. if (ssl == 0) {
  104. protid = 'http://'
  105. }
  106. if (ytheme == 'light') {
  107. ythemeid = 'theme=light&';
  108. }
  109. if (ytheme == 'dark') {
  110. ythemeid = 'theme=dark&';
  111. }
  112. if (annotation == 0) {
  113. ivid = 'iv_load_policy=3&';
  114. }
  115. if (annotation == 1) {
  116. ivid = 'iv_load_policy=1&';
  117. }
  118. if (related == 0) {
  119. relatedid = 'rel=0&';
  120. }
  121. if (related == 1) {
  122. relatedid = 'rel=1&';
  123. }
  124. if (autohide == 1) {
  125. ahid = 'autohide=1&';
  126. }
  127. if (autohide == 0) {
  128. ahid = 'autohide=0&';
  129. }
  130. if (ycolor == 'red') {
  131. ycolorid = 'color=red&';
  132. }
  133. if (ycolor == 'white') {
  134. ycolorid = 'color=white&';
  135. }
  136. if (hd == 0) {
  137. hdid = 'vq=default&';
  138. }
  139. if (hd == 1) {
  140. hdid = 'vq=hd720&';
  141. }
  142. if (hd == 2) {
  143. hdid = 'vq=hd1080&';
  144. }
  145. video_url = protid + 'www.youtube.com/embed/' + video_id + '?' + ythemeid + ycolorid + ivid + relatedid + ahid + hdid;
  146. }
  147. video_link = document.createElement('iframe');
  148. video_link.setAttribute('src', video_url);
  149. if (vsm = videosize.match(/^\s*(\d+)%\s*$/)) { // percentage size code thanks to wOxxOm
  150. var parent = bad_elements[i].parentNode;
  151. while (parent) {
  152. available_width = parent.offsetWidth || parent.clientWidth;
  153. if (available_width > 0) {
  154. video_link.width = available_width * vsm[1] / 100;
  155. video_link.height = Math.floor(video_link.width / 16 * 9);
  156. break;
  157. }
  158. parent = parent.parentNode;
  159. }
  160. }
  161. if (videosize == 'large') {
  162. video_link.width = '1024';
  163. video_link.height = '576';
  164. }
  165. if (videosize == 'medium') {
  166. video_link.width = '640';
  167. video_link.height = '385';
  168. }
  169. video_link.setAttribute('frameborder', '0');
  170. video_link.setAttribute('allowfullscreen', '1');
  171. bad_elements[i].parentNode.replaceChild(video_link, bad_elements[i]);
  172. }
  173. }