Youtube Persist Worldwide

By Burn Last update Dec 4, 2011 — Installed 9,122 times.

There are 35 previous versions of this script.

// ==UserScript==
// @name           Youtube Persist Worldwide
// @namespace      http://userscripts.org/scripts/show/33551
// @description    Force youtube to search for worldwide contents
// @include        http://*.youtube.com/*
// @exclude        http://*.youtube.com/user/*
// @version        0.5.7
// @author         Burn
// ==/UserScript==

const DBG = false; // set to true to enable messages in your JS console

var cversion = '0.5.7';
var time = new Date().getTime()+'';
var getName = window.location.host+'.lastCheck';
var ObjRef = "masthead-container";
var last_check = GM_getValue(getName);
if(!last_check) { GM_setValue(getName,time); }

// check for updates here... code adapted from http://userscripts.org/scripts/show/52398
var ypwspy = GM_getValue("ypw");
if (ypwspy == "ok" && (time - last_check > 86400000)) {

	GM_deleteValue("ypw");
	if (document.getElementById(ObjRef)) {	
		GM_setValue(getName, time);
		GM_xmlhttpRequest({
			method:				"GET",
			url:				"http://userscripts.org/scripts/source/33551.meta.js?sinc="+time,
			headers:			{ Accept:"text/javascript; charset=UTF-8" },
			overrideMimeType:	"application/javascript; charset=UTF-8",
			onload:				function(response) { checkversion(response, ObjRef); }	
		});	
	} else {if (DBG) GM_log(ObjRef + " not found, skipping updates check");}
}
// end updates check


