Youtube Embed Tweak HTML5 (16 February 2018)

Forces all emdedded Youtube videos to the new player. With options for: Video size, https, hide annotations and hide related and hide controls

  1. // ==UserScript==
  2. // @name Youtube Embed Tweak HTML5 (16 February 2018)
  3. // @namespace embedtweak
  4. // @icon http://i.imgur.com/JyEgdDl.png
  5. // @description Forces all emdedded Youtube videos to the new player. With options for: Video size, https, hide annotations and hide related and hide controls
  6. // @version 1.8
  7. // @include http*
  8. // @exclude *liveleak.com*
  9. // @exclude *youtube.com/*
  10. // @exclude *redditmedia.com/*
  11. // @exclude *twitter.com/*
  12. // @grant none
  13. //
  14. // Thanks to wOxxOm for contributing observer code.
  15. //
  16. // ==/UserScript==
  17. //
  18. // Set variables below
  19. //
  20. // 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.
  21. var videosize = 'medium';
  22. // nochangeurl must be set to no for player settings to work. yes = default url and no = modified url, size is always modified.
  23. var nochangeurl = 'no';
  24. // Show youtube logo, yes or no
  25. var ytlogo = 'yes';
  26. // Show annoations yes or no.
  27. var annotation = 'no';
  28. // Show Related videos at end of playback, yes or no.
  29. var related = 'no';
  30. // Force https option, yes or no
  31. var ssl = 'yes';
  32. // Autoplay, yes or no
  33. var autoplay = 'no';
  34. // The player progress bar colour setting, red or white, white disables the youtube logo setting above
  35. var color = 'white';
  36. // autohide player controls, yes shows, no hides.
  37. var controls = 'no';
  38.  
  39. ////////////////////////////////////////////////
  40. // No need to modify anything past this point //
  41. ////////////////////////////////////////////////
  42.  
  43.  
  44. var ob = new MutationObserver(function(mutations){
  45. for (var m, i=0; i<mutations.length && (m=mutations[i]); i++)
  46. for (var nodes=m.addedNodes, n, j=0; j<nodes.length && (n=nodes[j]); j++)
  47. if (n.nodeType == Node.ELEMENT_NODE)
  48. embedTweak(n);
  49. });
  50. ob.observe(document, {subtree:true, childList:true});
  51.  
  52. if (document.body)
  53. embedTweak(document.body);
  54.  
  55. function embedTweak(node) {
  56.  
  57. var i,j,k,index;
  58. var video_id,video_url,video_link;
  59. var risky_elements,risky_attributes,risky_node;
  60. var risky_tags = [
  61. 'object',
  62. 'embed',
  63. 'iframe'
  64. ];
  65. var bad_elements = [
  66. ];
  67. var bad_ids = [
  68. ];
  69. for (i = 0; i < risky_tags.length; i++) {
  70. risky_elements = node.getElementsByTagName(risky_tags[i]);
  71. for (j = 0; j < risky_elements.length; j++) {
  72. index = 0;
  73. risky_attributes = risky_elements[j].attributes;
  74. for (k = 0; k < risky_attributes.length; k++) {
  75. risky_node = risky_attributes[k].nodeValue;
  76. if (risky_node.indexOf('youtube.com') >= 0) {
  77. risky_elements[j].style.display = 'none';
  78. if (risky_node.indexOf('/v/') >= 0) {
  79. index = risky_node.indexOf('/v/') + 3;
  80. } else if (risky_node.indexOf('?v=') >= 0) {
  81. index = risky_node.indexOf('?v=') + 3;
  82. } else if (risky_node.indexOf('/embed/') >= 0) {
  83. index = risky_node.indexOf('/embed/') + 7;
  84. }
  85. if (index > 0) {
  86. video_id = risky_node.substring(index, index + 11);
  87. bad_elements.push(risky_elements[j]);
  88. bad_ids.push(video_id);
  89. }
  90. break;
  91. }
  92. }
  93. }
  94. }
  95. for (i = 0; i < bad_ids.length; i++) {
  96. video_id = bad_ids[i];
  97. if (nochangeurl == 'yes') {
  98. video_url = 'http://www.youtube.com/embed/' + video_id;
  99. }
  100. else {
  101. if (ssl == 'yes') {
  102. protid = 'https://';
  103. }
  104. if (ssl == 'no') {
  105. protid = 'http://';
  106. }
  107. if (annotation == 'no') {
  108. ivid = 'iv_load_policy=3&';
  109. }
  110. if (annotation == 'yes') {
  111. ivid = 'iv_load_policy=1&';
  112. }
  113. if (related == 'no') {
  114. relatedid = 'rel=0&';
  115. }
  116. if (related == 'yes') {
  117. relatedid = 'rel=1&';
  118. }
  119. if (ytlogo == 'no') {
  120. ytlogoid = 'modestbranding=1&';
  121. }
  122. if (ytlogo == 'yes') {
  123. ytlogoid = 'modestbranding=0&';
  124. }
  125. if (autoplay == 'no') {
  126. autoplayid = 'autoplay=0&';
  127. }
  128. if (autoplay== 'yes') {
  129. autoplayid = 'autoplay=1&';
  130. }
  131. if (color == 'red') {
  132. colorid = 'color=red&';
  133. }
  134. if (color == 'white') {
  135. colorid = 'color=white&';
  136. }
  137. if (controls == 'no') {
  138. controlsid = 'controls=2&';
  139. }
  140. if (controls == 'yes') {
  141. controlsid = 'controls=1&';
  142. }
  143. video_url = protid + 'www.youtube.com/embed/' + video_id + '?' + ivid + relatedid + ytlogoid + autoplayid + colorid + controlsid;
  144. }
  145. video_link = document.createElement('iframe');
  146. video_link.setAttribute('src', video_url);
  147. if (videosize == 'large') {
  148. video_link.width = '1024';
  149. video_link.height = '576';
  150. }
  151. if (videosize == 'medium') {
  152. video_link.width = '720';
  153. video_link.height = '405';
  154. }
  155. video_link.setAttribute('frameborder', '0');
  156. video_link.setAttribute('allowfullscreen', '1');
  157. bad_elements[i].parentNode.replaceChild(video_link, bad_elements[i]);
  158. }
  159. }