YouTube Automatic Fmt=18 Adder

By JoeSimmons Last update Jan 27, 2009 — Installed 5,724 times. Daily Installs: 5, 5, 2, 5, 4, 6, 1, 6, 5, 1, 3, 7, 1, 3, 4, 3, 9, 7, 10, 5, 4, 6, 7, 4, 6, 3, 5, 7, 5, 5, 0, 13

There are 9 previous versions of this script.

// ==UserScript==
// @name           YouTube Automatic Fmt=18 Adder
// @namespace      http://userscripts.org/users/23652
// @description    Adds &fmt=18 to the url for high quality but not if any &fmt=x already exist and adds &fmt=18 to all the links
// @include        http://*.youtube.com/*
// @copyright      JoeSimmons
// ==/UserScript==

function makeHQ() {
var lhr = location.href;
if(/^https?:\/\/(\w+\.)?youtube\.com\/watch\?.*v=/.test(lhr)) {
if(!/(\&|\?)fmt=(18|17|16|34|35|22)/.test(lhr)) {
if(/((\&|\?)fmt=$)|((\&|\?)fmt=\&)|((\&|\?)fmt=\d+)/.test(lhr)) {location.replace(location.replace(/(\&|\?)fmt=(\d+)?/g, '&fmt=18'));}
else if(!/(\&|\?)fmt=\d+/.test(lhr)) {location.replace(location+='&fmt=18');}
}
}
}

function makeLinks() {
var i, ytlinks, l;
ytlinks = document.evaluate("//a[contains(@href, 'watch?v=')]", document, null, 6, null);
for(i=0; i<ytlinks.snapshotLength; i++) {
l = ytlinks.snapshotItem(i);
if(l.href.indexOf("&fmt=")==-1) {l.href+="&fmt=18";}
else {l.href=l.href.replace(/(\&|\?)fmt=\d+/,"&fmt=18");}
}
}

setTimeout(makeHQ, 0);
window.addEventListener('load', makeLinks, false);