Add style buttons to replies on userscripts.org

By JoeSimmons Last update Aug 8, 2008 — Installed 96 times. Daily Installs: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0

There are 4 previous versions of this script.

// ==UserScript==
// @name           Add style buttons to replies on userscripts.org
// @namespace      http://www.Tenfold.co.nr
// @description    The buttons add the tags into the reply area around the selected text.
// @released    2008/08/07 1:54:00
// @frequency   12 hours
// @releaseURL  http://userscripts.org/scripts/review/31242?format=txt
// @scriptURL   http://userscripts.org/scripts/source/31242.user.js
// @include        http://userscripts.org/scripts/show/*
// @include        http://userscripts.org/forums/*/topics/*
// @author         Joesimmons
// @version        1.1
// @copyright     Tenfold
// ==/UserScript==

var i, reply, pre, code, blockquote;

reply = document.getElementById("reply");

function add_tag(opencode,closecode) {
    var n = document.getElementById("post_body");
    var b = n.selectionStart;
    var e = n.selectionEnd;
    var spre = n.value.slice(0,b);
    var s = n.value.slice(b,e);
    var spost = n.value.slice(e);
    n.value = spre+opencode+s+closecode+spost;
    n.setSelectionRange((spre+opencode).length,(spre+s+opencode).length);
    n.focus();
}

pre = document.createElement("button");
pre.addEventListener("click", function(){add_tag("<pre>","</pre>");}, false);
pre.appendChild(document.createTextNode("Pre"));

code = document.createElement("button");
code.addEventListener("click", function(){add_tag("<code>","</code>");}, false);
code.appendChild(document.createTextNode("Code"));

blockquote = document.createElement("button");
blockquote.addEventListener("click", function(){add_tag("<blockquote>","</blockquote>");}, false);
blockquote.appendChild(document.createTextNode("BlockQuote"));

reply.appendChild(pre);
reply.appendChild(code);
reply.appendChild(blockquote);