File: HTLAL_postbox.user.js - Tab length: 1 2 4 8 - Lines: on off - No wrap: on off

01: ?// ==UserScript==
02: // @name        HTLAL PostBox
03: // @description Makes writing posts easier
04: // @match       http://how-to-learn-any-language.com/forum/*
05: // @grant       none
06: // ==/UserScript==
07: 
08: var buttons = ["B","I","U","URL","IMG"];
09: var style = ["bold","italic","underline","url","image"];
10: 
11: for (var i = 0; i < buttons.length; i++) {
12:     regex = new RegExp("<a.*?JavaScript:Add.*?'"+buttons[i]+"'.*?>");
13:     document.body.innerHTML = document.body.innerHTML.replace(regex, '<a id="'+style[i]+'" href="JavaScript:addBBCode()">');
14: }
15: document.getElementById('bold').addEventListener('click', function () { addBBCode('[B]','[/B]'); }, false);
16: document.getElementById('italic').addEventListener('click', function () { addBBCode('[I]','[/I]'); }, false);
17: document.getElementById('underline').addEventListener('click', function () { addBBCode('[U]','[/U]'); }, false);
18: document.getElementById('url').addEventListener('click', function () { addBBCode('[URL]','[/URL]'); }, false);
19: document.getElementById('image').addEventListener('click', function () { addBBCode('[IMG]','[/IMG]'); }, false);
20: 
21: function addBBCode(open, close)
22: {
23:     var textareas = document.getElementsByTagName('textarea'); // grab the textarea element
24:     var textarea = textareas[0];
25:     textarea.focus();       // put cursor into textarea
26:     var start = textarea.selectionStart;   // find the start and end of the selection (if available) in textarea
27:     var end = textarea.selectionEnd;
28:     var length = textarea.textLength;   // we need these 3 values to split up the text
29:     var before = textarea.value.substring(0,start);
30:     var between = textarea.value.substring(start, end);
31:     var after = textarea.value.substring(end, length);
32:     textarea.value = before + open + between + close + after;
33:     var position = end + open.length + close.length;
34:     if(start-end == 0)
35:         position = start+open.length;
36:     textarea.setSelectionRange(position,position); // put cursor just after the last added tag
37:     return;
38: }