No Embed Youtube

replace embed iframe, object with anchor link.

As of 2014-08-02. See the latest version.

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 or Violentmonkey 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        No Embed Youtube
// @description	replace embed iframe, object with anchor link.
// @namespace   eight04.blogspot.com
// @include     http*
// @exclude		http://www.youtube.com/*
// @exclude		https://www.youtube.com/*
// @version     1.2.1
// @grant       none
// @run-at		document-start
// ==/UserScript==

/*
embed code from youtube:
<iframe width="420" height="315" src="//www.youtube.com/embed/MOyueLEw2xo" frameborder="0" allowfullscreen></iframe>
<object width="420" height="315"><param name="movie" value="//www.youtube.com/v/MOyueLEw2xo?hl=zh_TW&amp;version=3"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="//www.youtube.com/v/MOyueLEw2xo?hl=zh_TW&amp;version=3" type="application/x-shockwave-flash" width="420" height="315" allowscriptaccess="always" allowfullscreen="true"></embed></object>
*/

"use strict";
/*
if(/youtube\.com\/(v|embed)\//.test(window.location.href)){
	// stop iframe loading
	// window.location.href = "about:blank";
	// is there a better way to pause page?
	// window.stop();
	// alert("pause");
	console.log(unsafeWindow.parent.unEmbed);
}else{
*/

let xpath = "//iframe[contains(@src,'youtube.com/embed/')]|" +
	"//iframe[contains(@src,'youtube.com/v/')]|" +
	"//object[./param[contains(@value,'youtube.com/v/')]]|" +
	"//embed[contains(@src,'youtube.com/v/') and not(ancestor::object)]";

let unEmbed = function(node){
	
	let result = document.evaluate(
		xpath, node, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
	
	let i = 0;
	let element = null;
	
	while(element = result.snapshotItem(i++)){
		// iframe or embed
		let url = element.src;
		
		// object
		if(!url){
			for(let i = 0; i < element.childNodes.length; i++){
				let pa = element.childNodes[i];
				if(pa.getAttribute("name") == "movie"){
					url = pa.getAttribute("value");
					break;
				}
			}
		}
		
		if(!url){
			console.log("can't find url!", element);
			continue;
		}
		
		let id = url.match(/(embed|v)\/(.+?)(\?|&|$)/)[2];
		let a = document.createElement("a");
		let pageUrl = "http://www.youtube.com/watch?v=" + id;
		a.appendChild(document.createTextNode(pageUrl));
		a.setAttribute("href", pageUrl.replace("http:", ""));
		a.setAttribute("target", "_blank");
		a.className = "unembed";
		
		element.parentNode.replaceChild(a, element);
	}
};

document.addEventListener("DOMContentLoaded", function(e){
	
	unEmbed(document.documentElement);
	
	var observer = new MutationObserver(function(mutations){
		mutations.forEach(function(m){
			if(m.type != "childList"){
				return 
			}
			for(var i = 0; i < m.addedNodes.length; i++){
				unEmbed(m.addedNodes[i]);
			}
		});
	});

	observer.observe(document.documentElement, {
		childList: true,
		subtree: true
	});
	
}, false);