Source for "Link to YouTube Video Pages from Embedded YouTube Videos"

By Jake Kasprzak
Has 6 other scripts.


// ==UserScript== 
// @name		Link to YouTube Video Pages from Embedded YouTube Videos
// @author     	Jake Kasprzak
// @namespace	http://www.jake.kasprzak.ca
// @version	0.2.0
// @description	Adds a link below an embedded YouTube video to the YouTube page on which the YouTube video is hosted.
// @include	* 
// @exclude	http://*youtube.*
// ==/UserScript== 



(function() {


//this function checks if there is an embedded YouTube video in the URL it is passed, and returns the node for adding it if it is a link to a YouTube video
function checkIfVideo(theURL) {

	
	//first part of link to be added
	var startOfURL = "http://youtube.com/watch?v=";
	
	//this regular expression matches what needs to be added to the startOfURL variable
	var re = new RegExp("http:\/\/.*\.youtube\.com\/v\/([^(\&|$)]*)");
	
	
	
	//check if a URL for an embedded video was passed. Return the link node to be added if so, return 0 if not
	var result = re.exec(theURL);
		
	if (result) {
		
		//first part of the text of the link to be added
		var linkText = "YouTube - ";
		
		// what is to be added if video titles are not to be added to links
		var defaultTitle = "video page";
			
		//the link node that the above information and the link itself are added to
		var linkNode = document.createElement('a');
		
		
		//then extract the data the is needed from the URL passed to function, then add it to the link node to be added
		var partsOfURL = theURL.split(re);
		
		var videoID = partsOfURL[1];
		
		var link = startOfURL + videoID;
		
		linkNode.href = link;
		
		
		
		//if videos titles are to be displayed in link, add them
		//this is based on code in the YouTube Title adder v1.2 script that can be found at http://userscripts.org/scripts/show/12113
		if (displayTitles) {
		
			GM_xmlhttpRequest({
				method:"GET",
				url:link,
				headers:{"User-Agent":"monkeyagent"},
				onload:function(content){
					
					
					var videoName = content.responseText.match(/<h1 id=\"video_title\">([^<]+)<\/h1>/);
					
					
					if (!videoName) { 
						videoName = content.responseText.match(/<div id=\"vidTitle\">([^<]+)<\/div>/); 
					}
					
					
					if (!videoName) {
						videoName = content.responseText.match(/<meta name=\"title\" content=\"([^<]+)\">/);
					}
					
					if (!videoName) {
						videoName = content.title.substr(10);
					}
					
					
					videoName = content.responseText.match(/<meta name=\"title\" content=\"([^<]+)\">/);
					
					
					if (videoName) {
						linkText = linkText + videoName[1];
					}
					
					linkNode = addToNode(linkNode, linkText);
					
					
				}
			}) //GM_xmlhttpRequest
		
		
		}
		//if option for adding titles is disabled, simply mention in the link that it is a link to the poge for the video
		else {
			
			linkText = linkText + defaultTitle;
			linkNode = addToNode(linkNode, linkText);
					
		}
			
		return linkNode;

	} //if
	
	else {
		return 0;
	
	}
	
} //checkIfVideo


//given a link node and the text to be added to it, return the link node with a break tag before it followed by the link with the text to be in it
function addToNode(linkNode, linkText) {
	
	var breakNode = document.createElement('br');
	
	var linkTextNode = document.createTextNode(linkText);
	
	linkNode.appendChild(breakNode);
	linkNode.appendChild(linkTextNode);
	
	return linkNode;
} //addToNode



//this function is for initializing menu option for adding/removing video titles to or from links
function makeMenuToggle(key, defaultValue, toggleOn, toggleOff) {
  // Load current value into variable
  window[key] = GM_getValue(key, defaultValue);
  // Add menu toggle
  GM_registerMenuCommand((window[key] ? toggleOff : toggleOn), function() {
    GM_setValue(key, !window[key]);
	
  });
}  //makeMenuToggle



//variables for storing all nodes that it looks for, and each indivdual one of these nodes, respectively
var allLocations, thisLocation;


//variable for the node to be added, and the URL that is to be inserted in that node
var linkNode, theURL;



//create option for displaying video titles, and have it display video titles by default, 
makeMenuToggle("displayTitles", true, "Display Video Titles in Links to Embedded Videos", "Do Not Display Video Titles in Links to Embedded Videos");




//look for <embed> tags as well as <object> tags that have data attributes as they might be used to embed videos 
allLocations = document.evaluate(
    "//object[@data] | //embed",
    document,
    null,
    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
    null);	
	

	
	
//loop through each node that is found, check if for embdded video in tags it finds, and add links to YouTube page if embedded YouTube videos are found
for (i = 0; i < allLocations.snapshotLength; i++) {
    
	
	linkNode = 0;
	theURL = 0;
	
	thisLocation = allLocations.snapshotItem(i);
	
	
	//how to get URL information depends on what kind of tags are used to embed the video
	switch (thisLocation.nodeName.toUpperCase()) {
        case 'OBJECT':
			
			theURL = thisLocation.data;
			break;
		
		case 'EMBED':
		
			theURL = thisLocation.src;
			break;
		
	}
	
	if (theURL) {
		linkNode = checkIfVideo(theURL);
	}
	
	if (linkNode) {
				
		//the link must be added immediately after the object, not as a child of an object tag, so NoScript will not remove it
		if (thisLocation.parentNode.nodeName.toUpperCase()=="OBJECT") {
			thisLocation.parentNode.parentNode.insertBefore(linkNode, thisLocation.parentNode.nextSibling);
		}
		else {
			thisLocation.parentNode.insertBefore(linkNode, thisLocation.nextSibling);
		}
	
	}
	
	
} //for	


})();