Skip to content

Instantly share code, notes, and snippets.

@XP1
Created August 15, 2011 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XP1/1147490 to your computer and use it in GitHub Desktop.
Save XP1/1147490 to your computer and use it in GitHub Desktop.
Bypass YouTube Age Verification: Redirects from the age verification webpage to the comments webpage and then embeds the age-restricted video. Based on @display's "Bypass youtube age restrict" (http://userscripts.org/scripts/show/88856/).
// ==UserScript==
// @name Bypass YouTube Age Verification
// @version 1.01
// @description Redirects from the age verification webpage to the comments webpage and then embeds the age-restricted video. Based on @display's "Bypass youtube age restrict" (http://userscripts.org/scripts/show/88856/).
// @author XP1 (https://github.com/XP1/)
// @namespace https://gist.github.com/1147490/
// @include http*://youtube.*/verify_age?*
// @include http*://*.youtube.*/verify_age?*
// @include http*://youtube.*/all_comments?*&embed=1*
// @include http*://*.youtube.*/all_comments?*&embed=1*
// ==/UserScript==
// Battlefield 3 Physical Warfare Pack Gameplay Trailer:
// http://www.youtube.com/watch?v=G-ukxgldpOo
/*jslint browser: true, vars: true, white: true, maxerr: 50, indent: 4 */
(function (console, topWindow, windowLocation)
{
"use strict";
if (window.self !== topWindow)
{
return;
}
var userScript =
{
name: "Bypass YouTube Age Verification"
};
function removeEventListenerAfterFiring(numberOfTimes, callback)
{
var remaining = numberOfTimes;
return function listener(event)
{
remaining -= 1;
if (remaining <= 0)
{
var target = event.target;
var type = event.type;
target.removeEventListener(type, listener, false);
target.removeEventListener(type, listener, true);
}
callback();
};
}
var handleException = function (theFunction)
{
try
{
theFunction();
}
catch (exception)
{
console.log("[" + userScript.name + "]: Exception: " + exception);
}
};
var playerSizes =
{
EMBED_SMALL: {width: 425, height: 349},
EMBED: {width: 480, height: 390},
STANDARD: {width: 640, height: 363},
EMBED_MEDIUM: {width: 640, height: 510},
EMBED_LARGE: {width: 960, height: 750}
};
var options =
{
playerSize: playerSizes.STANDARD,
playerArguments:
{
// Documentation: http://code.google.com/apis/youtube/player_parameters.html
// String or boolean ("0" or "1") only. Null is the default that will embed without parameter and argument.
autohide: {parameter: "autohide", value: "2"},
autoplay: {parameter: "autoplay", value: true},
border: {parameter: "border", value: null},
closedCaptions: {parameter: "cc_load_policy", value: true},
progressBarColor: {parameter: "color", value: null},
primaryBorderColor: {parameter: "color1", value: null},
controlBarColor: {parameter: "color2", value: null},
controls: {parameter: "controls", value: null},
disableKeyboardControls: {parameter: "disablekb", value: null},
enhancedGenieMenu: {parameter: "egm", value: null},
enableJsApi: {parameter: "enablejsapi", value: null},
fullscreen: {parameter: "fs", value: true},
highDefinition: {parameter: "hd", value: null},
annotations: {parameter: "iv_load_policy", value: "1"},
loop: {parameter: "loop", value: false},
modestBranding: {parameter: "modestbranding", value: null},
origin: {parameter: "origin", value: null},
playerApiId: {parameter: "playerapiid", value: null},
playlist: {parameter: "playlist", value: null},
relatedVideos: {parameter: "rel", value: null},
showInfo: {parameter: "showinfo", value: false},
showSearch: {parameter: "showsearch", value: null},
startTime: {parameter: "start", value: null},
theme: {parameter: "theme", value: null}
}
};
/**
* Store the video data.
* Use the "find" functions to find the data first before calling the "get" functions to return the private variable.
* If a "find" function fails to find anything, then the default value stored in the private variable will be returned.
*/
var video = (function ()
{
var uri = windowLocation.href;
var uriProtocol = windowLocation.protocol;
var uriHost = windowLocation.host;
var uriPathname = windowLocation.pathname;
var uriStart = (uriProtocol + "//" + uriHost);
var uriEnd = (windowLocation.search + windowLocation.hash);
var id = null;
var publicMembers =
{
getUri: function ()
{
return uri;
},
getUriProtocol: function ()
{
return uriProtocol;
},
getUriHost: function ()
{
return uriHost;
},
getUriPathname: function ()
{
return uriPathname;
},
getUriStart: function ()
{
return uriStart;
},
getUriEnd: function ()
{
return uriEnd;
},
findId: function (theUri)
{
if (typeof theUri === "undefined")
{
theUri = uriEnd;
}
if (typeof theUri === "string")
{
theUri = window.encodeURIComponent(theUri);
handleException(function ()
{
var idRegex = /(v=|v%3D|v%253D|\/|%2F|%252F)([\w\-]{11,})/g;
id = idRegex.exec(theUri)[2];
});
}
},
getId: function ()
{
return id;
}
};
return publicMembers;
}());
function bypassAgeVerification()
{
video.findId();
if (video.getUriPathname().indexOf("/verify_age") !== -1) // Redirect to comments webpage.
{
if (typeof video.getId() === "string")
{
windowLocation.replace(video.getUriStart() + "/all_comments?v=" + video.getId() + "&embed=1");
}
}
else if (video.getUriEnd().indexOf("embed=1") !== -1) // Embed video on comments webpage.
{
handleException(function ()
{
var topWindowDocument = topWindow.document;
function removeElements()
{
function removeElement(element)
{
if (element !== null)
{
element.parentNode.removeChild(element);
}
}
var videoThumbnailsElement = topWindowDocument.getElementById("video-reference-info");
var videoRuntimeElement = topWindowDocument.querySelector(".runtime");
var videoThreadedCommentsOptionsElement = topWindowDocument.getElementById("threaded-option");
removeElement(videoThumbnailsElement);
removeElement(videoRuntimeElement);
removeElement(videoThreadedCommentsOptionsElement);
}
function injectHtml()
{
function generateArguments()
{
function toString(value)
{
if (typeof value === "string")
{
return value;
}
else if (typeof value === "boolean")
{
return (value ? "1" : "0");
}
else // Unknown data type.
{
return "";
}
}
var result = "";
var playerArguments = options.playerArguments;
var i = null;
for (i in playerArguments)
{
if (playerArguments.hasOwnProperty(i))
{
var playerArgument = playerArguments[i];
var value = toString(playerArgument.value);
if (value !== "")
{
result += ("&" + playerArgument.parameter + "=" + value);
}
}
}
return result;
}
var playerSize = options.playerSize;
var embedHtml = "<iframe width=\"" + playerSize.width + " \" height=\"" + playerSize.height + "\" src=\"" + video.getUriStart() + "/embed/" + video.getId() + "?"
+ (generateArguments())
+ "\" frameborder=\"0\" allowfullscreen></iframe>";
var videoDetailsElement = topWindowDocument.querySelector(".vfacets");
var videoDetails = videoDetailsElement.innerHTML.split(/<br\/?>/g);
videoDetailsElement.innerHTML = "<h1><h3>" + videoDetails[2] + "</h3></h1><br/>" + videoDetails[1] + "<br/>" + videoDetails[0];
var videoTitleElement = topWindowDocument.querySelector(".vtitle");
videoTitleElement.innerHTML = videoTitleElement.innerHTML + "<br/><strong>" + videoDetails[1] + "</strong><br/><br/>" + embedHtml;
}
function replaceTitle()
{
var titleElement = topWindowDocument.querySelector(".vtitle a");
if (titleElement !== null)
{
topWindowDocument.title = titleElement.innerHTML + " - YouTube";
}
}
function replaceDescription()
{
var descriptionElement = topWindowDocument.querySelector("meta[name=\"description\"]");
if (descriptionElement !== null)
{
descriptionElement.setAttribute("content", "");
}
}
removeElements();
injectHtml();
replaceTitle();
replaceDescription();
});
}
}
topWindow.addEventListener("DOMContentLoaded", removeEventListenerAfterFiring(1, bypassAgeVerification), false);
}(window.console, window.top, window.location));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment