https://www.betaarchive.com/wiki/index.php?title=User:Wwepro126/ajaxpreview.js&feed=atom&action=history
User:Wwepro126/ajaxpreview.js - Revision history
2024-03-28T17:19:54Z
Revision history for this page on the wiki
MediaWiki 1.39.3
https://www.betaarchive.com/wiki/index.php?title=User:Wwepro126/ajaxpreview.js&diff=20872&oldid=prev
Wwepro126 at 16:09, 2 May 2019
2019-05-02T16:09:30Z
<p></p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en-GB">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 16:09, 2 May 2019</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l1">Line 1:</td>
<td colspan="2" class="diff-lineno">Line 1:</td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>/* If you want to use this script, simply add the following line to your monobook.js:</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>/* If you want to use this script, simply add the following line to your monobook.js:</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> </div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> </div></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>importScript('User:<del style="font-weight: bold; text-decoration: none;">Anomie</del>/ajaxpreview.js'); // Linkback: [[User:<del style="font-weight: bold; text-decoration: none;">Anomie</del>/ajaxpreview.js]]</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>importScript('User:<ins style="font-weight: bold; text-decoration: none;">Wwepro126</ins>/ajaxpreview.js'); // Linkback: [[User:<ins style="font-weight: bold; text-decoration: none;">Wwepro126</ins>/ajaxpreview.js]]</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> </div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> </div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>* (Please keep the comment so I can see how many people use this).</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>* (Please keep the comment so I can see how many people use this).</div></td></tr>
</table>
Wwepro126
https://www.betaarchive.com/wiki/index.php?title=User:Wwepro126/ajaxpreview.js&diff=20871&oldid=prev
Wwepro126: Created page with "/* If you want to use this script, simply add the following line to your monobook.js: importScript('User:Anomie/ajaxpreview.js'); // Linkback: User:Anomie/ajaxpreview.js..."
2019-05-02T16:08:06Z
<p>Created page with "/* If you want to use this script, simply add the following line to your monobook.js: importScript('User:Anomie/ajaxpreview.js'); // Linkback: <a href="/wiki/index.php?title=User:Anomie/ajaxpreview.js&action=edit&redlink=1" class="new" title="User:Anomie/ajaxpreview.js (page does not exist)">User:Anomie/ajaxpreview.js</a>..."</p>
<p><b>New page</b></p><div>/* If you want to use this script, simply add the following line to your monobook.js:<br />
<br />
importScript('User:Anomie/ajaxpreview.js'); // Linkback: [[User:Anomie/ajaxpreview.js]]<br />
<br />
* (Please keep the comment so I can see how many people use this).<br />
*/<br />
<br />
var AJAXPreview={<br />
node:null,<br />
txt:null,<br />
timer:null,<br />
idx:0,<br />
<br />
spinner:function(){<br />
switch(AJAXPreview.idx++){<br />
case 0:<br />
AJAXPreview.node.innerHTML='<center style="font-size:50pt">|</center>';<br />
break;<br />
case 1:<br />
AJAXPreview.node.innerHTML='<center style="font-size:50pt">/</center>';<br />
break;<br />
case 2:<br />
AJAXPreview.node.innerHTML='<center style="font-size:50pt">–</center>';<br />
break;<br />
case 3:<br />
AJAXPreview.node.innerHTML='<center style="font-size:50pt">\\</center>';<br />
AJAXPreview.idx=0;<br />
break;<br />
}<br />
AJAXPreview.node.style.display='block';<br />
},<br />
<br />
callback:function(r){<br />
if(AJAXPreview.timer) window.clearInterval(AJAXPreview.timer);<br />
AJAXPreview.timer=null;<br />
if(!r.parse || !r.parse.text || !r.parse.text['*']){<br />
AJAXPreview.node.innerHTML='<div style="border:1px solid #f00;background-color:#fcc;color:#f00;text-align:center">Bad response</div>';<br />
throw new Error('Bad response');<br />
}<br />
AJAXPreview.node.innerHTML=r.parse.text['*']+'<br />';<br />
AJAXPreview.node.style.display='block';<br />
<br />
// Set a timeout to allow the browser a chance to parse the innerHTML<br />
window.setTimeout(function(){<br />
mw.hook( 'wikipage.content' ).fire( $( AJAXPreview.node ) );<br />
for(var i=AJAXPreview.$OnLoadHooks.length-1; i>=0; i--)<br />
AJAXPreview.$OnLoadHooks[i].call(window, AJAXPreview.node);<br />
}, 250);<br />
},<br />
<br />
doError:function(xhr,textStatus,errorThrown){<br />
if(AJAXPreview.timer) window.clearInterval(AJAXPreview.timer);<br />
AJAXPreview.timer=null;<br />
while(AJAXPreview.node.firstChild) AJAXPreview.node.removeChild(AJAXPreview.node.firstChild);<br />
var d=document.createElement('DIV');<br />
d.style.border='1px solid #f00';<br />
d.style.backgroundColor='#fcc';<br />
d.style.color='#f00';<br />
d.style.textAlign='center';<br />
d.appendChild(document.createTextNode('AJAX Error: '+textStatus+' '+errorThrown));<br />
AJAXPreview.node.appendChild(d);<br />
AJAXPreview.node.style.display='block';<br />
throw new Error('AJAX error: '+textStatus+' '+errorThrown);<br />
},<br />
<br />
doPreview:function(ev){<br />
if(!ev) ev=window.event;<br />
var txt='__NOEDITSECTION__\n'+AJAXPreview.getTextContent();<br />
var refs=AJAXPreview.getRefs(txt);<br />
var need=[];<br />
var groups={};<br />
for(var g in refs){<br />
groups[g]='';<br />
for(var n in refs[g]){<br />
if(refs[g][n].text===null) need.push([g,n]);<br />
}<br />
}<br />
var doPreview2=function(wikitext,sts,xhr){<br />
if(wikitext){<br />
refs=AJAXPreview.getRefs(wikitext);<br />
for(var i=need.length-1; i>=0; i--){<br />
var x=refs[need[i][0]][need[i][1]];<br />
if(!x) continue;<br />
if(x.type=='tag'){<br />
groups[need[i][0]]+='\x7b\x7b#tag:ref|'+x.text+'|name='+need[i][1]+'|group='+need[i][0]+'\x7d\x7d';<br />
} else {<br />
groups[need[i][0]]+='\x3cref name="'+need[i][1]+'" group="'+need[i][0]+'"\x3e'+x.text+'\x3c/ref\x3e';<br />
}<br />
}<br />
}<br />
txt+='\n\n\x7b\x7b-\x7d\x7d\n----\n';<br />
for(var g in groups){<br />
txt+='\n;'+(g?'Group '+g:'References')+'\n\x7b\x7breflist|2|group='+g+'|refs='+groups[g]+'\x7d\x7d';<br />
}<br />
<br />
jQuery.ajax({<br />
url:mw.util.wikiScript('api'),<br />
dataType:'json',<br />
type:'POST',<br />
data:{<br />
format:'json',<br />
action:'parse',<br />
pst:1,<br />
text:txt,<br />
title:mw.config.get('wgPageName'),<br />
prop:'text'<br />
},<br />
success:AJAXPreview.callback,<br />
error:AJAXPreview.doError<br />
});<br />
};<br />
mw.loader.using('mediawiki.util', function(){<br />
if(need.length>0){<br />
jQuery.ajax({<br />
url:mw.util.wikiScript('index'),<br />
dataType:'text',<br />
type:'GET',<br />
data:{ action:'raw', title:mw.config.get('wgPageName') },<br />
success:doPreview2,<br />
error:AJAXPreview.doError<br />
});<br />
} else {<br />
doPreview2(null,null,null);<br />
}<br />
});<br />
<br />
var x=document.getElementById('wikiDiff');<br />
if(x) x.parentNode.removeChild(x);<br />
if(AJAXPreview.timer) window.clearInterval(AJAXPreview.timer);<br />
AJAXPreview.timer=window.setInterval(AJAXPreview.spinner, 250);<br />
<br />
this.blur();<br />
window.scrollTo(0,0);<br />
if(ev){ // OOUI may not have an event here<br />
if(ev.preventDefault) ev.preventDefault();<br />
if(ev.stopPropagation) ev.stopPropagation();<br />
ev.returnValue=false;<br />
ev.cancelBubble=true;<br />
}<br />
return false;<br />
},<br />
<br />
getRefs:function(txt){<br />
var g;<br />
<br />
var refs={};<br />
<br />
// The new "list-defined references" have to be handled specially,<br />
// which means we have to manage to pull them out of the wikitext. Fun.<br />
// First, do the XML-style tags.<br />
txt=txt.replace(/<references((?:\s+[^>]*[^\/>])?)(?:\/>|>((?:.|[\r\n])*?)(<\/references>|$))/ig, function(x,p,t,c){<br />
p=p.replace(/\s+$/g,'');<br />
g=p.match(/\sgroup="([^\x22]*)"/i);<br />
if(!g) g=p.match(/\sgroup='([^\x27]*)'/i);<br />
if(!g) g=p.match(/\sgroup=(\S*)/i);<br />
g=g?g[1]:'';<br />
refs=AJAXPreview.getRefs2(t,g,refs);<br />
return '';<br />
});<br />
<br />
// Next, to reflist and #tag:references<br />
txt=AJAXPreview.process_templates(txt,function(n,p,o){<br />
var c=null, g='';<br />
if(n=='Reflist'){<br />
for(var j=0; j<p.length; j++){<br />
var m=p[j].match(/^\s*refs\s*=\s*((?:.|[\r\n])*?)\s*$/);<br />
if(m) c=m[1];<br />
var m=p[j].match(/^\s*group\s*=\s*([\x22\x27]?)([^\x22\x27]+?)\1\s*$/);<br />
if(m) g=m[2];<br />
}<br />
if(c===null) c='';<br />
} else if(/^#tag:\s*references$/i.test(n)){<br />
c=p.length ? p.shift() : '';<br />
for(var j=0; j<p.length; j++){<br />
var m=p[j].match(/^\s*group\s*=\s*([\x22\x27]?)([^\x22\x27]+?)\1\s*$/);<br />
if(m) g=m[2];<br />
}<br />
} else {<br />
return null;<br />
}<br />
refs=AJAXPreview.getRefs2(c,g,refs);<br />
return '';<br />
});<br />
<br />
return AJAXPreview.getRefs2(txt,'',refs);<br />
},<br />
getRefs2:function(txt,defgroup,refs){<br />
var g,n;<br />
<br />
// First, pull out regular <refs>. We can do this with a regex.<br />
txt.replace(/<ref((?:\s+\S+=(?:"[^\x22]*"|'[^\x27]*'|\S*?))*)\s*(?:\/>|>((?:.|[\r\n])*?)<\/ref>)/ig, function(x,p,t){<br />
g=p.match(/\sgroup="([^\x22]*)"/i);<br />
if(!g) g=p.match(/\sgroup='([^\x27]*)'/i);<br />
if(!g) g=p.match(/\sgroup=(\S*)/i);<br />
g=g?g[1]:defgroup;<br />
if(typeof(refs[g])=='undefined') refs[g]={};<br />
n=p.match(/\sname="([^\x22]*)"/i);<br />
if(!n) n=p.match(/\sname='([^\x27]*)'/i);<br />
if(!n) n=p.match(/\sname=(\S*)/i);<br />
if(!n) return null;<br />
n=n[1];<br />
if(typeof(refs[g][n])=='undefined') refs[g][n]={text:null,type:'?'};<br />
if(refs[g][n].text===null && typeof(t)!='undefined' && t!=='' && t!==null){<br />
refs[g][n].text=t;<br />
refs[g][n].type='ref'<br />
}<br />
return null;<br />
});<br />
<br />
// Second, if it looks like there are #tag refs, parse them too<br />
AJAXPreview.process_templates(txt,function(nm,p,o){<br />
if(!/^#tag:\s*ref$/i.test(nm)) return null;<br />
g=defgroup; n=null;<br />
for(var j=p.length-1; j>=1; j--){<br />
var m=p[j].match(/^\s*group\s*=\s*([\x22\x27]?)([^\x22\x27]+?)\1\s*$/);<br />
if(m) g=m[2];<br />
var m=p[j].match(/^\s*name\s*=\s*([\x22\x27]?)([^\x22\x27]+?)\1\s*$/);<br />
if(m) n=m[2];<br />
}<br />
if(typeof(refs[g])=='undefined') refs[g]={};<br />
if(n!==null){<br />
if(typeof(refs[g][n])=='undefined') refs[g][n]={text:null,type:'?'};<br />
if(refs[g][n].text===null && p[0]!==''){<br />
refs[g][n].text=p[0];<br />
refs[g][n].type='tag'<br />
}<br />
}<br />
return null;<br />
});<br />
<br />
return refs;<br />
},<br />
<br />
process_templates:function(txt,cb,data){<br />
var stack=[], i=0;<br />
while(i<txt.length){<br />
var x=stack.length?stack[stack.length-1]:null;<br />
var xb=null;<br />
for(var j=0; j<stack.length; j++){<br />
if(stack[j].char=='\x5b') xb=stack[j];<br />
}<br />
if(txt.substr(i,2)=='\x7b\x7b'){<br />
var ct;<br />
for(ct=2; txt.substr(i+ct,1)=='\x7b'; ct++);<br />
stack.push({char:'\x7b',start:i,count:ct,pstart:i+ct,params:[]});<br />
i+=ct;<br />
} else if(txt.substr(i,2)=='\x5b\x5b'){<br />
var ct;<br />
for(ct=2; txt.substr(i+ct,1)=='\x5b'; ct++);<br />
stack.push({char:'\x5b',start:i,count:ct,pstart:i+ct,params:[]});<br />
i+=ct;<br />
} else if(x && x.char=='\x7b' && txt.substr(i,2)=='\x7d\x7d'){<br />
var ct;<br />
for(ct=2; txt.substr(i+ct,1)=='\x7d'; ct++);<br />
if(ct>x.count) ct=x.count;<br />
i+=ct;<br />
x.params.push(txt.substring(x.pstart,i-ct));<br />
// First, parse out variables<br />
while(ct>=3){<br />
x.count-=3;<br />
ct-=3;<br />
var s=x.start-x.count;<br />
x.params=[txt.substring(s,i-x.count)];<br />
}<br />
// Any left is templates<br />
while(ct>=2){<br />
x.count-=2;<br />
ct-=2;<br />
var s=x.start+x.count;<br />
var orig=txt.substring(s,i-ct);<br />
var name=x.params.shift();<br />
var oname=name;<br />
name=name.replace(/_/g,' ');<br />
name=name.replace(/^\s+|\s+$/g,'');<br />
name=name.replace(/ +/g,' ');<br />
name=name.replace(/^Template\s*:\s*/ig,'');<br />
name=name.substr(0,1).toUpperCase()+name.substr(1);<br />
var ret=cb(name, x.params, orig, data, oname);<br />
if(ret===null){<br />
x.params=[orig];<br />
} else {<br />
ret=""+ret;<br />
var d=(ret=='' && (s==0 || txt.substr(s-1,1)=='\n') && txt.substr(i-ct,1)=='\n')?1:0;<br />
txt=txt.substr(0,s)+ret+txt.substr(i-ct+d);<br />
i=s+ret.length+ct;<br />
x.params=[ret];<br />
}<br />
}<br />
if(x.count<2){<br />
stack.pop();<br />
} else {<br />
// The one we just completed might not be the end of the<br />
// param, so reset the param array and pstart<br />
x.params=[];<br />
x.pstart=x.start+x.count;<br />
}<br />
} else if(xb && txt.substr(i,2)=='\x5d\x5d'){<br />
// Drop any pending templates, they're not really templates<br />
while(stack[stack.length-1]!=xb) stack.pop();<br />
var ct;<br />
for(ct=2; txt.substr(i+ct,1)=='\x5d'; ct++);<br />
if(ct>xb.count) ct=xb.count;<br />
i+=ct;<br />
xb.count-=ct;<br />
if(xb.count<2){<br />
stack.pop();<br />
} else {<br />
// The one we just completed might not be the end of the<br />
// param, so reset the param array and pstart<br />
xb.params=[];<br />
xb.pstart=xb.start+xb.count;<br />
}<br />
} else if(x && txt.substr(i,1)=='|'){<br />
x.params.push(txt.substring(x.pstart,i));<br />
x.pstart=++i;<br />
} else {<br />
i++;<br />
}<br />
}<br />
return txt;<br />
},<br />
<br />
onLoad:function(){<br />
var action=mw.config.get('wgAction');<br />
if(action!='edit' && action!='submit') return;<br />
<br />
var editForm=document.getElementById('editform');<br />
if(!editForm) return;<br />
var sectionField = editForm.elements['wpSection'];<br />
var isSection=(sectionField && sectionField.value!="");<br />
var p=editForm.elements["wpPreview"];<br />
if(!p) return;<br />
<br />
AJAXPreview.node=document.getElementById('wikiPreview');<br />
if(!AJAXPreview.node) return;<br />
AJAXPreview.txt=editForm.elements["wpTextbox1"];<br />
if(!AJAXPreview.txt) return;<br />
<br />
mw.loader.using( [ 'oojs-ui-core' ] ).done( function () {<br />
var b = new OO.ui.ButtonWidget( {<br />
label: 'Ajax Preview'+(isSection?' w/Refs':''),<br />
tabIndex: p.tabIndex<br />
} );<br />
b.on( 'click', AJAXPreview.doPreview );<br />
$( p ).before( b.$element, ' ' );<br />
} );<br />
p.value='Preview';<br />
<br />
// Hooks for standard functions<br />
if(typeof(window.createCollapseButtons) == 'function')<br />
AJAXPreview.AddOnLoadHook(createCollapseButtons);<br />
if(typeof(window.createNavigationBarToggleButton) == 'function')<br />
AJAXPreview.AddOnLoadHook(createNavigationBarToggleButton);<br />
},<br />
<br />
getTextContent:function(){<br />
return AJAXPreview.txt.value;<br />
},<br />
<br />
// Add callback functions here.<br />
AddOnLoadHook:function(f){<br />
AJAXPreview.$OnLoadHooks.push(f);<br />
},<br />
$OnLoadHooks:[]<br />
};<br />
<br />
$(document).ready(AJAXPreview.onLoad);</div>
Wwepro126