No Embed Youtube

replace embed iframe, object with anchor link.

目前為 2015-03-19 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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.3.0
// @grant       none
// ==/UserScript==

"use strict";

var xpath = "//iframe[contains(@src,'youtube.com/embed/') and not(ancestor::*[@id='YTLT-player'])]|" +
	"//iframe[contains(@src,'youtube.com/v/') and not(ancestor::*[@id='YTLT-player'])]|" +
	"//object[./param[contains(@value,'youtube.com/v/')] and not(ancestor::*[@id='YTLT-player'])]|" +
	"//embed[contains(@src,'youtube.com/v/') and not(ancestor::object) and not(ancestor::*[@id='YTLT-player'])]";
	
var unEmbed = function(node){
	var result = document.evaluate(
		xpath, node, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
	
	var element = null;
	var i = 0, j;
	
	while(element = result.snapshotItem(i++)){
	
		// iframe or embed
		var url = element.src;
		
		// object
		if(!url){
			for(j = 0; j < element.childNodes.length; j++){
				var pa = element.childNodes[j];
				if(pa.nodeName == "PARAM" && pa.getAttribute("name") == "movie"){
					url = pa.getAttribute("value");
					break;
				}
			}
		}
		
		if(!url){
			continue;
		}
		
		var id = url.match(/(embed|v)\/(.+?)(\?|&|$)/)[2];
		var a = document.createElement("a");
		var 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);
	}
};

unEmbed(document.documentElement);

var thread = function(){
	var data = [],
		maxLoop = 50,
		pos = 0,
		loopCount = 0,
		started = false;
		
	var worker = function(){
		for (loopCount = 0; pos < data.length && loopCount < maxLoop; pos++, loopCount++) {
			unEmbed(data[pos]);
		}
	};
	
	var start = function(){
		if (started) return;
		
		started = true;
		
		worker();
		
		if (pos < data.length) {
			loopCount = 0;
			setTimeout(worker, 16);
		} else {
			started = false;
			data = [];
			pos = 0;
		}
	};
	
	var queue = function(node){
		data.push(node);
	};
	
	return {
		start: start,
		queue: queue
	};
}();

var observer = function(){
	// Observer
	
	new MutationObserver(function(mutations){
		var i, j, m;
		for(i = 0; i < mutations.length; i++){
			m = mutations[i];
			if(m.type != "childList"){
				return;
			}
			for(j = 0; j < m.addedNodes.length; j++){
				thread.queue(m.addedNodes[j]);
			}
		}
		thread.start();
		
	}).observe(document.body, {
		childList: true,
		subtree: true
	});
}();