Last active
August 29, 2015 14:20
-
-
Save korden32/4bbc4e129e353f6a11ed to your computer and use it in GitHub Desktop.
SCUD BBCodes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name SCUD BBCodes | |
// @version 0.1.2 | |
// @author KorDen | |
// @grant none | |
// @include *//steamcommunity.com/discussions/forum/* | |
// @include *//steamcommunity.com/groups/*/discussions* | |
// @include *//steamcommunity.com/app/*discussions* | |
// @include *//steamcommunity.com/app/*/tradingforum* | |
// @include *//steamcommunity.com/workshop/discussions/* | |
// @include *//steamcommunity.com/*/filedetails/discussion* | |
// ==/UserScript== | |
// Based on code from http://nickolay.info/jscript/js_editor.html | |
contentEval( bbcode = new Array() ); | |
( function() { | |
bbtags = new Array( | |
'[b]','[/b]', | |
'[i]','[/i]', | |
'[u]','[/u]', | |
'[strike]','[/strike]', | |
'[spoiler]','[/spoiler]', | |
'[url=]','[/url]', | |
'[h1]','[/h1]', | |
'[noparse]','[/noparse]', | |
'[quote]','[/quote]', | |
'[code]','[/code]', | |
'[list]','[/list]', | |
'[olist]','[/olist]', | |
'[*]','',//26 | |
'[table]','[/table]', | |
'[tr]','[/tr]', | |
'[td]','[/td]', | |
'[th]','[/th]'); | |
var bbcodes; | |
var tId; | |
$J('.forumtopic_reply_textarea, .commentthread_textarea').each(function () { | |
tId = $J(this).attr('id'); | |
bbcodes = ''; | |
for (i=0; i < bbtags.length; i+=2) { | |
var tagname = bbtags[i].substring (1, bbtags[i].indexOf(']')); | |
bbcodes += '<a class="btn_darkblue_white_innerfade" style="margin:2px;padding:0 5px;line-height: 20px;" href="javascript:bbstyle(' + i + ', \'' + tId + '\')">' + tagname + '</a>'; | |
if(i == 14) | |
bbcodes+='<br>'; | |
else if(i == 18 || i == 24) | |
bbcodes+=' '; | |
} | |
$J(this).before('<div class="bb_bar" style="margin:4px 0px 8px;">' + bbcodes + '</div>'); | |
}); | |
})(); | |
contentEval ( function bbstyle(bbnumber, tId) { | |
bbtags = new Array( | |
'[b]','[/b]', | |
'[i]','[/i]', | |
'[u]','[/u]', | |
'[strike]','[/strike]', | |
'[spoiler]','[/spoiler]', | |
'[url=]','[/url]', | |
'[h1]','[/h1]', | |
'[noparse]','[/noparse]', | |
'[quote]','[/quote]', | |
'[code]','[/code]', | |
'[list]','[/list]', | |
'[olist]','[/olist]', | |
'[*]','',//26 | |
'[table]','[/table]', | |
'[tr]','[/tr]', | |
'[td]','[/td]', | |
'[th]','[/th]'); | |
var txtarea = $J('#' + tId)[0]; | |
txtarea.focus(); | |
if (bbnumber == -1) { //Close all tags | |
while (bbcode[0]) { | |
butnumber = arraypop(bbcode) - 1; | |
txtarea.value += bbtags[butnumber + 1]; | |
} | |
txtarea.focus(); | |
return; | |
} | |
if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0)) { | |
//Get selection | |
mWrap(txtarea, bbtags[bbnumber], bbtags[bbnumber+1]); | |
return; | |
} | |
donotinsert = false; | |
bblast = 0; | |
for (i = 0; i < bbcode.length; i++) { | |
if (bbcode[i] == bbnumber+1 && !not_closed_tags(bbnumber)) { | |
bblast = i; | |
donotinsert = true; | |
} | |
} | |
if (donotinsert) { | |
while (bbcode[bblast]) { | |
butnumber = arraypop(bbcode) - 1; | |
if (!not_closed_tags(butnumber)) | |
bbplace(bbtags[butnumber + 1], txtarea); | |
} | |
txtarea.focus(); | |
return; | |
} | |
else { //Open tag | |
bbplace(bbtags[bbnumber], txtarea); | |
arraypush(bbcode,bbnumber+1); | |
txtarea.focus(); | |
return; | |
} | |
//////////////////////////// | |
function bbplace(text, txtarea) { | |
var scrollTop = (typeof(txtarea.scrollTop) == 'number' ? txtarea.scrollTop : -1); | |
if (txtarea.createTextRange && txtarea.caretPos) { | |
var caretPos = txtarea.caretPos; | |
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text; | |
txtarea.focus(); | |
} | |
else if (txtarea.selectionStart || txtarea.selectionStart == '0') { | |
var startPos = txtarea.selectionStart; | |
var endPos = txtarea.selectionEnd; | |
txtarea.value = txtarea.value.substring(0, startPos) + text + txtarea.value.substring(endPos, txtarea.value.length); | |
txtarea.focus(); | |
txtarea.selectionStart = startPos + text.length; | |
txtarea.selectionEnd = startPos + text.length; | |
} | |
else { | |
txtarea.value += text; | |
txtarea.focus(); | |
} | |
if (scrollTop >= 0 ) | |
txtarea.scrollTop = scrollTop; | |
} | |
function mWrap(txtarea, open, close) { | |
if (txtarea.selectionEnd > txtarea.value.length) | |
txtarea.selectionEnd = txtarea.value.length; | |
var oldPos = txtarea.scrollTop; | |
var oldHght = txtarea.scrollHeight; | |
var selStart = txtarea.selectionStart; | |
var selEnd = txtarea.selectionEnd+open.length; | |
txtarea.value = txtarea.value.slice(0,selStart)+open+txtarea.value.slice(selStart); | |
txtarea.value = txtarea.value.slice(0,selEnd)+close+txtarea.value.slice(selEnd); | |
txtarea.selectionStart = selStart+open.length; | |
txtarea.selectionEnd = selEnd; | |
var newHght = txtarea.scrollHeight - oldHght; | |
txtarea.scrollTop += newHght; | |
txtarea.focus(); | |
} | |
function not_closed_tags(n) { | |
return (n==24 || n==54); | |
} | |
function getarraysize(thearray) { | |
for (i = 0; i < thearray.length; i++) { | |
if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null)) | |
return i; | |
} | |
return thearray.length; | |
} | |
function arraypush(thearray ,value) { | |
thearray[ getarraysize(thearray) ] = value; | |
} | |
function arraypop(thearray) { | |
asize = getarraysize(thearray) - 1; | |
retval = thearray[asize]; | |
delete thearray[asize]; | |
return retval; | |
} | |
} ); | |
function contentEval(source) { | |
var script = document.createElement('script'); | |
script.setAttribute("type", "application/javascript"); | |
script.textContent = source; | |
document.body.appendChild(script); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment