Wikitube - YouTube on Wikipedia & Wikiwand

Adds relevant YouTube videos to Wikipedia and Wikiwand.

// ==UserScript==
// @name         Wikitube - YouTube on Wikipedia & Wikiwand
// @namespace    WYOWW
// @version      3.1
// @description  Adds relevant YouTube videos to Wikipedia and Wikiwand.
// @include      http*://*.wikipedia.org/wiki*
// @include      http*://www.wikiwand.com/*/*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @author       drhouse
// @developer    Mark Dunne | http://markdunne.github.io/ | https://chrome.google.com/webstore/detail/wikitube/aneddidibfifdpbeppmpoackniodpekj
// @icon         https://en.wikipedia.org/static/favicon/wikipedia.ico
// ==/UserScript==

$(document).ready(function () {

	function addGlobalStyle(css) {
		var head, style;
		head = document.getElementsByTagName('head')[0];
		if (!head) { return; }
		style = document.createElement('style');
		style.type = 'text/css';
		style.innerHTML = css;
		head.appendChild(style);
	}

	addGlobalStyle('#wikitube_container { padding-bottom: 2px;margin-bottom: 5px;border-bottom: 1px solid #aaa; overflow-x:hidden; overflow-y:hidden; white-space: nowrap; margin-top: -10px;}');
	addGlobalStyle('#wikitube_container:hover {	overflow-x:scroll; }');
	addGlobalStyle('#wikitube_container div {width: auto; height: 200px; margin-right: 5px; display: inline-block; box-shadow: 0 0 5px #888; }');
	addGlobalStyle('#wikitube_container .plusBtn {	width: 100px;	text-align: center;	border-radius: 5px;	background-color: rgb(192, 62, 62);	background-position: center;	background-repeat: no-repeat;	cursor: pointer;}');
	addGlobalStyle('#wikitube_container .plusBtn:hover { background-color: rgb(192, 92, 92); }');

	var allow_path = function(path){
		var banned_paths = [
			//landing pages
			'/wiki/Main_Page',
			'/wiki/Wikipedia:Portada',
			'/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3%83%9A%E3%83%BC%E3%82%B8',
			'/wiki/Wikip%C3%A9dia:Accueil_principal',
			'/wiki/Wikip%C3%A9dia:P%C3%A1gina_principal',
			'/wiki/Wikipedia:%E9%A6%96%E9%A1%B5',
			'/wiki/Wikipedia:Strona_g%C5%82%C3%B3wna',
			'/wiki/Pagina_principale',
			'/wiki/Wikipedia:Hauptseite',
			'/wiki/%D0%97%D0%B0%D0%B3%D0%BB%D0%B0%D0%B2%D0%BD%D0%B0%D1%8F_%D1%81%D1%82%D1%80%D0%B0%D0%BD%D0%B8%D1%86%D0%B0'
		];
		for (var i = 0; i < banned_paths.length; i++) {
			if(path == banned_paths[i]){
				return false;
			}
		}
		if (path.indexOf(':') != -1) {
			//portals, users, categories
			return false;
		}
		return true;
	}

	var title_text;
	var num_videos_to_load;
	var num_videos_loaded = 0;
	var more_videos_button = $('<div class="plusBtn" title="Load more videos!"></div>'); 
	var container = $('<div id="wikitube_container"></div>');

	var first_load = function(){
		if( $('#mw-content-text').length )         //wikipedia  
		{
			container.insertBefore('#mw-content-text');
		}
		if( $('#fullContent').length )         //wikiwand
		{
			container.insertBefore('#fullContent');
		}

		container.append(more_videos_button);
		
		var plusImgURL = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAACXBIWXMAAAsTAAALEwEAmpwYAAAKT2lDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFPpFj333vRCS4iAlEtv\
UhUIIFJCi4AUkSYqIQkQSoghodkVUcERRUUEG8igiAOOjoCMFVEsDIoK2AfkIaKOg6OIisr74Xuja9a89+bN/rXXPues852zzwfACAyWSDNRNYAMqUIeEeCDx8TG4eQuQIEKJHAAEAizZCFz/SMBAPh+PDwrIsAHvgABeNMLCADATZvAMByH/w/qQplcA\
YCEAcB0kThLCIAUAEB6jkKmAEBGAYCdmCZTAKAEAGDLY2LjAFAtAGAnf+bTAICd+Jl7AQBblCEVAaCRACATZYhEAGg7AKzPVopFAFgwABRmS8Q5ANgtADBJV2ZIALC3AMDOEAuyAAgMADBRiIUpAAR7AGDIIyN4AISZABRG8lc88SuuEOcqAAB4mbI8uSQ\
5RYFbCC1xB1dXLh4ozkkXKxQ2YQJhmkAuwnm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ERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERER\
ERERERERERERERERERERERERERERERERERERERERERERlvLzngAAAEF0Uk5TAAABAgMEBQYHCAkKCwwNDg8QEhUXGBkaHR4gISMkJSYpKy4xMjM0Nzg7PUFGR0hNZWZnr7fHyNrb5ufo8vP19v4deCuTAAACGklEQVRYw+2XWXOiQBSFnV6jgmy2gqC4oSgGY3RMJjEL//9PxQAd\
DTFTtD0PTlXO65WvKLvv4ZzKL0lVLgJQOS0AIMKEEIwgAKd/8jcAgJhWFU03dE2pUQyBIAAgWtetltNxO07L0usUASEAQFcNy/aHk2k4nQx922pcnSJ8C9g/r7Hu6GZ995Q83a1Xoy7TThG+BxC16cfbhOt37DdVIgCAVdNdPCQH/Vm4ZhWWByCFDTbJsTYDpqDyAKzZwcsnwEtga\
7g8gBhumHxW6BqkPICa3qwAmHkmFQH05gXAvPcDuHxAZj80l8r6UQEQ9ZmaTw8edQAARPb2Y5i5mBfEBUAceCyfGppSJelyfwD2+6+azHa9XqZ+EC0LgGUU9LOh59rMVFN74IB3/2i6gyCczTNF8fILYBlH2XAWBgO3mRoMB0DaYP5i85yU1PNm4bMGhRwAcN3qXj8mAnq87lp1DDi\
A6s5omwhpO7J1ygGwZvmrRFAr36rBHICU1nAtClgPWwrKAVhzJveigPuJo+EcQPTOdCcK2E07OuGAvQO+igJe303yX72B9H+AlPY5p9D+OIWz7sHt0T047yY6h5uY7kIssQt8G0ufxK6wjdwPxmX9YFz0A2lHkvdEeVf++bRdJEAyZEnHvK9BczcWCprSUTcN28cG8SAYtotxfysa9/\
PCsTq7cEhXHl662meXrqz21SRqn1Dx/M/L9xutmnjEud3T6wAAAABJRU5ErkJggg==';
		
		more_videos_button.css('background-image', 'url(' + plusImgURL + ')');

		more_videos_button.click(function(){
			load_new_videos(false);
		});
	}

	var load_new_videos = function(is_first_load){
		var url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&q='+title_text+'&key=AIzaSyC1ucHysupgOH1JQmPaGqfFGoO1QCCOhQA&maxResults='+(num_videos_loaded+num_videos_to_load);
		$.getJSON(url, function(response){
			if(response['items'].length > 0){
				if (is_first_load) {
					first_load();
				}
				var new_videos = videos = response['items'];
				new_videos = new_videos.slice(num_videos_loaded);
				num_videos_loaded += num_videos_to_load;
				add_videos_to_page(new_videos);
			}
		});
	}

	var add_videos_to_page = function(new_videos){
		for (var i = 0; i < new_videos.length; i++) {
			video = new_videos[i];
			var videoHtml = '<div><iframe width="350" height="200" frameborder="0" allowfullscreen src="//www.youtube.com/embed/'+video['id']['videoId']+'"></iframe></div>';
			more_videos_button.before(videoHtml);
		};
	}

	var test_func = function(){
		url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&q=memes&key=AIzaSyC1ucHysupgOH1JQmPaGqfFGoO1QCCOhQA';
		$.getJSON(url, function(response){
			console.log(response);
		})
	}

	$(document).ready(function(){
		if(allow_path(window.location.pathname)){

			if( $('#mw-content-text').length )       //wikipedia  
			{
				title_text = document.getElementById('firstHeading').innerText;
				num_videos_to_load = Math.floor($('#bodyContent').width() / 350); //video width = 350px
			}
			if( $('#fullContent').length )         //wikiwand
			{
				title_text = $('.firstHeading > span').text();
				num_videos_to_load = Math.floor($('#article_content_wrapper').width() / 350); //video width = 350px
			}
			load_new_videos(true);
		}
	});

});