function ForceWorldwideContents() {
	try {
		var ClassN = document.body.className.toString();
		if (!ClassN) {return false;}
		if (DBG){GM_log(ClassN);}
		var RE_lang = /[a-z]{2}_[A-Z]{2}/;
		var lang = ClassN.match(RE_lang).toString();
	} catch(e) {
		GM_log(e);
	}
	if(DBG){GM_log(">"+lang+"<");}
	var stri = "";
	switch (lang) {case "it_IT": stri = "a livello mondiale";break;case "de_DE":stri = "weltweit";break;case "en_GB":case "en_US":stri = "worldwide";break;case "es_ES":stri = "mundial";break;case "es_MX":stri = "en todo el mundo";break;case "fr_FR":stri = "dans le monde entier";break;case "nl_NL":stri = "wereldwijd";break;case "pl_PL":stri = "cały świat";break;case "he_IL":stri = "כלל עולמי (כולם)";break;case "ru_RU":stri = "По всему миру";break;case "bg_BG":stri = "от цял свят";break;case "":default:GM_log('Language not found: ->'+lang+'<-');return false;}
	if(DBG){GM_log("stri: ["+stri+"]");}
	var lis = document.evaluate("//ul[@class='pickers yt-uix-button-group']/li/button/span",document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
	if(DBG){GM_log("found "+lis.snapshotLength + " nodes");} // how many <li> with the <ul> parent having class='pickers yt-uix-button-group'
	
	if(lis.snapshotLength==0){
		GM_log("node not found");
		return;
	}
	
	// workaround to fix continue reloading on Opera
	if (lis.snapshotLength == 3) {
		var l = lis.snapshotItem(1);
	} else if (lis.snapshotLength == 4) {
		var l = lis.snapshotItem(2);
	}
	
	if (l.textContent.toLowerCase().indexOf(stri) == -1) {
		if (DBG){GM_log('textContent found: ' +  l.textContent);} 
			ypwspy = GM_getValue("ypw");
			if(!ypwspy) { GM_setValue("ypw","ok"); }
			if (DBG){GM_log('Worldwide contents ok!');}	
			redirectWithNewParam('US', 'gl');
	}  else {
		if(DBG){GM_log('nothing to do: ' + l.textContent);}
	}
	
}

document.getElementsByClassName = function(clsName) {
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for (var i = 0;i < elements.length;i++) {
        if(elements[i].className.indexOf(" ") >= 0){
            var classes = elements[i].className.split(" ");
            for (var j = 0;j < classes.length;j++) {
                if(classes[j] == clsName)
                    retVal.push(elements[i]);
            }
        } else if(elements[i].className == clsName) {
            retVal.push(elements[i]);
        }
    }
    return retVal;
}

// the following functions are taken from youtube.com - thanks Firebug
function redirectWithNewParam(a,b) {
	var c,d;
	c=window.location.href;
	c=c.split("#");
	d=c.length==2?"#"+c[1]:"";
	c=c[0];
	var e=c.match(/[\?&]\w+=[^&#]*/g), f={};
	if(e) for(var g=0;g<e.length;++g){
		e[g]=e[g].split("=");
		f[e[g][0].substring(1)]=decodeURIComponent(e[g][1].replace(/\+/g,"%20"))
	}
	f[b]=a;
	f["persist_"+b]="1";
	c=c.split("?");
	c=c[0];
	oj(c,f,d);
}
var i = null;
var gd=function(a,b){for(var c in b){var d=c,e=b[c],f=a;if(ea(e))for(var g=0;g<e.length;g++){f.push("&",d);e[g]!==""&&f.push("=",sa(e[g]))}else if(e!=i){f.push("&",d);e!==""&&f.push("=",sa(e))}}return a}, hd=function(a){a=gd([],a);a[0]="";return a.join("")};
var id=function(a,b){var c=gd([a],b);if(c[1]){var d=c[0],e=d.indexOf("#");if(e>=0){c.push(d.substr(e));c[0]=d=d.substr(0,e)}e=d.indexOf("?");if(e<0)c[1]="?";else if(e==d.length-1)c[1]=undefined}return c.join("")};
var oj=function(a,b,c){c=c||"";window.location=id(a,b||{})+c;};
var ea=function(a){return da(a)=="array"};
var sa=function(a){a=String(a);if(!ra.test(a))return encodeURIComponent(a);return a};
var ra=/^[a-zA-Z0-9\-_.!~*'()]*$/;
var da=function(a){var b=typeof a;if(b=="object")if(a){if(a instanceof Array||!(a instanceof Object)&&Object.prototype.toString.call(a)=="[object Array]"||typeof a.length=="number"&&typeof a.splice!="undefined"&&typeof a.propertyIsEnumerable!="undefined"&&!a.propertyIsEnumerable("splice"))return"array";if(!(a instanceof Object)&&(Object.prototype.toString.call(a)=="[object Function]"||typeof a.call!="undefined"&&typeof a.propertyIsEnumerable!="undefined"&&!a.propertyIsEnumerable("call")))return"function"}else return"null";else if(b=="function"&&typeof a.call=="undefined")return "object";return b}
// end of youtube functions

// the following function is taken from http://userscripts.org/scripts/show/52398
function checkversion(response,ObjRef) {
	if (DBG) GM_log(ObjRef);
	var availableVersion = "";
	var TmpStr = "";
	if (response.status == 200) {
		
		var resReg = /\/\/ @version\s+(\d+\.\d+\.\d+)/.exec(response.responseText);
		if (resReg != null) {
		if(DBG) GM_log(typeof(resReg));
			availableVersion = resReg[1];
			if(DBG) GM_log(availableVersion);
		}
	} else {
		if(DBG) GM_log("XHR error: "+response.status + " :end XHR");
		availableVersion = cversion;
		TmpStr += "Updates check failed.";
	}
	if (availableVersion!=cversion) {
		TmpStr += "YOUTUBE PERSIST WORLDWIDE<br />New Version Available<br /><a title='Click here to update Youtube Persist Worldwide!' href='http://userscripts.org/scripts/source/33551.user.js'><b>CLICK TO UPDATE NOW</b></a>";
		if(DBG) {GM_log(response.responseText);GM_log(availableVersion);}
	}
	if(DBG) GM_log('innerHTML: ' + TmpStr);
	var sp = document.createElement('div');
	sp.setAttribute('style','background-color:#FFEEBF;padding:6px;position:absolute;margin-top:-55px;text-align:center;color:#333;font-size:8px;z-index:1001;display:block !important');
	sp.innerHTML = TmpStr;
	document.getElementById(ObjRef).parentNode.appendChild(sp);
}

if (document.addEventListener) {window.addEventListener("load", ForceWorldwideContents, false);}
else {window.document.onLoad = ForceWorldwideContents();}