Content deleted Content added
+ urlDecoderCustom call for user-defined URL processing, e.g. conversion to template |
now button file:Link_go.png in both old and new toolbar; rm ajax request for namespaces cause vars are available for a long time now |
||
Line 1:
function urlDecoderButton(){
addFuncButton2('urlDecoder', urlDecoderRun, 'http://upload.wikimedia.org/wikipedia/commons/9/91/Link_go.png', 'Decode URL before cursor or all URLs in selected text', window.urlDecoderKey)
}
if (wgAction=='edit' || wgAction=='submit') addOnloadHook(urlDecoderButton)
function addFuncButton2(id, func, img, title, akey){
if (window.wgWikiEditorEnabledModules && wgWikiEditorEnabledModules.toolbar){ //new
var msg = {}; msg[id] = title; mw.usability.addMessages(msg)
// $j('#wpTextbox1').wikiEditor('addToToolbar', { section:'main', groups: {'ruwp':{}}})
$j('#wpTextbox1').wikiEditor('addToToolbar', {
section:'main', //groups: {'mytools':{}},
group:'insert', tools: {id:{
type:'button',
action: {type:'callback', execute: func},
labelMsg:id,
icon:img
}}})
}else{ //old
var tlb_ = document.getElementById('toolbar')
if (!tlb_) return
var i=document.createElement('img')
i.className='mw-toolbar-custombutton'; i.id=id
i.onclick=func; i.src=img; i.title=title; i.alt=title.substr(0,3)
appendCSS('img.mw-toolbar-custombutton {height:20px; background-color:#bce; border:1px outset #bce; margin:0 1px; cursor:pointer}')
tlb_.appendChild(i)
if (akey){ i.accessKey = akey; i.title += ' ['+akey+']'; updateTooltipAccessKeys([i]) }
}
}
Line 88 ⟶ 100:
txt = txt.replace(RegExp('(\\[{0,2})' + httpRegExp + '([^\\]\\[\\n\\r]*?\\]\\]?)?', 'ig'),
simplifyMatched)
if (window.urlDecoderIntLinks)
if (window.wgFormattedNamespaces) ut = wgFormattedNamespaces[3].replace(/ /g,'_')
ut = RegExp ('\\[\\[' + ut.toLowerCase() + ':[^#]+$', 'i')
txt = txt.replace(/\[\[[^\]\|\n]+/g, function(lnk){
return ut.test(lnk) ? lnk : decodeAnchor(lnk)
})
}
return txt
}
Line 97 ⟶ 113:
function simplifyMatched(str, bracket, url, rest){//arguments: (whole string), '[', url, ' name]'; calls decodeUrl
if (!bracket){//no brackets, just url
var trail = url.match(RegExp('[,;\\\\\.:!\\?' //trailing punctuation, per Parser.php
+ (!/\(/.test(url) ? '\\)' : '') + ']+$' )) //trailing no-matching )
if (trail) url = url.substring(0, url.length-trail[0].length) //move these out of url
return decodeUrl(url) + str.substring(url.length)
}else if (rest) //both brackets and possibly name
Line 118 ⟶ 131:
for (var n in window.urlDecoderEngNames) //to eng keywords
url = url.replace(RegExp('(title=|wiki\/)('+urlDecoderEngNames[n]+':)'), '$1' + n + ':')
var link
if (!/(\}\}|\|)$/.test(url)) link = toWikilink(url) //trailing | or }} can be a part of template, skip to be safe
if (!link && window.urlDecoderCustom
&& (link = urlDecoderCustom(url)) && /^(https?:\/\/|\{\{)/.test(link))
{url = link; link = null} //still external
if (link){
link = link.replace(/%(3f|26|22)/ig, decodeURIComponent) //decode ?&"
if ((wgNamespaceNumber==0 || wgNamespaceNumber==14)
&& isBeforeCursor)
link=link.replace(/^:/,'') //probably interwiki
return '[\[' + link + (name?'|'+name:'') + ']]'
}
if (isBeforeCursor || typeof name == 'string')
url = url.replace(/''/g,'%27%27')//techically '' means the end of URL, but more likely it's part of it
if (typeof name == 'string') return '[' + url + (name?' '+name:'') + ']' //empty name
else return url
}
Line 207 ⟶ 225:
function isColonNeeded(pg){
if (pg.indexOf(':')==-1) return false
else return RegExp('^('+getDecoderNS()+'|file|category) *:','i').test(pg)
}
function getDecoderNS(){
if (!window.urlDecoderNS) var urlDecoderNS = {}
urlDecoderNS.en = 'image'
var ns = urlDecoderNS[wgContentLanguage]
if (typeof ns
if (!window.wgNamespaceIds) {
alert('Warning: wgNamespaceIds not defined, old MediaWiki version?'); return ''
}
ns = ''
for (var name in wgNamespaceIds)
if (wgNamespaceIds[name]==6 || wgNamespaceIds[name]==14)
ns += '|' + name
ns = ns.substring(1)
urlDecoderNS[wgContentLanguage] = ns
return ns
}
|