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