// Thumbs2Links
// Copyright (c) 2007 TheDarkman
//
// This work is licensed under a Creative Commons License
// See http://creativecommons.org/licenses/by-nc-sa/2.5/
// 
// Update 1.8.1 (2007/03/16) - New code to show titles on the links thanks to lazyttrick at userscripts.org
//			     - Added support for webshots videos
//			     - Fixed support for tos.mindpoop.com videos & pics
// Update 1.8 (2007/02/17) - Added support for bolt.com videos
//			   - Added support for break.com videos
//			   - Added support for myvideo.de videos
//			   - Added support for tos.mindpoop.com pics
// Update 1.7 (2007/02/10) - Added support for rude.com videos
//			   - Added support for stickam.com user videos
//			   - Shows download link under embedded videos on infamousx.com
// Update 1.6 (2007/02/08) - Added support for imagebeaver.com albums
//			   - Added support for imagefap.com albums
//			   - Added support for photobucket.com albums
// Update 1.5 (2007/02/07) - Added support for yahoo albums
//			   - Tinypic changed layout so i had to update the code
// Update 1.4 (2007/02/07) - Added support for webshots albums
// Update 1.3 (2007/02/04) - Added support for tutaka.com pic galleries
// Update 1.2 (2007/02/03) - Fixed the code for tos.mindpoop.com now 100% working
//                         - Fixed code for dailymotion, no more double results
//			   - The script has his own button now, u can call the function everywhere you want
// Update 1.1 (2007/01/28) - Added partial support for www.tubehos.com & tos.mindpoop.com
//
// ==UserScript==
// @name         Thumbs2Links
// @version      1.8.1
// @namespace    http://thedarkman.altervista.org/
// @description  Collect direct links to vids & pics from the thumbnails page
// @include      http://*dailymotion.*/*
// @include      http://*tinypic.com/*
// @include      http://*youtube.com/*
// @include      http://*tubehos.com/*
// @include      http://*mindpoop.com/*
// @include      http://*tutaka.com/*
// @include      http://*webshots.com/*
// @include      http://*photos.yahoo.com/*
// @include      http://*imagebeaver.com/*
// @include      http://*imagefap.com/*
// @include      http://*photobucket.com/*
// @include      http://*rude.com/*
// @include      http://*infamousx.com/*
// @include      http://*stickam.com/*
// @include      http://*bolt.com/*
// @include      http://*myvideo.de/*
// @include      http://*break.com/*
// ==/UserScript==

var elements, vid;

    var gm_button=document.createElement('div');
        gm_button.setAttribute('id','t2l-button');
        gm_button.setAttribute('style','position:fixed;bottom:40px;right:10px;background-color:#FFFFFF;border:2px solid #000000;padding:5px;text-align:center;');

    var gm_paragraph=document.createElement('p');
        gm_paragraph.setAttribute('style','font:normal normal normal 12px Arial,Helvetica,sans-serif;color:#666198;text-decoration:none;margin:0;padding:0;');

    var t2l_span_1=document.createElement('span');
        t2l_span_1.setAttribute('id','t2l-span-1');
        t2l_span_1.setAttribute('style','color:#FF0000;cursor:pointer;');
    var t2l_span_1_text=document.createTextNode('Get direct links!');

    var gm_divider=document.createTextNode(' | ');

    var t2l_span_2=document.createElement('span');
        t2l_span_2.setAttribute('id','t2l-span-2');
        t2l_span_2.setAttribute('style','color:#777777;cursor:pointer;');
    var t2l_span_2_text=document.createTextNode('Close');

    document.getElementsByTagName('body')[0].appendChild(gm_button);
    gm_button.appendChild(gm_paragraph);
    gm_paragraph.appendChild(t2l_span_1);
    t2l_span_1.appendChild(t2l_span_1_text);
    gm_paragraph.appendChild(gm_divider);
    gm_paragraph.appendChild(t2l_span_2);
    t2l_span_2.appendChild(t2l_span_2_text);
    
    document.getElementById('t2l-span-1').addEventListener('click',findserver,false);
    document.getElementById('t2l-span-2').addEventListener('click',hidet2l,false);
    
