// ==UserScript==
// @name           Deframe
// @namespace      http://w-shadow.com/
// @description    Automatically removes frames and toolbars
// @copyright      2009+, Janis Elsts (http://w-shadow.com/)
// @version	       1.0.0
// @homepage       http://w-shadow.com/blog/2009/12/10/deframe-remove-frames-and-toolbars/
//
// @include        http://www.stumbleupon.com/su/*
// @include        http://stumbleupon.com/su/*
// @include        http://ow.ly/*
// ==/UserScript==

//------------------------------------------------------------------------------
// These settings control how the script selects the frame to open/redirect to.
//------------------------------------------------------------------------------

var min_width = 100;					//Minimum frame width, pixels. Set to zero to ignore frame width.
var min_height = 100;					//Minimum frame height, pixels. Set to zero to ignore frame height. 
var must_be_different_domain = true;	//Only consider frames pointing to other domains.

//------------------------------------------------------------------------------
// You don't need to change anything below this line.
//------------------------------------------------------------------------------

//Don't run the script when loaded inside a frame
if ( top == self ){

	var baseHost = location.host;

	//Collect normal frames and iframes in one array
	var frames = [];

	var tframes = document.getElementsByTagName('frame');
	for ( var i = tframes.length - 1; i >= 0; i-- ){
		frames.push(tframes[i]);
	}
	tframes = document.getElementsByTagName('iframe');
	for ( var i = tframes.length - 1; i >= 0; i-- ){
		frames.push(tframes[i]);
	}

	//Got frames? Find the one that's most likely to contain the real content.
	var len = frames.length;
	if ( len > 0 ){
	
		var best_candidate = { url : '', size : 0 };
		
		for ( var i = 0; i < len; i++ ){
			var frame = frames[i];
			
			//Skip invisible/invalid frames
			if ( (frame.style.display == 'none') || (frame.src == '') ) continue;
			
			//Skip frames from the same domain
			if ( must_be_different_domain && (getHostname(frame.src) == baseHost) ) continue;
			
			//Skip frames that are too small
			var width = frame.scrollWidth ? frame.scrollWidth : frame.offsetWidth;
			var height = frame.scrollHeight ? frame.scrollHeight : frame.offsetHeight;
			if ( ((min_width != 0) && (width < min_width)) || ((min_height != 0) && (height < min_height)) ) {
				continue;
			}
			
			//Compare against the current best candidate and select the biggest frame
			if ( (best_candidate.url == '') || ( width*height > best_candidate.size ) ){
				best_candidate.url = frame.src;
				best_candidate.size = width * height;
			}
			
		}
		
		//Redirect to the URL of the frame that's most likely to contain the real content
		if ( best_candidate.url != '' ){
			top.location.href = best_candidate.url;
		}
		
	}
	
}

//Get the hostname from an URL
function getHostname(str) {
	var re = new RegExp('^(?:f|ht)tp(?:s)?\://([^/]+)', 'im');
	if ( rez = str.match(re) ){
		return rez[1].toString();
	} else {
		return '';
	}
}