// ==UserScript==
// @name MarkDown stealer
// @namespace HTLAL
// @include http://how-to-learn-any-language.com/forum/*
// @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]'];
previewPageHTML = ' <html>' +
' <head>' +
' <title>Post Preview</title>' +
' </head>' +
' <body style="background-image:url(http://how-to-learn-any-language.com/forum/forum_images/main_bg.gif)">' +
' <center>Post Preview</center>' +
' <div style="width:98%; border:1px solid; background-color:#F4F4FB; height:147; padding:2px;">';
previewPageHTML_bottom = ' </div>' +
' <center><a href="JavaScript:onClick=window.close()">Close Window</a></center>' +
'</body>' +
'</html>';
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;
}
}
function previewMarkDown() {
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],mdrep[i]);
}
preview = window.open('','Preview','toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1,width=680,height=400');
preview.document.body.innerHTML = previewPageHTML + text + previewPageHTML_bottom;
preview.document.body.style.backgroundImage="url(http://how-to-learn-any-language.com/forum/forum_images/main_bg.gif)";
}
}
inputs = document.querySelectorAll("form input[type=submit]");
for(i=0;i<inputs.length;i++) {
// inputs[i].removeAttribute("onclick");
}
// 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 = "Preview Markdown";
button.setAttribute("type", "button");
button.addEventListener('click', previewMarkDown, false);
form.appendChild(button);
document.getElementsByTagName("form")[0].addEventListener("submit",convertMarkDown,false);
}