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: // @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 CSS = ' style="display: inline-block;'+
15:                   'background-color: white;'+
16:                   'border: 1px solid;'+
17:                   'color: black;'+
18:                   'height: 15px;'+
19:                   'padding: 3px;'+
20:                   'margin-left: 4px;'+
21:     'text-decoration:none;vertical-align:bottom;font-weight:bold;box-shadow: 1px 1px 1px #888888;"';
22: 
23: var buttons = ["B","I","U","URL","EMAIL","CENTER","LIST","INDENT","IMG","QUOTE","COLOR","FONT","CODE"];
24: var style = ["bold","italic","underline","url","email","center","list","indent","image","quote","color","font","code"];
25: 
26: var links = document.querySelectorAll("form a");
27: for(var i = buttons.length-1; i >= links.length; i--) {
28:     links[links.length-1].insertAdjacentHTML('afterend','<a'+CSS+'>'+buttons[i]+'</a>');
29: }
30: 
31: td = links[1].parentElement;
32: td.setAttribute("width","100%");
33: 
34: 
35: // update links now that we've added QUOTE
36: links = document.querySelectorAll("form a");
37: for(var i = 0; i < links.length; i++) {
38:     links[i].href = "JavaScript:addBBCode()";
39:     links[i].id = style[i];
40: }
41: 
42: for (var i = 0; i < buttons.length; i++) {
43:     var open = '[' + buttons[i] + ']';
44:     var close = '[/' + buttons[i] + ']';
45:     document.getElementById(style[i]).addEventListener('click', addBBCode_delegate(open,close), false); 
46: }
47: 
48: function addBBCode(open, close)
49: {
50:     var textareas = document.getElementsByTagName('textarea');  // grab the textarea element
51:     var textarea = textareas[0];
52:     textarea.focus();                           // put cursor into textarea
53:     var start = textarea.selectionStart;        // find the start and end of the selection (if available) in textarea
54:     var end = textarea.selectionEnd;
55:     var length = textarea.textLength;           // we need these 3 values to split up the text
56:     var before = textarea.value.substring(0,start);
57:     var between = textarea.value.substring(start, end);
58:     var after = textarea.value.substring(end, length);
59:     textarea.value = before + open + between + close + after;
60:     var position = end + open.length + close.length;
61:     if(start-end == 0)
62:         position = start+open.length;
63:     textarea.setSelectionRange(position,position);  // put cursor just after the last added tag
64:     return;
65: }