Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ABBC Live Compiler for http://dis.4chan.org/
- // by Tablecat <http://tablecat.co.cc>
- var init = function() {
- var forms = document.forms;
- for (var i = 0; i < forms.length; i++) {
- if (forms[i].action === 'http://dis.4chan.org/derefer') continue;
- var preview = document.createElement('div');
- preview.className = 'preview';
- preview.style.display = 'none';
- forms[i].appendChild(preview);
- var textarea = forms[i].getElementsByTagName('textarea')[0];
- textarea.onchange = textarea.onkeyup = function() {
- var comment = this.value;
- var thread = this.parentNode.parentNode.parentNode.parentNode.parentNode.id.value;
- var script = window.location.href.replace(/^http:\/\/dis\.4chan\.org\/([^\.]+)/, '$1');
- comment = markup(clean_string(comment), thread, script)[0];
- this.parentNode.parentNode.parentNode.parentNode.parentNode.lastChild.style.display = comment ? 'inline-block' : 'none';
- this.parentNode.parentNode.parentNode.parentNode.parentNode.lastChild.innerHTML = comment;
- }
- }
- };
- var clean_string = function(str) {
- str = str.replace(/[\x00-\x08\x0B-\x1F\x7F]/g, "");
- str = str.replace(/^(\n|\x20)+|[\s]+$/g, "");
- str = str.replace(/&/g, "&");
- str = str.replace(/</g, "<");
- str = str.replace(/>/g, ">");
- str = str.replace(/\"/g, """);
- return str;
- };
- var markup = function(str, thread, script) {
- var tagList = [ "#" , "code" , "b" , "i" , "o" , "u" , "s" , "aa" , "m" , "spoiler" , "sup" , "sub" ];
- var malformed = 0;
- var quoteRe = />>([0-9\-\,l]+)/g;
- str = str.replace(quoteRe, '<a href="read/'+script+thread+'/$1">>>$1</a>');
- // super tags
- for (var i = 0; i < tagList.length; i++) {
- var tag = tagList[i],
- re = new RegExp("(^|[\\s\\S]*?)\\["+tag+"\\]([\\s\\S]*?)\\[\\/"+tag+"\\]($|[\\s\\S]*)", "g");
- while ( str.match(re) ) {
- if (tag === "#")
- str = str.replace(re, "$1"+escape_tag(RegExp.$2)+"$3");
- else if (tag === "code")
- str = str.replace(re, "$1<code>"+escape_tag(RegExp.$2)+"</code>$3");
- else
- break;
- }
- }
- // quote
- var pattern = new RegExp("(\\n|^)> (.*?)(\\n|$)", "g");
- while ( str.match(pattern) )
- str = str.replace(pattern, "$1<span class=\"quote\">$2</span>$3");
- // regular tags
- for (var i = 0; i < tagList.length; i++) {
- var tag = tagList[i],
- re = new RegExp("(^|[\\s\\S]*)\\["+tag+"\\]([\\s\\S]*?)\\[/"+tag+"\\]($|[\\s\\S]*)", "g");
- while ( str.match(re) ) {
- var before = str.replace(re, "$1"),
- content = str.replace(re, "$2"),
- after = str.replace(re, "$3");
- if (fail_check(content, tagList)) {
- str = before + "\x1FB"+tag+"\x1FC" + escape_tag(content) + "\x1FB/"+tag+"\x1FC" + after;
- malformed++;
- }
- else if (tag.match("^(b|i|s|u|sup|sub)$"))
- str = before + '<' + tag + '>' + content + '</' + tag + '>' + after;
- else if (tag === "m")
- str = before + '<tt>' + content + '</tt>' + after;
- else if (tag === "rem")
- str = before + after;
- else if (tag.match("^(spoiler|aa|o)$"))
- str = before + '<span class="' + tag + '">' + content + '</span>' + after;
- else
- break;
- }
- }
- str = str.replace(/\n/g, "<br>");
- str = '<blockquote style="border:1px dashed #000;padding:0.5em;background:#EFEFEF">'+str+'</blockquote>';
- //if (malformed > 0) str += '<div style="color:red"><b>Warning: Malformed markup, '+malformed+' error(s)</b></div>';
- return [unescape_tag(str), malformed];
- };
- var escape_tag = function(str) {
- str = str.replace(/>/g, "\x1FA");
- str = str.replace(/\[/g, "\x1FB");
- str = str.replace(/\]/g, "\x1FC");
- return str;
- };
- var unescape_tag = function(str) {
- str = str.replace(/\x1FA/g, ">");
- str = str.replace(/\x1FB/g, "[");
- str = str.replace(/\x1FC/g, "]");
- return str;
- };
- var fail_check = function(str, tagList) {
- for (var i = 0; i < tagList.length; i++) {
- var tag = tagList[i],
- reup = new RegExp("(.*)\\["+tag+"\\](.*)","g"),
- redwn = new RegExp("(.*)\\[/"+tag+"\\](.*)","g"),
- count = 0;
- while ( str.match(reup) ) {
- str = str.replace(reup, "$1$2");
- count++;
- }
- while ( str.match(redwn) ) {
- str = str.replace(redwn, "$1$2");
- count--;
- }
- if (count !== 0) return true;
- }
- return false;
- };
- if (window.location.href.search('^http://dis.4chan.org/[^\.]+') != -1) {
- window.onload = function() { init(); };
- }
Advertisement
Add Comment
Please, Sign In to add comment