function hidet2l()
{
  document.getElementById('t2l-button').setAttribute('style','display:none;');
}    

function findserver()
    {		
      addresults(); 
      if  (window.location.href.match(/webshots/i)!=null) getwebshotspics();
      if  (window.location.hostname.match(/tinypic/i)!=null) gettinylinks();
      if  (window.location.hostname.match(/youtube/i)!=null) gettubelinks();
      if  (window.location.href.match(/dailymotion/i)!=null) getdailylinks();
      if  (window.location.href.match(/mindpoop/i)!=null) gettoslinks();
      if  (window.location.href.match(/tubehos/i)!=null) gettubehoslinks();
      if  (window.location.href.match(/tutaka/i)!=null) gettutakapics();
      if  (window.location.href.match(/yahoo/i)!=null) getyahoopics();
      if  (window.location.href.match(/imagebeaver/i)!=null) getbeaverpics();
      if  (window.location.href.match(/imagefap/i)!=null) getfappics();
      if  (window.location.href.match(/photobucket/i)!=null) getbucketpics();
      if  (window.location.hostname.match(/rude/i)!=null) getrudelinks();
      if  (window.location.hostname.match(/stickam/i)!=null) getstickamlinks();
      if  (window.location.hostname.match(/bolt/i)!=null) getboltlinks();
      if  (window.location.hostname.match(/myvideo/i)!=null) getmyvideolinks();
      if  (window.location.hostname.match(/break/i)!=null) getbreaklinks();
}

function addresults()
    {
      var gm_results=document.createElement('div');
      gm_results.setAttribute('id','t2l-results');
      gm_results.setAttribute('style','background-color:#FFFFFF;border:2px solid #000000;padding:5px;text-align:left;'); 
      document.body.appendChild(gm_results);
      var gm_results_paragraph=document.createElement('p');
      gm_results_paragraph.setAttribute('style','font:normal normal normal 12px Arial,Helvetica,sans-serif;color:#777777;text-decoration:none;margin:0;padding:0;');
      var gm_results_span_1=document.createElement('span');
      gm_results_span_1.setAttribute('id','gm_results-span-1');
      var gm_results_span_1_text=document.createTextNode('-=[ Please wait while i load results... ]=-');
      gm_results.appendChild(gm_results_paragraph);
      gm_results_paragraph.appendChild(gm_results_span_1);
      gm_results_span_1.appendChild(gm_results_span_1_text);     
}

// Tinypic function
var gettinylinks = function() 
 {
  elements = document.getElementsByTagName('a');	// collect all the links
  for (i = 0; i < elements.length; i++) {
    if (elements[i].href.match(/player.php/i)!=null)  // if it's a link to a movie then grab the direct link	 	
    {
    	addlinka(elements[i].href.replace('player.php?v=',''));
     }     
   }      
}	                     

