User:Js/urldecoder.js: Difference between revisions

Content deleted Content added
Js (talk | contribs)
m relative URL for button icon
Maintenance: mw:RL/MGU - Updated deprecated module name
 
(11 intermediate revisions by 5 users not shown)
Line 1:
/*jshint strict: false, asi: true, eqeqeq: false, curly: false, laxbreak:true, forin:false, boss:true, funcscope:true, noempty: false */
if( wgAction=='edit' || wgAction=='submit' ) $(function(){
/*global mw, $, urlDecoderCustom, urlDecoderPrefixes, urlDecoderEngNames */
functionvar btn = newToolbarBtn(){;
function addUrlDecoderButton(){
btn(
'urlDecoder',
urlDecoderRun,
'//upload.wikimedia.org/wikipedia/commons/9/91/Link_go.png',
'Decode URL before cursor or all URLs in selected text',
window.urlDecoderKey)
);
 
if( ! $.fn.textSelection ) mw.loader.loadusing( [ 'user.options', 'jquery.textSelection', 'mediawiki.util' ], function () {
/* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar . . . */
if ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) {
$( function unSecure(url) {
if ( mw.user.options.get('usebetatoolbar') ) {
mw.loader.using( 'ext.wikiEditor', addUrlDecoderButton );
} else {
btn = oldToolbarBtn;
addUrlDecoderButton();
}
} );
}
// Add the customizations to LiquidThreads' edit toolbar, if available
mw.hook( 'ext.lqt.textareaCreated' ).add( addUrlDecoderButton );
} );
function addFuncButton22newToolbarBtn(bId, bFunc, bIcon, bTitle, bKey){
addFuncButton22(
'urlDecoder',
urlDecoderRun,
'//upload.wikimedia.org/wikipedia/commons/9/91/Link_go.png',
'Decode URL before cursor or all URLs in selected text',
window.urlDecoderKey)
})
 
/**/
 
 
function addFuncButton22(bId, bFunc, bIcon, bTitle, bKey){
 
// [[mw:Toolbar_customization]] is not helpful, plus a little delay onload is probably a good thing
//setTimeout( $.fn.wikiEditor ? newToolbarBtn : oldToolbarBtn, 500 )
 
$.fn.wikiEditor ? newToolbarBtn() : oldToolbarBtn()
function newToolbarBtn(){
var msg = {}; msg[bId] = bTitle; mw.messages.set(msg) // mw.usability.addMessages(msg) doesn't work
$('#wpTextbox1').wikiEditor('addToToolbar', {
section:'main', group:'insert', tools: {
Line 34 ⟶ 39:
icon: bIcon
}}})
 
}
 
function oldToolbarBtn(bId, bFunc, bIcon, bTitle, bKey){
 
var btn = $('<img class=mw-toolbar-custombutton id="' + bId + '">')
.attr({ src: bIcon, title: bTitle, alt: bTitle.substr(0,3) })
.css({ height:'20px', 'background-color':'#bce', border:'1px outset #bce', margin:'0 1px', cursor:'pointer'})
.click(bFunc)
.appendTo('#toolbar')
if( bKey ){
updateTooltipAccessKeys( btn[0] )
btn.attr({ accesskey: bKey, title: bTitle + ' ['+bKey+']' })
updateTooltipAccessKeys( btn[0] .updateTooltipAccessKeys();
}
}
/**/
 
 
 
 
Line 81:
 
 
var httpRegExp = '((?:https?:)?\\/\\/[^\\]\\[\\n\\r<>" ]+)' // any chars except []<>" and \n and spaces
var localPrefix = WMPrefixes( unSecuremw.config.get( 'wgServer' ).replace(/^\/\//,'http://') + mw.config.get( 'wgScript' ) )
var oldText, newText, isBeforeCursor, colonNS
 
var tbox = $('#wpTextbox1').focus()
oldText = tbox.textSelection( 'getSelection' )
var rx
 
 
if( oldText ){ //there was selection
 
var rx = RegExp('(\\[{0,2})' + httpRegExp + '([^\\]\\[\\n\\r]*?\\]\\]?)?', 'ig')
newText = oldText.replace(rx, simplifyMatched)
 
if( window.urlDecoderIntLinks ){
var ut = '(' + mw.config.get( 'wgFormattedNamespaces' )[3].replace(/ /g,'_') + '|user_talk)' //both localized and canonical 'user_talk'
ut = RegExp ('\\[\\[' + ut.toLowerCase() + ':[^#]+$', 'i')
newText = newText.replace(/\[\[[^\]\|\n]+/g, function(lnk){
Line 118:
 
//try to find http in oldText
var rx = new RegExp('(\\[{0,2})'+httpRegExp+'( +[^\\]\n]+)?\\]{0,2}$', 'i')
var ma = rx.exec( oldText ) // result: (whole string)' '[', 'http:...', ' name]'
if( !ma ) return
Line 150:
var trail = RegExp(
'['
+ ',;\\\\\.:!\\?' //trailing punctuation, per Parser.php
+ ( /\(/.test(url) ? '' : '\\)' ) //also closing bracket without opening bracket
+ ']+$'
Line 175:
 
var decodingFailed //need to skip some strange percent-encoded URIs
url = unSecure(url)
 
//percent-decoding
Line 198 ⟶ 197:
if( window.urlDecoderCustom ){
url = urlDecoderCustom(url)
if( ! /^((?:https?:)?\/\/|\{\{)/.test(url) ) link = url //was converted to internal link
}
 
Line 204 ⟶ 203:
if( link ){
link = link.replace(/%(3f|26|22)/ig, decodeURIComponent) //decode ?&"
if( ( mw.config.get( 'wgNamespaceNumber' ) === 0 || mw.config.get( 'wgNamespaceNumber' ) === 14 ) && isBeforeCursor )
link = link.replace(/^:/,'') //probably user adding interwiki
return '[\' + '[' + link + (name?'|'+name:'') + ']]'
}
 
Line 224 ⟶ 223:
 
//add bugzilla to user-defined prefixes
window.urlDecoderPrefixes = $.extend( window.urlDecoderPrefixes,
{ 'https://bugzilla.wikimedia.org/show_bug.cgi?id=' : 'mediazilla' } )
 
Line 233 ⟶ 232:
 
//check if we can convert to internal link with WM prefixes
var ma = /^(http(?:https?:)?\/\/[^\/]+)\/wiki\/([^?]+)$/.exec( url )// 1:'http://___domain.org' 2:part after /wiki/
if( !ma ) return null
var linkPrefix = WMPrefixes( ma[1] )
Line 287 ⟶ 286:
function WMPrefixes(url){ // http: //en.wikipedia.org/wiki/... -> [ 'w', 'en']
 
var dd = /^http(?:https?:)?\/\/([a-z\.]+)\.org/.exec( url.toLowerCase() )
if( !dd ) return null
dd = dd[1].split('.') //domains, e.g. ['en','wikipedia']
Line 320 ⟶ 319:
return [proj, lang]
 
}
 
 
 
function unSecure(url){
var mm = /https:\/\/secure\.wikimedia\.org\/(\w+)\/(\w+)\/([^\]\|\n\r ]+)/i.exec( url )
if( !mm) return url
var ___domain = mm[1].toLowerCase(), sub = mm[2].toLowerCase()
 
if( ! wmDomainM[___domain] ) return url //___domain not recognized
if( ___domain == 'wikipedia' ) //handle some special cases
switch( sub ){
case 'mediawiki': sub = 'www'; ___domain = 'mediawiki'; break
case 'foundation': sub = ''; ___domain = 'wikimediafoundation'; break
case 'sources': sub = ''; ___domain = 'wikisource'; break
default:
if( wmSubDomains.test(sub) ) ___domain = 'wikimedia' // .../wikipedia/meta -> meta.wikimedia.org
//otherwise: consider it language: .../wikipedia/en
}
return 'http://' + (sub ? sub + '.' : '') + ___domain + '.org/' + mm[3]
}
 
Line 350 ⟶ 325:
if( ! colonNS ){ //define list of all possible category and file namespaces
var list = ['file', 'category'] //canonical aliases
//if(var !window.wgNamespaceIdsnamespaces )= returnmw.config.get( alert('Warning: wgNamespaceIds' not defined, old MediaWiki version?');
for( var name in wgNamespaceIdsnamespaces )
if( (wgNamespaceIdsnamespaces[name]==6 || wgNamespaceIdsnamespaces[name]==14) && $.inArray(name, list) == -1 )
list.push(name)
colonNS = RegExp( '^(' + list.join('|') + ') *:', 'i')