User:Ais523/sandbox.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Ais523/sandbox. |
// Sandboxing for a script request by [[User:Anas Salloum]] on [[WP:US/R]]
addOnloadHook(function()
{
var actionToTake='wpDiff'; // you can change to wpSave once you're happy this works properly
if(wgAction=="view"&&location.href.indexOf("/wiki/")!=-1)
{
if(wgNamespaceNumber==1) //talk page
{
addTab("javascript:arremovetag();","ar","ca-ar","remove {"+"{arabic}} tag","");
}
else if(wgNameSpaceNumber==0) //article
{
addTab("javascript:araddtag();","ar","ca-ar","add {"+"{lang-ar}} tag","");
}
}
if(location.href.indexOf("&arremovetag=yes")!=-1)
{
var tb1=document.getElementById('wpTextbox1');
tb1.value=tb1.value.split(/\{\{[aA]rabic\|[^}]*\}\}/).join("");
document.getElementById('wpSummary').value="Removed {"+"{[[Template:Arabic|Arabic]]}}";
document.getElementById(actionToTake).click();
}
else if(location.href.indexOf("&araddtag=")!=-1)
{
var x=decodeURIComponent(location.href.split("&araddtag=")[1]);
var tb1=document.getElementById('wpTextbox1');
var a=tb1.value.split("'''");
if(a.length<3)
{
alert("Couldn't figure out where to put the tag; try adding it manually.");
return;
}
a[2]="({"+"{lang-ar|'''"+x+"'''}})"+a[2];
tb1.value=a.join("'''");
document.getElementById('wpSummary').value="Added original [[Arabic alphabet|Arabic script]]";
document.getElementById('wpDiff').click(); //too dangerous to save without user intervention
}
});
function arremovetag()
{
location.href+="?action=edit&arremovetag=yes";
}
function araddtag()
{
var x=prompt("Please enter the parameter for the {"+"{lang-ar}} tag (it will be bolded "+
"automatically:");
if(x==null) return
location.href+="?action=edit&araddtag="+encodeURIComponent(x);
}
// Signature optimizing script
//<pre><nowiki>
//<![CDATA[>
/* AJAX string rendering script; based on the public-domain [[User:Alex Smotrov/qpreview.js]] */
/* Written in Continuation Passing Style, because it's asynchronous */
function soAJAXRenderWikimarkup(markup, callback){
var a = sajax_init_object();
a.open('POST', wgServer+wgScriptPath+'/index.php?title=Wikipedia:Sandbox&action=submit&live',
true);
var Boundary = '--------p1415';
a.setRequestHeader('Content-Type', 'multipart/form-data; boundary='+Boundary);
var PostData = '--' + Boundary
+ '\nContent-Disposition: form-data; name="wpTextbox1"\n\n'
+ markup + '\n--'+Boundary;
if (a.overrideMimeType) a.overrideMimeType('text/html');
a.send(PostData);
a.onreadystatechange = function(){
if (a.readyState != 4) return;
var html = a.responseText;
html = html.replace(/>/g,'>').replace(/</g,'<')
.replace(/"/g,'"').replace(/&/g,'&')
.replace(/'/g,"'").replace(/\n/g," ")
.replace(/\r/g," ").replace(/\>\ \ +\</g,"> <");
html = html.match(/\<\/strong>\ ?\<\/p\>\ ?\<\/div\>(.*)\<\/preview\>/)[1];
(callback)(html);
};
}
function htmlspecialchars(html)
{
return html.replace(/\&/g,'((((ampersand))))amp;').replace(/\>/g,'>')
.replace(/\</g,'<').replace(/\"/g,'"')
.replace(/\'/g,"'").replace(/\(\(\(\(ampersand\)\)\)\)/g,"&");
}
function soAToWikilink(html)
{
while(html.indexOf("<a ")!=-1)
{
var m=html.match
(/^(.*)\<a\ href\=\"(\/wiki\/|\/w\/index\.php\?title=)(.*?)(\&action\=edit)?\"[^\>]*?\>(.*?)\<\/a\>(.*)$/);
if(m[3].indexOf("Category:")==0||m[3].indexOf("Image:")==0) m[3]=":"+m[3];
if(m==null)
{ //external link needed; false-negatives here for unadorned edit links to existing pages,
//but that's unlikely to come up because comment links are more useful for Talk links in sigs
m=html.match(/^(.*)\<a\ href=\"(.*?)\"[^\>]*\>(.*?)\<\/a\>(.*)$/);
if(m!=null) html=m[1]+'<!--extstart-->['+m[2]+' '+m[3]+']<!--extend-->'+m[4]; else break;
// the comments here are recognized when making the signature compliant with [[WP:SIG]];
// they're removed later in the process
}
else html=m[1]+'[['+decodeURI(m[3])+'|'+m[5]+']]'+m[6];
}
return html;
}
var soAsyncComms;
function sigOptimize3(policised)
{
document.getElementById('wlrender').innerHTML=policised;
document.getElementById('wlsig').innerHTML=htmlspecialchars(soAsyncComms);
// The next step is to optimize the markup.
// To do this, the markup is split into individual properties, and then various combinations of
// them can be tested.
var props=new Array();
// Properties with their own tags (replace logical markup with physical as it's shorter)
props['tag-b']='b'; props['tag-strong']='b'; props['style-font-weight:bold']='b'; //bold
props['tag-cite']='cite'; // cite style
props['tag-code']='code'; // code style
props['tag-i']='i'; props['tag-em']='i'; props['tag-var']='i';
props['style-font-style:italic']='i'; //italic
props['tag-s']='s'; props['tag-del']='s'; props['tag-strike']='s';
props['style-text-decoration:line-through']='s'; //strike
props['tag-small']='small'; //small
props['tag-sub']='sub'; //subscript
props['tag-sup']='sup'; //superscript
props['tag-u']='u'; props['tag-ins']='u'; props['style-text-decoration:underline']='u'; //underline
}
function sigOptimize2(tidied)
{
document.getElementById('wikisigrender').innerHTML=tidied;
tidied=soAToWikilink(tidied);
document.getElementById('tidysig').innerHTML=htmlspecialchars(tidied);
// remove things against WP:SIG:
tidied=tidied.replace(/\<img [^\>]*\/\>/g,''); // Images (replace with nothing)
// Block level and other undesired elements with content (replace with spans)
var cb="big blockquote caption center dd div dl dt h1 h2 h3 h4 h5 h6 li ol p table td th tr ul"
.split(' ')
for(e in cb)
{
tidied=tidied.split('<'+cb[e]+' ').join('<span ')
.split('<'+cb[e]+'>').join('<span>')
.split('<'+cb[e]+'/>').join('<span/>') // hopefully this never comes up
.split('</'+cb[e]+'>').join('</span>')
}
// Other undesired elements without content (only <br> and <hr> at the moment)
tidied=tidied.replace(/\<br [^\>]*\/\>/g,'');
tidied=tidied.replace(/\<hr [^\>]*\/\>/g,'');
// External links:
// not http (the only whitelisted protocol starting with h AFAIK):
tidied=tidied.replace(/\<\!\-\-extstart\-\-\>\[[^h].*?<\!\-\-extend\-\-\>/g,'');
// not to en.wikipedia (a link back to the Main Page without the /wiki/ also isn't allowed, but
// who'd want that in a sig?)
tidied=tidied.replace(
/\<\!\-\-extstart\-\-\>\[http\:\/\/en\.([^w]|w[^i]|wi[^k]|wik[^i]|wiki[^p]|wikip[^e]|wikipe[^d]|wikiped[^i]|wikipedi[^a]|wikipedia[^\.]|wikipedia\.[^o]|wikipedia\.o[^r]|wikipedia\.or[^g]|wikipedia\.org[^\/]).*?<\!\-\-extend\-\-\>/g
,'');
// If a link pipes to nothing, it must have been an image, so remove it
tidied=tidied.replace(/\[\[.*?\|\]\]/g,'');
// Leading spaces
tidied=tidied.replace(/^\ +/,'');
soAsyncComms=tidied;
soAJAXRenderWikimarkup(tidied, sigOptimize3);
}
function sigOptimize1()
{
soAJAXRenderWikimarkup(document.getElementById('wikisig').value, sigOptimize2);
}
addOnloadHook(function()
{
if(wgPageName == "User:Ais523/sigoptimize")
{
var bodyContent=document.getElementById('bodyContent');
bodyContent.innerHTML="Wikimarkup you want your sig to be equivalent to (doesn't have to "+
"be valid signature markup; use ~~~ for your existing sig):<br />"+
"<input type='text' id='wikisig' value='~~~' style='width:80%' />"+
"<a href='javascript:sigOptimize1()'>Optimize</a><br />"+
"Your signature currently renders as:<br/>"+
"<div id='wikisigrender'><p><br/></p></div>"+
"Step 1: Tidying markup (correcting misnested tags, substing templates, "+
"etc.): "+
"<small>(extra <p>s and <br/>s may be added that will "+
"be removed later)</small><br/>"+
"<div><code id='tidysig'></code></div><p><br/></p>"+
"Step 2: Removing images, block-level markup, and other things not "+
"within the signature guidelines:<br/>"+
"<div><code id='wlsig'></code></div><div id='wlrender'><p><br/></p></div>";
}
});
//<]]>
//</nowiki></pre>