There are 18 previous versions of this script.
// ==UserScript==
// @name Quietube Auto-Redirect
// @namespace cyranix
// @description Auto-redirect YouTube, Vimeo, and Viddler videos to Quietube.
// @include *
// ==/UserScript==
// Determines whether the current site is Quietube.
function isQuietube() {
var quietube = /^http:\/\/(www\.)?quietube\.com\/v\.php\/(.)+/.test(document.URL);
return quietube;
}
// Determines whether a given URL points to a Quietube-supported video site.
function isSupported(url) {
var youtube = /^http:\/\/(www\.)?youtube\.com\/watch\?v=(.)+/.test(url);
var vimeo = /^http:\/\/(www\.)?vimeo\.com\/(\d)+$/.test(url.split("?")[0]);
var viddler = /^http:\/\/(www\.)?viddler\.com\/explore\/(.)+\/videos\/(.)+/.test(url);
var bbc = /^http:\/\/(www\.)?bbc\.co\.uk\/iplayer\/episode\/(.)+/.test(url);
return (youtube || vimeo || viddler || bbc);
}
// Determines whether a given URL should be exempt from redirection. Method extracted in case I want to modify it later.
function preventRedirect(url) {
var prevent = /redirect=0/.test(url);
return prevent;
}
// Aggressively converts relevant links to supported video sites to their Quietube counterparts, as a form of "pre-direction".
function convertLinks() {
var links = document.evaluate("//a[@href]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0; i < links.snapshotLength; i++) {
var link = links.snapshotItem(i);
if (isSupported(link.href)) {
link.href = "http://quietube.com/v.php/" + link.href;
}
}
}
// More robust way to get the Original Page link.
function getOriginalPageLink() {
var arrayLinks = document.getElementsByTagName("a");
for (var i = 0; i < arrayLinks.length; i++) {
var current = arrayLinks[i];
if (current.innerHTML == "Original Page") {
return current;
}
}
}
var doConvert = true; // set to false in order to disable aggressive link modification
// If site is Quietube, alter the Original Page link to include the no-redirect parameter.
if (isQuietube()) {
var original = getOriginalPageLink();
original.href = (/youtube\.com/.test(document.URL)) ? original.href + "&redirect=0" : original.href + "?redirect=0";
}
// If site is a supported video site and doesn't have the no-redirect parameter, redirect to Quietube.
else if (isSupported(document.URL) && !preventRedirect(document.URL)) {
window.location.href = "http://quietube.com/v.php/" + window.location.href;
}
// If site is neither a supported video site nor Quietube, attempt to preemptively alter links to supported video sites.
else if (doConvert) {
convertLinks();
}
