// ==UserScript==
// @name Block external
// @author TarquinWJ 
// @namespace http://www.howtocreate.co.uk/ 
// @version 1.0
// @description  Removes all scripts, images, iframes, objects, embeds,
//			and applets, that come from a different domain
//			to the page itself.
// @ujs:category site: enhancements
// @ujs:published 2005-05-30 23:08
// @ujs:modified 2005-09-19 09:19
// @ujs:documentation http://userjs.org/scripts/site/enhancements/block-external 
// @ujs:download http://userjs.org/scripts/download/site/enhancements/block-external.js
// ==/UserScript==


/* 
 * Please see
 * http://www.howtocreate.co.uk/operaStuff/userJavaScript.html#terms
 * for License and Terms of Use
 */

window.opera.addEventListener(
	'BeforeExternalScript',
	function (e) {
		var matchThis = new RegExp('^'+location.protocol+'\\\/\\\/'+location.hostname.replace(/\./g,'\\.')+'\\\/','i');
		if( !e.element.getAttribute('src').match(matchThis) ) {
			e.preventDefault();
		}
	},
	false
);
document.addEventListener(
	'load',
	function () {
		var matchThis = new RegExp('^'+location.protocol+'\\\/\\\/'+location.hostname.replace(/\./g,'\\.')+'\\\/','i');
		var x = 0;
		while( document.images[x] ) {
			if( !document.images[x].src.match(matchThis) ) {
				document.images[x].parentNode.removeChild(document.images[x]);
			} else { x++; }
		}
		var x = 0, y = document.getElementsByTagName('iframe');
		while( y[x] ) {
			if( !y[x].getAttribute('src').match(matchThis) ) {
				y[x].parentNode.removeChild(y[x]);
			} else { x++; }
		}
		var y = [document.getElementsByTagName('object'),document.getElementsByTagName('embed'),document.getElementsByTagName('applet')];
		for( var x = 0; y[x]; x++ ) { for( var z = 0; y[x][z]; z++ ) {
			var oSrc = y[x][z].getAttribute('data');
			if( !oSrc ) { oSrc = y[x][z].getAttribute('src'); }
			if( !oSrc ) { for( var g = 0, h; h = y[x][z].childNodes[g]; g++ ) {
				if( !h.tagName ) { continue; }
				var pName = h.getAttribute('name');
				if( ( pName == 'data' ) || ( pName == 'movie' ) || ( pName == 'src' ) ) {
					oSrc = h.getAttribute('value');
					if( oSrc ) { break; }
			} } }
			if( !oSrc ) { oSrc = y[x][z].getAttribute('code'); }
			if( oSrc && !oSrc.match(matchThis) ) {
				y[x][z].parentNode.removeChild(y[x][z]);
				z--;
		} } }
	},
	false
);