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.

当前为 2015-08-16 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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.6 (16 August 2015)
// @include	http*
// @exclude	*liveleak.com*
// @exclude	*.youtube.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 = 'no';
// 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';
//
//
////////////////////////////////////////////////
// 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&';
    }
    video_url = protid + 'www.youtube.com/embed/' + video_id + '?' + ivid + relatedid + ytlogoid;
  }
  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]);
}
}