User:Js/urldecoder.js: Difference between revisions

Content deleted Content added
Js (talk | contribs)
+ urlDecoderCustom call for user-defined URL processing, e.g. conversion to template
Js (talk | contribs)
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:
//[[user:js/urldecoder]]
 
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)
var tlb = document.getElementById('toolbar')
if (!tlb) return
var bt = document.createElement('input')
bt.type = 'button'; bt.onclick = urlDecoderRun; bt.id = 'urlDecoder'
bt.value = '→[\[]]';
bt.title = 'Decode URL before cursor or all URLs in selected text'
bt.style.cssText = 'background:#adbede; height:22px; vertical-align:top; padding:0'
tlb.appendChild(bt)
if (!window.urlDecoderKey) return
bt.accessKey = urlDecoderKey
bt.title += ' ['+urlDecoderKey+']'
updateTooltipAccessKeys([bt])
}
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) txt = txt.replace(/\[\[[^\]\|\n]+/g, {
function(lnk){var ut = 'user_talk' //skip user_talk, usually in sig
if (window.wgFormattedNamespaces) ut = wgFormattedNamespaces[3].replace(/ /g,'_')
return /^\[\[user_talk:[^#]+$/i.test(lnk) ? lnk : decodeAnchor(lnk)
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
var pos = url.indexOf("''")
if (pos != -1) url = url.substring(0, pos) // double ' is not allowed inside urls
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
if (/(\}\}|\|)$/.test(url)) return str //trailing | or }} can be a part of template, skip to be safe
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 = toWikilink(url)
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)
link = link.replace(/%3f/ig,'?') //decode ?
&& isBeforeCursor)
if (wgNamespaceNumber==0 || wgNamespaceNumber==14) link=link.replace(/^:/,'') //interwiki?
link=link.replace(/^:/,'') //probably interwiki
return '[\[' + link + (name?'|'+name:'') + ']]'
}
}else if (typeof name == 'string') return '[' + url + (name?' '+name:'') + ']' //empty 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)
urlDecoderNS = window.urlDecoderNS || {}
}
 
function getDecoderNS(){
if (!window.urlDecoderNS) var urlDecoderNS = {}
urlDecoderNS.en = 'image'
var ns = urlDecoderNS[wgContentLanguage]
if (typeof ns !== 'string') return ns =//user-defined requestColonNS()list
if (!window.wgNamespaceIds) {
return RegExp('^('+ns+'|file|category) *:','i').test(pg)
alert('Warning: wgNamespaceIds not defined, old MediaWiki version?'); return ''
}
}
 
ns = ''
function requestColonNS(){
for (var name in wgNamespaceIds)
var whatsthis = ' &nbsp; &nbsp; <a href="http://en.wikipedia.org/wiki/user:js/urldecoder#Localization" target=_blank>(?)</a>'
if (wgNamespaceIds[name]==6 || wgNamespaceIds[name]==14)
showMsg('Requesting namespaces...'+whatsthis)
ns += '|' + name
var aj = sajax_init_object(), q = null, ns = []
ns = ns.substring(1)
aj.open('GET', '/w/api.php?format=json&action=query&meta=siteinfo&siprop=namespaces|namespacealiases', false)
aj.send(null)
try { eval('q='+aj.responseText); q = q.query
} catch(e){return null}
ns.push(q.namespaces[6]['*']); ns.push(q.namespaces[14]['*'])
for (var k in q.namespacealiases)
if (q.namespacealiases[k].id==6 || q.namespacealiases[k].id==14)
ns.push(q.namespacealiases[k]['*'])
ns = ns.join('|').toLowerCase()
urlDecoderNS[wgContentLanguage] = ns
showMsg("<code>urlDecoderNS = {'"+wgContentLanguage+"':'"+ns+"'}<code>"+whatsthis)
return ns
}
 
function showMsg(htm){
var dv = document.getElementById('edit-msg')
if (!dv){
dv = document.createElement('div')
dv.id = 'edit-msg'; dv.style.cssText = 'margin:5px; border:1px solid gray'
var tbox = document.getElementById('wpTextbox1')
tbox.parentNode.insertBefore(dv, tbox)
}
dv.innerHTML = htm; dv.style.display = ''
setTimeout(hideMsg, 15000)
}
 
function hideMsg(){
var dv = document.getElementById('edit-msg')
dv.style.display = 'none'
}