// Youtube function
var gettubelinks = function()  // if we are on a youtube thumbnails page
 {	
  elements = document.getElementsByTagName('a');	// collect all the links
  for (i = 0; i < elements.length; i++) {
    if ((elements[i].href.match(/watch/i)!=null) && (elements[i].innerHTML.match(/img/i)==null)) // if it's a link to a movie then download the page and grab the direct link	 	
    {
      GM_xmlhttpRequest({  //for each thumbnail we download the video page
        method:"GET",
        url:elements[i].href,
        onload:function(result)
          {            	
            title = result.responseText.match(/\<h1 id\=\"video\_title\"\>[^\<]+\</);
            if(title) 
              title = result.responseText.match(/\<h1 id\=\"video\_title\"\>[^\<]+\</)[0].match(/\>[^\<]+\</)[0].replace(/[\>\<]/g,"");	 
            else title = null;
	    lines = result.responseText.split("\n");  // Make an array out of the HTML
            for (var i = 0; i < lines.length; i++) {  // Loop through lines of the HTML		
			var line = lines[i];  		
			if (line.indexOf('SWFObject') != -1) {  // This line contains a movie
				addlinka('http://www.youtube.com/get_video?'+ line.substring(line.indexOf('video_id'),line.indexOf('movie_player')-4),title);
				break;
			}		
	    }     
	  }
      });
     }     
   }     
}	                     

// Dailymotion function
var getdailylinks = function()  // if we are on a dailymotion thumbnails page
 {	
  elements = document.getElementsByTagName('a');	// collect all the links
  for (t = 0; t < elements.length; t++) {
    if ((elements[t].href.match(/video/i)!=null) && (elements[t].getAttribute("class") == 'video_title')) // if it's a link to a movie then download the page and grab the direct link	 	
    {
      GM_xmlhttpRequest({  //for each thumbnail we download the video page
        method:"GET",
        url:elements[t].href,
        onload:function(result)
          {            	
            title = result.responseText.match(/\<h1\>[^\<]+\</);
            if(title) 
              title = result.responseText.match(/\<h1\>[^\<]+\</)[0].match(/\>[^\<]+\</)[0].replace(/[\>\<]/g,"");	 
            else title = null;          	
	    lines = result.responseText.split("\n");  // Make an array out of the HTML
            for (var i = 0; i < lines.length; i++) {  // Loop through lines of the HTML		
			var line = lines[i];  		
			if (line.indexOf('320x240') != -1 && line.indexOf('.flv') != -1) {  // This line contains a movie
				addlinka(unescape(line.substring(line.indexOf('http'),line.indexOf(';')-2)),title);
				break;
			}		
	    }     
	  }	
      });  
    }     
  }      
}	

// Mindpoop function
var gettoslinks = function()  // if we are on a mindpoop thumbnails page
 {	
  elements = document.getElementsByTagName('a');	// collect all the links
  for (t = 0; t < elements.length; t++) {
    if ((elements[t].href.match(/tos.php/i)!=null) && (elements[t].innerHTML.match(/bucket/i)==null) && (elements[t].innerHTML.match(/tinypic/i)==null))// if it's a link to a movie then download the page and grab the direct link	 	
    {
      GM_xmlhttpRequest({  //for each thumbnail we download the video page
        method:"GET",
        url:elements[t].href,
        onload:function(result)
          {            	
	    lines = result.responseText.split("\n");  // Make an array out of the HTML
            for (var i = 0; i < lines.length; i++) {  // Loop through lines of the HTML		
			var line = lines[i];  		
			if (line.indexOf('llnw') != -1) {  // This line contains a movie
				addlinka(line.substring(line.indexOf('youtube-')-7,line.indexOf('.flv')+4));	
				break;
			}				
			if (line.indexOf('youtubeconverter') != -1) {  // This line contains a movie
				var line2 = line.split('value');
				addlinka(line2[1].substring(line2[1].indexOf('http'),line2[1].indexOf('input')-3));
				break;
			}		
			if (line.indexOf('flv_converter') != -1) {  // This line contains a movie
				addlinka(line.substring(line.indexOf('converter')+12,line.indexOf('target')-2));
				break;
			}		
	    }     
	  }	
      });  
    }     
  }
  elements = document.getElementsByTagName('img');	// collect all the links
  for (i = 0; i < elements.length; i++) {
    if (elements[i].src.match(/photobucket/i)!=null) //&& (elements[i].src.match('flv')==null)) // if it's a link to a pic page convert it to a direct link	 	
    {
	addlinka('http://' + elements[i].src.substring(elements[i].src.indexOf('bucket-')+7).replace(/-dot-/g,'.').replace(/-slash-/g,'/').replace('_th.jpg','').replace('.flv.jpg','.flv').replace(/-space-/g,' '));
    }
    if (elements[i].src.match('tinypic-')!=null) // if it's a link to a video page convert it to a direct link	 	
    {
	addlinka('http://video.tinypic.com/' + elements[i].src.substring(elements[i].src.indexOf('tinypic-')+8).replace('.jpg','').replace('_th',''));
	//addlinka('http://video.tinypic.com/' + elements[i].src.substring(elements[i].src.indexOf('tinypic-')+8).replace('.jpg','') + "_th");
    } 
    if (elements[i].src.match('video.tinypic')!=null) // if it's a link to a video page convert it to a direct link	 	
    {
	addlinka(elements[i].src);
	addlinka(elements[i].src.replace('_th',''));
    }        
  }  
}	

// Tubehos function
var gettubehoslinks = function()  // if we are on a tubehos thumbnails page
 {	
  elements = document.getElementsByTagName('embed');	// collect all the links
  for (t = 0; t < elements.length; t++) {
      vid = 'http://www.youtube.com/watch?v=' + elements[t].src.substring(25,36);
      GM_xmlhttpRequest({  //for each thumbnail we download the video page
        method:"GET",
        url:vid,
        onload:function(result)
          {         
            title = result.responseText.match(/\<h1 id\=\"video\_title\"\>[^\<]+\</);
            if(title) 
              title = result.responseText.match(/\<h1 id\=\"video\_title\"\>[^\<]+\</)[0].match(/\>[^\<]+\</)[0].replace(/[\>\<]/g,"");	 
            else title = null;          	   	
	    lines = result.responseText.split("\n");  // Make an array out of the HTML
            for (var i = 0; i < lines.length; i++) {  // Loop through lines of the HTML		
			var line = lines[i];  		
			if (line.indexOf('SWFObject') != -1) {  // This line contains a movie
				addlinka('http://www.youtube.com/get_video?'+ line.substring(line.indexOf('video_id'),line.indexOf('movie_player')-4),title);
				break;
			}		
	    }     
	  }	
      });     
  }      
}	

// Tutaka function
var gettutakapics = function() 
 {
  elements = document.getElementsByTagName('a');	// collect all the links
  for (i = 0; i < elements.length; i++) {
    if (elements[i].href.match(/gallery.php/i)!=null)  // if it's a link to a gallery page convert it to a direct link	 	
    {
	vid = elements[i].href;
	addlinka('http://www.tutaka.com/images/' + vid.substring(vid.indexOf('u=')+2,vid.indexOf('&i=')) + '/' + vid.substring(vid.indexOf('&i=')+3));
    }
  }
}

// Webshots function
var getwebshotspics = function()  // if we are on a webshots thumbnails page
 {   		
  elements = document.getElementsByTagName('a');	// collect all the links
  for (t = 0; t < elements.length; t++) {
    if ((elements[t].getAttribute("class") == 'thumb') && (elements[t].href.match(/photo/i)!=null)) // if it's a link to a pic then download the page and grab the direct link	 	
    {
      GM_xmlhttpRequest({  //for each thumbnail we download the pic page
        method:"GET",
        url:elements[t].href,
        onload:function(result)
          {            	
	    lines = result.responseText.split("\n");  // Make an array out of the HTML
            for (var i = 0; i < lines.length; i++) {  // Loop through lines of the HTML		
			var line = lines[i];  		
			if (line.indexOf('fullsize') != -1) {  // This line contains a pic
				addlinka(unescape(line.substring(line.indexOf('http'),line.indexOf('.jpg')+4)));
				break;
			}		
	    }     
	  }	
      });  
    }     
    if ((elements[t].getAttribute("class") == 'thumb') && (elements[t].href.match(/video/i)!=null)) // if it's a link to a vid then download the page and grab the direct link	 	
    {
      GM_xmlhttpRequest({  //for each video we download the html page
        method:"GET",
        url:elements[t].href,
        onload:function(result)
          {            	
	    lines = result.responseText.split("\n");  // Make an array out of the HTML
            for (var i = 0; i < lines.length; i++) {  // Loop through lines of the HTML		
			var line = lines[i];  		
			if (line.indexOf('writeFlashVideo') != -1) {  // This line contains a pic
				addlinka(unescape(line.substring(line.indexOf('http'),line.indexOf('.flv')+4)));
				break;
			}		
	    }     
	  }	
      });  
    }         
  }      
}	

// Yahoo function
var getyahoopics = function()  // if we are on a webshots thumbnails page
 {   		
  elements = document.getElementsByTagName('a');	// collect all the links
  for (t = 0; t < elements.length; t++) {
    if (elements[t].href.match(/photo/i)!=null) // if it's a link to a pic then download the page and grab the direct link	 	
    {
      GM_xmlhttpRequest({  //for each thumbnail we download the pic page
        method:"GET",
        url:elements[t].href,
        onload:function(result)
          {            	
	    lines = result.responseText.split("\n");  // Make an array out of the HTML
            for (var i = 0; i < lines.length; i++) {  // Loop through lines of the HTML		
			var line = lines[i];  		
			if (line.indexOf('__sr_') != -1) {  // This line contains a pic
				addlinka(unescape(line.substring(line.indexOf('http'),line.indexOf('&saveas'))));
				break;
			}
	    }
	  }
      });  
    }
  }
}

// Imagebeaver function
var getbeaverpics = function() 
 {
  elements = document.getElementsByTagName('img');	// collect all the links
  for (i = 0; i < elements.length; i++) {
    if (elements[i].src.match(/files/i)!=null)  // if it's a link to a pic page convert it to a direct link	 	
    {
	addlinka(elements[i].src.replace('thumb/',''));
    }
  }
}

// Imagefap function
var getfappics = function() 
 {
  elements = document.getElementsByTagName('img');	// collect all the links
  for (i = 0; i < elements.length; i++) {
    if (elements[i].src.match(/thumb/i)!=null)  // if it's a link to a pic page convert it to a direct link	 	
    {
	addlinka(elements[i].src.replace('thumb','full'));
    }
  }
}

// Photobucket function
var getbucketpics = function() 
 {
  elements = document.getElementsByTagName('input');	// collect all the links
  for (i = 0; i < elements.length; i++) {
    if (elements[i].name.match(/urlcode/i)!=null)  // if it's a link to a pic page convert it to a direct link	 	
    {
	addlinka(elements[i].value.replace('?action=view&current=',''));
    }
  }
}

// Rude.com function
var getrudelinks = function()  // if we are on a rude.com thumbnails page
 {	
  elements = document.getElementsByTagName('a');	// collect all the links
  for (i = 0; i < elements.length; i++) {
    if ((elements[i].href.indexOf('/v/') != -1) && (elements[i].innerHTML.match(/img/i)!=null)) // if it's a link to a movie then download the page and grab the direct link	 	
    {
      GM_xmlhttpRequest({  //for each thumbnail we download the video page
        method:"GET",
        url:elements[i].href + '/view_xml/',
        onload:function(result)
          {            	
	    lines = result.responseText.split("\n");  // Make an array out of the HTML
            for (var i = 0; i < lines.length; i++) {  // Loop through lines of the HTML		
			var line = lines[i];  		
			if (line.indexOf('.flv') != -1) {  // This line contains a movie
				title = lines[i - 6].replace('<![CDATA[','').replace(']]>','');
				addlinka(line.substring(line.indexOf('http'),line.indexOf('.flv')+4),title);
				break;
			}
	    }
	  }
      });
     }
   }
}

// Stickam function
var getstickamlinks = function() 
 {
  elements = document.getElementsByTagName('img');	// collect all the links
  for (i = 0; i < elements.length; i++) {
    if (elements[i].src.match(/converted/i)!=null)  // if it's a link to a video page convert it to a direct link	 	
    {
	addlinka(elements[i].src.replace('.jpg',''));
    }
  }
}

// Bolt function
var getboltlinks = function() 
 {
  elements = document.getElementsByTagName('img');	// collect all the links
  for (i = 0; i < elements.length; i++) {
    if ((elements[i].src.match(/streams/i)!=null) && (elements[i].src.match(/content/i)!=null)) // if it's a link to a gallery page convert it to a direct link	 	
    {
	vid = elements[i].src.replace('image','movie').replace('thumbnail','movie').replace('.flv','').replace('/tm/','/movie/');
	addlinka(vid.substring(0,vid.indexOf('.jpg')-2) + '.flv');
    }
    else if (elements[i].src.match(/streams/i)!=null)
      {
      parte = elements[i].parentNode.href.split('/');	
      GM_xmlhttpRequest({  //for each thumbnail we download the video page
        method:"GET",
        url:'http://utils.bolt.com/portfolio/video/genXml.bt?contentId=' + parte[6],
        onload:function(result)
          {            	
	    lines = result.responseText.split("\n");  // Make an array out of the HTML
            for (var i = 0; i < lines.length; i++) {  // Loop through lines of the HTML		
			var line = lines[i];  		
			if (line.indexOf('mediafile') != -1) {  // This line contains a movie
				addlinka(line.substring(line.indexOf('mediafile')+10,line.indexOf('.flv')+4));
				break;
			}
	    }
	  }
      });
      }	
  }
}

// Myvideo function
var getmyvideolinks = function() 
 { 
  elements = document.getElementsByTagName('img');	// collect all the links
  for (i = 0; i < elements.length; i++) {
    if (elements[i].src.match(/thumbs/i)!=null)  // if it's a link to a video page convert it to a direct link	 	
    {
	addlinka(elements[i].src.replace('_1.jpg','.flv').replace('/thumbs',''));
    }
  }
}

// Break function
var getbreaklinks = function() 
 { 
  elements = document.getElementsByTagName('img');	// collect all the links
  for (i = 0; i < elements.length; i++) {
    if (elements[i].src.match(/dnet/i)!=null)  // if it's a link to a video page convert it to a direct link	 	
    {
	addlinka(elements[i].src.replace('_Break_Thumb_100_1.jpeg','.flv').replace('.jpg','.flv').replace('.gif','.flv'));
    }
  }
}

// this makes the file smaller
function addlinka(linka,titlename)
 {
  var a = document.createElement('a');
  a.setAttribute('style','font:normal normal normal 12px Arial,Helvetica,sans-serif;color:#777777;text-decoration:none;margin:0;padding:0;');
  a.href = linka;	
  a.href = a.href.replace('&#37;3F','?').replace('&#37;3D','=').replace('mindpoop.com','v1.tinypic.com');
  if (titlename != null) a.innerHTML = titlename+'<br>';
  else a.innerHTML = a.href+'<br>';
  document.getElementById('t2l-results').appendChild(a);  // add a link to the video on the bottom of the page
}

// EXTRA = replace tinypic Random link with download link
if (window.location.href.match(/player.php/i)!=null) // if we are on a video page
 {
  elements = document.getElementsByTagName('a');	
  for (i = 0; i < elements.length; i++) {
    if (elements[i].href.match(/Random.php/i)!=null) // we found the 'Random' link
    {
      elements[i].href=window.location.href.replace('player.php?v=','')
      elements[i].innerHTML = 'Download!';   // then we replace the 'Random' link with the new 'Download!' 
    }
  }
}

// EXTRA - if we are on a webshots page lets take care of those annoying ads
if (window.location.host.match(/webshots/i)!=null) 
{
  document.getElementById('footer').parentNode.removeChild(document.getElementById('footer'));
  document.getElementById('photoDataAd').parentNode.removeChild(document.getElementById('photoDataAd'));
  document.getElementById('madison_mpu').parentNode.removeChild(document.getElementById('madison_mpu'));    
}

// EXTRA - shows download link under embedded videos on infamousx
if (window.location.host.match(/infamousx/i)!=null) 
{
  element = document.getElementsByTagName('embed')[0].getAttribute('flashvars');
  var a = document.createElement('a');
  a.setAttribute('style','font:normal normal normal 12px Arial,Helvetica,sans-serif;color:#777777;text-decoration:none;margin:0;padding:0;');
  a.href = element.substring(element.indexOf('http'),element.indexOf('autoPlay')-1);
  a.innerHTML = 'Download video'+'<br>';
  document.body.appendChild(a);  // add a link to the video on the bottom of the page  
  var b = document.createElement('a');
  b.setAttribute('style','font:normal normal normal 12px Arial,Helvetica,sans-serif;color:#777777;text-decoration:none;margin:0;padding:0;');
  b.href = 'http://www.youtube.com/watch?v=' + a.href.substring(a.href.indexOf('video_id=')+9);
  b.innerHTML = 'Watch video on Youtube'+'<br>';
  b.setAttribute('target','_new');
  document.body.appendChild(b);  // add a link to the video on the bottom of the page   
  document.getElementById('t2l-button').setAttribute('style','display:none;');   
}