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