I've been getting annoyed with how many people don't wrap their code in the code tags on the forum, so I wrote a quick script to handle that.

This has not been tested anywhere but vbforums, and only works in Chrome and Firefox using Tampermonkey(Chrome) or Greasemonkey(Firefox).

What it does:

The code just adds a red box to the top left corner of your screen, so it is out of the way of everything on vbforums. When you click the red box, it will go green, which means the functions is available.

When the box is green, simply highlight the text you want to wrap in a code tag and it will format it properly. When done adding code tags, click the green box and it will go red.


*Name:  before.png
Views: 1280
Size:  175.2 KB

Name:  after.png
Views: 1047
Size:  186.0 KB

// ==UserScript==
// @name       Format Code sections
// @namespace  http://vbforums.com
// @version    0.1
// @description  Add code tags around selected text
// @match      http://www.vbforums.com/showthread.php?*
// @copyright  2012+, You
// @require       http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
// ==/UserScript==

var codeOriginals = {};
var enabled = false;

$('body').append('<div id="enableCodeSelection" style="background-color: red; width: 15px; height: 30px; z-index: 9999; position: fixed; top: 100px; left: 0px;"></div>');

		enabled = false;
		$(this).css("background-color", "red");
		enabled = true;
		$(this).css("background-color", "green");
	} //end if

$(window).mouseup(function (e){
        var selection = getSelectionHtml();
        var selectedText = "";
        var selectionParent;
        if (typeof window.getSelection != "undefined") {
            selectedText = $(selection).html();
            selectionParent = window.getSelection().anchorNode.parentElement;
        } //end if
        if(selectedText != ""){
            var parentText = $(selectionParent).html();
            var id = (new Date).getTime().toString();
            codeOriginals[id] = selectedText;
            var wrapperStart = '<div class="bbcode_container"><div class="bbcode_description">Code: (<a href="" id="' + id + 
                '" class="removeCodeStyle">Remove</a>)</div><pre class="bbcode_code" style="max-height: 372px;">';
            var wrapperEnd = '</pre></div>'
            var newText = wrapperStart + selectedText + wrapperEnd;
            //if you're inside a code element and trying to add another
            if(selectionParent.className.indexOf("postcontent") == -1){
                if(selectedText.indexOf(wrapperStart) == -1 && selectedText.indexOf(wrapperEnd) == -1){
                } //end if
            } //end if
            //don't let the user select over a code window.
            if(selectedText.indexOf(wrapperStart) > -1 && selectedText.indexOf(wrapperEnd) > -1){
            } //end if
            $(selectionParent).html(parentText.replace(selectedText, replaceBreaks(newText)));
            $('.removeCodeStyle').click(function (ev){
               delete codeOriginals[$(this).attr("id")];
        } //end if
    } //end if

function replaceBreaks(selectedText){
	selectedText = selectedText.replace(/<br>/g,"").replace(/<br \/>/g,"");

    return selectedText;
} //end function

function getSelectionHtml() {
    var html = "";
    if (typeof window.getSelection != "undefined") {
        var sel = window.getSelection();
        if (sel.rangeCount) {
            var container = document.createElement("div");
            for (var i = 0, len = sel.rangeCount; i < len; ++i) {
            html = container;
        } //end if
    } //end if
    return html;