File: MarkDown_stealer.user.js - Tab length: 1 2 4 8 - Lines: on off - No wrap: on off

// ==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 = ['&#91;B]$1&#91;/B]',
              '&#91;I]$1&#91;/I]',
              '&#91;U]$1&#91;/U]',
              '&#91;URL=$2]$1&#91;/URL]',
              '&#91;URL]$1&#91;/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(/&#91;/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);
}