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.

Ekde 2016/10/29. Vidu La ĝisdata versio.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Youtube Embed Tweak HTML5
// @namespace	embedtweak
// @icon   http://i.imgur.com/JyEgdDl.png
// @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. 
// @version	1.66 (29 October 2016)
// @include	http*
// @exclude	*liveleak.com*
// @exclude	*.youtube.com/*
// @exclude	*.redditmedia.com/*
// @grant       none
// ==/UserScript==         
//
// Set variables below
//
// 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. 
var videosize = 'medium';
// or
//var videosize = '50%';
// nochangeurl must be set to no for player settings to work. yes = default url and no = modified url, size is always modified.
var nochangeurl = 'no';
// Show youtube logo, yes or no
var ytlogo = 'yes';
// Show annoations yes or no. 
var annotation = 'no';
// Show Related videos at end of playback, yes or no.
var related = 'no';
// Force https option, yes or no
var ssl = 'yes';
// Autoplay, yes or no
var autoplay = 'no';
// The player progress bar colour setting, red or white, white disables the youtube logo setting above
var color = 'red';


////////////////////////////////////////////////
// No need to modify anything past this point // 
////////////////////////////////////////////////


var ob = new MutationObserver(function(mutations){
  for (var m, i=0; i<mutations.length && (m=mutations[i]); i++)
    for (var nodes=m.addedNodes, n, j=0; j<nodes.length && (n=nodes[j]); j++)  // code thanks to wOxxOm
      if (n.nodeType == Node.ELEMENT_NODE)
        embedTweak(n);
});
ob.observe(document, {subtree:true, childList:true});

if (document.body)
  embedTweak(document.body);

function embedTweak(node) {

var i,j,k,index;
var video_id,video_url,video_link;
var risky_elements,risky_attributes,risky_node;
var risky_tags = [
  'object',
  'embed',
  'iframe'
];
var bad_elements = [
];
var bad_ids = [
];
for (i = 0; i < risky_tags.length; i++) {
  risky_elements = node.getElementsByTagName(risky_tags[i]);
  for (j = 0; j < risky_elements.length; j++) {
    index = 0;
    risky_attributes = risky_elements[j].attributes;
    for (k = 0; k < risky_attributes.length; k++) {
      risky_node = risky_attributes[k].nodeValue;
      if (risky_node.indexOf('youtube.com') >= 0) {
        risky_elements[j].style.display = 'none';
        if (risky_node.indexOf('/v/') >= 0) {
          index = risky_node.indexOf('/v/') + 3;
        } else if (risky_node.indexOf('?v=') >= 0) {
          index = risky_node.indexOf('?v=') + 3;
        } else if (risky_node.indexOf('/embed/') >= 0) {
          index = risky_node.indexOf('/embed/') + 7;
        }
        if (index > 0) {
          video_id = risky_node.substring(index, index + 11);
          bad_elements.push(risky_elements[j]);
          bad_ids.push(video_id);
        }
        break;
      }
    }
  }
}
for (i = 0; i < bad_ids.length; i++) {
  video_id = bad_ids[i];
  if (nochangeurl == 'yes') {
    video_url = 'http://www.youtube.com/embed/' + video_id;
  } 
  else {
    if (ssl == 'yes') {
      protid = 'https://'
    }
    if (ssl == 'no') {
      protid = 'http://'
    }
    if (annotation == 'no') {
      ivid = 'iv_load_policy=3&';
    }
    if (annotation == 'yes') {
      ivid = 'iv_load_policy=1&';
    }
    if (related == 'no') {
      relatedid = 'rel=0&';
    }
    if (related == 'yes') {
      relatedid = 'rel=1&';
    }
    if (ytlogo == 'no') {
        ytlogoid = 'modestbranding=1&';
    }
    if (ytlogo == 'yes') {
        ytlogoid = 'modestbranding=0&';
    }
    if (autoplay == 'no') {
        autoplayid = 'autoplay=0&';
    }
    if (autoplay== 'yes') {
        autoplayid = 'autoplay=1&';
    }  
    if (color == 'red') {
        colorid = 'color=red&';
    }
    if (color == 'white') {
        colorid = 'color=white&';
    } 
    video_url = protid + 'www.youtube.com/embed/' + video_id + '?' + ivid + relatedid + ytlogoid + autoplayid + colorid;
  }
  video_link = document.createElement('iframe');
  video_link.setAttribute('src', video_url);
  if (vsm = videosize.match(/^\s*(\d+)%\s*$/)) { // percentage size code thanks to wOxxOm
    var parent = bad_elements[i].parentNode;
    while (parent) {
      available_width = parent.offsetWidth || parent.clientWidth;
      if (available_width > 0) {
        video_link.width = available_width * vsm[1] / 100;
        video_link.height = Math.floor(video_link.width / 16 * 9);
        break;
      }
      parent = parent.parentNode;
    }
  }
  if (videosize == 'large') {
    video_link.width = '1024';
    video_link.height = '576';
  }
  if (videosize == 'medium') {
    video_link.width = '720';
    video_link.height = '405';
  }
  video_link.setAttribute('frameborder', '0');
  video_link.setAttribute('allowfullscreen', '1');
  bad_elements[i].parentNode.replaceChild(video_link, bad_elements[i]);
}
}