Youtube Embed Tweak HTML5

Modifies Youtube HTML5 embedded videos, Forces all videos to a larger size (1024x576) or (720x405), With options for: Https, Hide Annotations and Hide Related.

当前为 2016-02-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Youtube Embed Tweak HTML5
  3. // @namespace embedtweak
  4. // @icon http://i.imgur.com/JyEgdDl.png
  5. // @description Modifies Youtube HTML5 embedded videos, Forces all videos to a larger size (1024x576) or (720x405), With options for: Https, Hide Annotations and Hide Related.
  6. // @version 1.6 (16 August 2015)
  7. // @include http*
  8. // @exclude *liveleak.com*
  9. // @exclude *.youtube.com/*
  10. // @grant none
  11. // ==/UserScript==
  12. //
  13. // Set variables below
  14. //
  15. // Set Video Size, with a 16:9 preset, Large (1024x576) medium (720x405) or Set size in percentage, for a video size as a percantage of 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. // Show youtube logo, yes or no
  22. var ytlogo = 'no';
  23. // Show annoations yes or no.
  24. var annotation = 'no';
  25. // Show Related videos at end of playback, yes or no.
  26. var related = 'no';
  27. // Force https option, yes or no
  28. var ssl = 'yes';
  29. //
  30. //
  31. ////////////////////////////////////////////////
  32. // No need to modify anything past this point //
  33. ////////////////////////////////////////////////
  34.  
  35.  
  36. var ob = new MutationObserver(function(mutations){
  37. for (var m, i=0; i<mutations.length && (m=mutations[i]); i++)
  38. for (var nodes=m.addedNodes, n, j=0; j<nodes.length && (n=nodes[j]); j++) // code thanks to wOxxOm
  39. if (n.nodeType == Node.ELEMENT_NODE)
  40. embedTweak(n);
  41. });
  42. ob.observe(document, {subtree:true, childList:true});
  43.  
  44. if (document.body)
  45. embedTweak(document.body);
  46.  
  47. function embedTweak(node) {
  48.  
  49. var i,j,k,index;
  50. var video_id,video_url,video_link;
  51. var risky_elements,risky_attributes,risky_node;
  52. var risky_tags = [
  53. 'object',
  54. 'embed',
  55. 'iframe'
  56. ];
  57. var bad_elements = [
  58. ];
  59. var bad_ids = [
  60. ];
  61. for (i = 0; i < risky_tags.length; i++) {
  62. risky_elements = node.getElementsByTagName(risky_tags[i]);
  63. for (j = 0; j < risky_elements.length; j++) {
  64. index = 0;
  65. risky_attributes = risky_elements[j].attributes;
  66. for (k = 0; k < risky_attributes.length; k++) {
  67. risky_node = risky_attributes[k].nodeValue;
  68. if (risky_node.indexOf('youtube.com') >= 0) {
  69. risky_elements[j].style.display = 'none';
  70. if (risky_node.indexOf('/v/') >= 0) {
  71. index = risky_node.indexOf('/v/') + 3;
  72. } else if (risky_node.indexOf('?v=') >= 0) {
  73. index = risky_node.indexOf('?v=') + 3;
  74. } else if (risky_node.indexOf('/embed/') >= 0) {
  75. index = risky_node.indexOf('/embed/') + 7;
  76. }
  77. if (index > 0) {
  78. video_id = risky_node.substring(index, index + 11);
  79. bad_elements.push(risky_elements[j]);
  80. bad_ids.push(video_id);
  81. }
  82. break;
  83. }
  84. }
  85. }
  86. }
  87. for (i = 0; i < bad_ids.length; i++) {
  88. video_id = bad_ids[i];
  89. if (nochangeurl == 'yes') {
  90. video_url = 'http://www.youtube.com/embed/' + video_id;
  91. }
  92. else {
  93. if (ssl == 'yes') {
  94. protid = 'https://'
  95. }
  96. if (ssl == 'no') {
  97. protid = 'http://'
  98. }
  99. if (annotation == 'no') {
  100. ivid = 'iv_load_policy=3&';
  101. }
  102. if (annotation == 'yes') {
  103. ivid = 'iv_load_policy=1&';
  104. }
  105. if (related == 'no') {
  106. relatedid = 'rel=0&';
  107. }
  108. if (related == 'yes') {
  109. relatedid = 'rel=1&';
  110. }
  111. if (ytlogo == 'no') {
  112. ytlogoid = 'modestbranding=1&';
  113. }
  114. if (ytlogo == 'yes') {
  115. ytlogoid = 'modestbranding=0&';
  116. }
  117. video_url = protid + 'www.youtube.com/embed/' + video_id + '?' + ivid + relatedid + ytlogoid;
  118. }
  119. video_link = document.createElement('iframe');
  120. video_link.setAttribute('src', video_url);
  121. if (vsm = videosize.match(/^\s*(\d+)%\s*$/)) { // percentage size code thanks to wOxxOm
  122. var parent = bad_elements[i].parentNode;
  123. while (parent) {
  124. available_width = parent.offsetWidth || parent.clientWidth;
  125. if (available_width > 0) {
  126. video_link.width = available_width * vsm[1] / 100;
  127. video_link.height = Math.floor(video_link.width / 16 * 9);
  128. break;
  129. }
  130. parent = parent.parentNode;
  131. }
  132. }
  133. if (videosize == 'large') {
  134. video_link.width = '1024';
  135. video_link.height = '576';
  136. }
  137. if (videosize == 'medium') {
  138. video_link.width = '720';
  139. video_link.height = '405';
  140. }
  141. video_link.setAttribute('frameborder', '0');
  142. video_link.setAttribute('allowfullscreen', '1');
  143. bad_elements[i].parentNode.replaceChild(video_link, bad_elements[i]);
  144. }
  145. }