Youtube to Invidio.us Embed

Forces all embedded Youtube videos to Invidio.us for watch youtube videos with more privacy

目前為 2020-01-12 提交的版本,檢視 最新版本

// ==UserScript==
// @name              Youtube to Invidio.us Embed
// @description       Forces all embedded Youtube videos to Invidio.us for watch youtube videos with more privacy
// @version           1.1
// @include           http*
// @exclude           *youtube.com/*
// @grant             none
// @namespace https://greasyfork.org/users/433508
// ==/UserScript==

var invidioUrl = 'invidio.us'; // Domain of Invidio instance, change by your prefer. 
// (Default: invidio.us)

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];
		video_url  = 'https://www.' + invidioUrl + '/embed/' + video_id ;
		
		video_link = document.createElement('iframe');		
		video_link.setAttribute('src', video_url);
		video_link.setAttribute('style', 'min-height:422px;min-width:680px;');
		video_link.setAttribute('frameborder', '0');
		video_link.setAttribute('allowfullscreen', '1');
      
		bad_elements[i].parentNode.replaceChild(video_link, bad_elements[i]);
    }
}

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

/*
 * Adapted fork of "Youtube Embed Tweak HTML5":
 * https://greasyfork.org/en/scripts/3435-youtube-embed-tweak-html5-16-february-2018/code 
 */