XTube - Video Download Link

By Signe Last update Jul 8, 2009 — Installed 4,202 times. Daily Installs: 20, 10, 9, 7, 6, 15, 15, 11, 15, 7, 10, 13, 5, 3, 14, 12, 6, 12, 18, 5, 9, 13, 17, 6, 2, 2, 10, 6, 8, 24, 11, 7

There are 6 previous versions of this script.

// ==UserScript==
// @name          XTube - Video Download Link
// @namespace     http://www.cothlamadh.net/greasemonkey
// @author        Signe - http://www.cothlamadh.net/
// @description	  Modify the video page to include a link to download the video to your desktop.  This is a completely rewritten and optimized version of a script originally by Lorax
// @include       http://v*.xtube.com/watch_video.php?*
// @include       http://v*.xtube.com/watch.php?*
// ==/UserScript==


String.prototype.getPref = function(s, splitter) {
		return this.split(s+"=")[1].split((splitter||"&"))[0];
};

function genDownloadLink() {
		var url = window.location.search.substring(1);

    // All of the query parameters we need are part of the watch.php page we're already viewing
    var whos  = url.getPref('v_user_id');
    var vix   = url.getPref('idx');
    var vidid = url.getPref('v');
    var clid  = url.getPref('cl');

    // Make the search request and then update the document with a link to the video
    GM_xmlhttpRequest({
        method: 'POST',
        url: "http://video2.xtube.com/find_video.php",
        data: "user%5Fid=" + escape(whos) + "&clip%5Fid=" + escape(clid) + "&video%5Fid=" + escape(vidid),
        headers: {
            'User-agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1b3pre) Gecko/20081204 Shiretoko/3.1b3pre',
            'Accept': 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        onload: function(responseDetails) {
            /**
             * From output that looks like this:
             * &filename=/videos/2008/20080911pm/5721S4Jd0EB.flv&clip_id=7aX3d-S721-&status=1&preview_type=0&preview_video_id=&preview_clip_id=
             * We're taking everything after "&filename=".
             */
            // var flvPath = responseDetails.responseText.split('&')[1].split('=')[1];
            var flvPath = responseDetails.responseText.substr(10);

            // Previously we had to locate a specific CDN, but now they have a central server
            var cdnSrv = "cdns.xtube.com/s/e13";
            
            /**
             * Modify the video's title to include a download link
             */
            // Locate the h2, add two non-breaking spaces
            var anchorExpr = document.createExpression('//td/h2', null);
            var h2 = anchorExpr.evaluate(document, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
            h2.innerHTML += '  ';

            // Create a new span and add the opening paren for the link we're about to add
            var newSpan = document.createElement('span');
            newSpan.innerHTML = '(';
            
            // Create a new link and set the path and text
            var newAnchor = document.createElement('a');
            newAnchor.href = 'http://' + cdnSrv + flvPath;
            newAnchor.innerHTML = 'Download this video';

            // Add the link to the span and then add the closing paren
            newSpan.appendChild(newAnchor);
            newSpan.innerHTML += ')';
            
            // Finally add the span to the h2
            h2.appendChild(newSpan);

            /* and thats that! */
        }
    });
}

genDownloadLink();