User:Js/urldecoder.js: Difference between revisions

Content deleted Content added
fix the script
support for liquid threads, code by User:Helder.wiki
Line 1:
/*jshint strict: false, asi: true, eqeqeq: false, curly: false, laxbreak:true, forin:false, boss:true, funcscope:true, noempty: false */
if( mw.config.get( 'wgAction' ) =='edit' || mw.config.get( 'wgAction' ) =='submit' ) $(function(){
/*global mw, $, urlDecoderCustom, urlDecoderPrefixes, urlDecoderEngNames */
var 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' ], 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' || mw.config.get(, 'wgActionsubmit' ] ) !=='submit' -1 ) $(function(){
addFuncButton22(
$(document).ready( function () {
'urlDecoder',
if ( mw.user.options.get('usebetatoolbar') ) {
urlDecoderRun,
mw.loader.using( 'ext.wikiEditor.toolbar', newToolbarBtnaddUrlDecoderButton );
'//upload.wikimedia.org/wikipedia/commons/9/91/Link_go.png',
} else {
'Decode URL before cursor or all URLs in selected text',
btn = oldToolbarBtn();
window.urlDecoderKey)
addUrlDecoderButton();
}
})
} );
 
/**/
 
 
function addFuncButton22(bId, bFunc, bIcon, bTitle, bKey){
 
mw.loader.using( 'user.options', function () {
if ( mw.user.options.get('usebetatoolbar') ) {
mw.loader.using( 'ext.wikiEditor.toolbar', newToolbarBtn );
} else {
oldToolbarBtn();
}
// Add the customizations to LiquidThreads' edit toolbar, if available
mw.hook( 'ext.lqt.textareaCreated' ).add( addUrlDecoderButton );
} );
function newToolbarBtn(bId, bFunc, bIcon, bTitle){
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 37 ⟶ 39:
icon: bIcon
}}})
 
}
 
function oldToolbarBtn(bId, bFunc, bIcon, bTitle, bKey){
 
var btn = $('<img class=mw-toolbar-custombutton id="' + bId + '">')
Line 49 ⟶ 50:
.appendTo('#toolbar')
if( bKey ){
mw.util.updateTooltipAccessKeys( btn[0] )
btn.attr({ accesskey: bKey, title: bTitle + ' ['+bKey+']' })
}
}
/**/
 
 
 
 
Line 88 ⟶ 85:
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)
 
Line 121 ⟶ 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 153 ⟶ 150:
var trail = RegExp(
'['
+ ',;\\\\\.:!\\?' //trailing punctuation, per Parser.php
+ ( /\(/.test(url) ? '' : '\\)' ) //also closing bracket without opening bracket
+ ']+$'
Line 207 ⟶ 204:
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 227 ⟶ 224:
 
//add bugzilla to user-defined prefixes
window.urlDecoderPrefixes = $.extend( window.urlDecoderPrefixes,
{ 'https://bugzilla.wikimedia.org/show_bug.cgi?id=' : 'mediazilla' } )