// ==UserScript==
// @name           Google Webmaster Tools: URL Removal Helper
// @namespace      http://whatz.eu/posts/google-webmaster-tools-url-removal-helper.html
// @description	Adds a Textarea to GWTs Removal Request Form so you can easily process URL Lists instead of adding one by one manually.
// @include        https://www.google.com/webmasters/tools/wm?security_token=*&action=create&siteUrl=*&hl=*&type=i&next=*
// ==/UserScript==

/* Reference:
Add / Remove Event Functions:
http://ejohn.org/projects/flexible-javascript-events/

Trim11 Function:
http://blog.stevenlevithan.com/archives/faster-trim-javascript
*/

function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}
function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
}


/* GUI */
var divOuter = document.createElement("div");
	divOuter.style.overflow = "auto";
	
	var divInner = document.createElement("div");
		divInner.style.backgroundColor = "#d5dff3";
		divInner.style.cssFloat = "left";
		divInner.style.padding = "5px";
			
			divInner.appendChild( document.createTextNode("URLs to remove:") );
			
			divInner.appendChild( document.createElement("br") );			
			
			var textarea = document.createElement("textarea");
			textarea.setAttribute("id", "GWT_Removal_Batch_Textarea")
			textarea.style.width = "500px";
			textarea.style.height = "150px";
			divInner.appendChild(textarea);
			
			divInner.appendChild( document.createElement("br") );			
			
			var button = document.createElement("input");
			button.setAttribute("type", "button");
			button.setAttribute("value", "Process URLs")
			button.setAttribute("id", "GWT_Removal_Batch_Button")
			button.style.cssFloat = "right";
			divInner.appendChild(button);
			
			var checkbox = document.createElement("input");
			checkbox.setAttribute("type", "checkbox");
			checkbox.setAttribute("id", "GWT_Removal_Batch_Trim");
			checkbox.checked = 1;
			divInner.appendChild(checkbox);
			
			var small = document.createElement("small");
				small.appendChild( document.createTextNode('trim "'+ unsafeWindow.getSiteUri() +'" from URLs') );
				small.style.cursor = "default";
			divInner.appendChild( small );
			
	divOuter.appendChild(divInner);
	
document.getElementById("url_entry").parentNode.insertBefore(divOuter, document.getElementById("url_entry") );


/* Processor / Events */
addEvent( document.getElementById('GWT_Removal_Batch_Button'), 'click', function ()  {
	function trim11 (str) {
		str = str.replace(/^\s+/, '');
		for (var i = str.length - 1; i >= 0; i--) {
			if (/\S/.test(str.charAt(i))) {
				str = str.substring(0, i + 1);
				break;
			}
		}
		return str;
	}
	
	var source = document.getElementById("GWT_Removal_Batch_Textarea");
	var urls = trim11(source.value).split("\n");
	
	var myReg = new RegExp('^' + unsafeWindow.getSiteUri() );
	
	var urlInput = document.getElementById("urlInput");
	
	var active_url;
	for ( i = 0; i < urls.length; i++ ) {
		if ( document.getElementById("GWT_Removal_Batch_Trim").checked ) {
			active_url = urls[i].replace(myReg, "");
			urlInput.value = active_url;
		} else {
			urlInput.value = urls[i];
		}
		unsafeWindow.handleAddClick();
	}
} );

addEvent( small, 'click', function ()  { 
	document.getElementById("GWT_Removal_Batch_Trim").checked = !document.getElementById("GWT_Removal_Batch_Trim").checked;
} );

