BBCode Replacer
By Aquilax
—
Last update Apr 26, 2008
—
Installed
88 times.
// ==UserScript==
// @name BBCode Replacer
// @namespace http://userscripts.org/users/28612
//@version 0.01.00
// @description Replace BBCode with html tags
// @include *
// ==/UserScript==
replaceTextContent(/\[(\w*)=?(.*?)\]((?:.|\s)*?)\[\/\1\]/gmi,replaceBBCode,document.body);
function replaceTextContent(regexp,handler,node)
{
//GM_log("replacing: "+node.textContent);
var snapshots=document.evaluate("//text()",node,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
for(var num1=0;num1<snapshots.snapshotLength;num1++)
{
regexp.lastIndex=0;
var node1=snapshots.snapshotItem(num1);
var match1=regexp.exec(node1.textContent);
if (match1)
{
var node2=node1.parentNode;
var node3=node1.nextSibling;
var text1=RegExp.rightContext;
node2.removeChild(node1);
while (match1)
{
text1=RegExp.rightContext;
node2.insertBefore(document.createTextNode(RegExp.leftContext),node3);
var node1=handler(match1);
node2.insertBefore(node1,node3);
replaceTextContent(regexp,handler,node1);
regexp.lastIndex=0;
match1=regexp.exec(text1);
}
node2.insertBefore(document.createTextNode(text1),node3);
}
}
}
function replaceBBCode(match)
{
//GM_log("match: "+match);
switch(match[1].toLowerCase())
{
case "b":
case "i":
case "s":
case "u":
case "center":
return createElement(match[1],match[3]);
case "code":
return createElement("pre",match[3]);
case "color":
return createElement("span",match[3],{"style":"color:"+match[2]});
case "email":
return createElement("a",match[3],{"src":"mailto:"+getUrl(match)});
case "image":
case "img":
return createElement("img",null,{"src":getUrl(match),"alt":match[3]});
case "link":
case "url":
return createElement("a",match[3],{"src":getUrl(match)});
case "quote":
return createElement("blockquote",match[3]);
case "size":
return createElement("span",match[3],{"style":"font-size:"+match[2]});
case "spoiler":
var span1=createElement("span",null,{"title":"Click to hide/show Spoiler","onclick":"var s1=this.firstChild.style; var s2=this.firstChild.nextSibling.style; var t=s1.display; s1.display=s2.display; s2.display=t;"});
span1.appendChild(createElement("span","Spoiler"));
span1.appendChild(createElement("span",match[3],{"style":"display:none"}));
return span1;
}
return document.createTextNode(match[0]);
}
function createElement(tagName,textContent,attributes)
{
var element1=document.createElement(tagName);
element1.textContent=textContent;
for(var name1 in attributes) element1.setAttribute(name1,attributes[name1]);
return element1;
}
function getUrl(match) {return (match[2]?match[2]:match[3]).replace(/\s/g,"");}