// ==UserScript==
// @name MarkDown stealer
// @namespace HTLAL
// @include http://how-to-learn-any-language.com/forum/*
// @exclude http://how-to-learn-any-language.com/forum/login_user.asp*
// @version 1
// @grant none
// ==/UserScript==
// basic markdown reg expressions
var mdpat = [/\*\*(.*?)\*\*/g,
/\*(.*?)\*/g,
/_(.*?)_/g,
/\[(.*?)\]\((.*?)\)/g,
/\[([http|ftp].*?)\]/g];
// replacements for the preview window
var mdrep = ['<strong>$1</strong>',
'<em>$1</em>',
'<u>$1</u>',
'<a href="$2" rel="nofollow">$1</a>',
'<a href="$1" rel="nofollow">$1</a>'];
// to convert the markdown to BBCode
var bbcode = ['[B]$1[/B]',
'[I]$1[/I]',
'[U]$1[/U]',
'[URL=$2]$1[/URL]',
'[URL]$1[/URL]'];
function convertMarkDown(e) {
// e.preventDefault();
var textareas = document.getElementsByTagName('textarea'); // grab the textarea element
if (textareas) {
var textarea = textareas[0];
text = textarea.value;
// text = text.replace(/(?:\r\n|\r|\n)/g, '<br />'); // replace all new lines for BR
for(i=0;i<mdpat.length;i++) {
text = text.replace(mdpat[i],bbcode[i]);
}
text = text.replace(/[/g,"[");
textarea.value = text;
}
}
// create the preview button
var formp = document.querySelectorAll("form p");
if (formp) {
form = formp[1];
submit = form.querySelector("input[type=submit]");
var button = document.createElement("button");
button.textContent = "Convert MD to BBCode";
button.setAttribute("type", "button");
button.addEventListener('click', convertMarkDown, false);
form.appendChild(button);
document.getElementsByTagName("form")[0].addEventListener("submit",convertMarkDown,false);
}