function replaceSelection(replaceText) {
	var input = $(document.activeElement);
	if ((input.is('input') && input.attr('type') == 'text') || input.is('textarea')) {
		//input.val(input.val().substring(0, input.selectionStart) + replaceText + input.val().substring(input.selectionEnd));
		input.selection('replace', {text: replaceText});
	}
}
if(window.location.href.substring(0, 30) == "http://scratch.mit.edu/discuss") {
//$(".markItUpContainer .markItUpHeader > ul").append('<li class="markItUpSeparator">---------------</li><li class="markItUpButton markItUpDropMenu markItUpTextTools"><a href="#" title="TextTools - WIP">TextTools - WIP</a><ul><li class="markItUpButton"><a href="#" title="Rainbow" class="markItUpRainbow">Rainbow - BBCode</a></li><li class="markItUpButton"><a href="#" title="Flipped" class="markItUpFlip">Flipped</a></li><li class="markItUpButton"><a href="#" title="Reversed" class="markItUpReverse">Reversed</a></li></ul></li>');
//$(".markItUpTextTools > a").css("background-image", "url(" + chrome.extension.getURL("icon_16.png") + ")");
//$(".markItUpRainbow").css("background-image", "url(" + chrome.extension.getURL("icon_rainbow.png") + ")");
//$(".markItUpFlip").css("background-image", "url(" + chrome.extension.getURL("icon_flip.png") + ")");
//$(".markItUpReverse").css("background-image", "url(" + chrome.extension.getURL("icon_reverse.png") + ")");
//Uncomment above to add button to scratch forum post editor. WIP - will add more later
}
chrome.runtime.onMessage.addListener(
	function(request, sender, sendResponse) {
		switch (request.textTool) {
			case 'reverse':
				replaceSelection(reverse($(document.activeElement).selection()));
				break;
			case 'flip':
				replaceSelection(flip($(document.activeElement).selection()));
				break;
			case 'rainbowBBCode':
				replaceSelection(rainbowBBCode($(document.activeElement).selection()));
				break;
			case 'rainbowHTML':
				replaceSelection(rainbowHTML($(document.activeElement).selection()));
				break;
		}
	});

@@ -335,16 +335,364 @@
 );%0A%09%7D%0A%7D%0A
+function replaceScratch(replaceText) %7B%0A%09var input = $(document.forms%5B'post'%5D%5B'body'%5D);%0A%09if ((input.is('input') && input.attr('type') == 'text') %7C%7C input.is('textarea')) %7B%0A%09%09//input.val(input.val().substring(0, input.selectionStart) + replaceText + input.val().substring(input.selectionEnd));%0A%09%09input.selection('replace', %7Btext: replaceText%7D);%0A%09%7D%0A%7D%0A
 if(windo
@@ -931,33 +931,32 @@
 Tools%22%3E%3Ca href=%22
-#
 %22 title=%22TextToo
@@ -1016,33 +1016,32 @@
 utton%22%3E%3Ca href=%22
-#
 %22 title=%22Rainbow
@@ -1725,16 +1725,352 @@
 + %22)%22);%0A
+//$(%22.markItUpTextTools %3E a%22).click(function(e) %7B%0A//    e.preventDefault();%0A//%7D);%0A//$(%22.markItUpRainbow%22).click(function(e) %7B%0A//    e.preventDefault();%0A//    var textarea = document.forms%5B'post'%5D%5B'body'%5D;%0A//    replaceScratch(rainbowBBCode($(textarea).selection()));%0A//%7D);%0A//$(%22.markItUpFlip%22).click();%0A//$(%22.markItUpReverse%22).click();%0A
 //Uncomm

add programming when clicked - buggy and WIP