Dalībnieks:Edgars2007/fixurl.js

Vikipēdijas lapa

Piezīme: Lai redzētu izmaiņas, pēc publicēšanas var nākties iztīrīt sava pārlūka kešatmiņu.

  • Firefox / Safari: Pieturi Shift un klikšķini uz Pārlādēt vai nospied Ctrl-F5 vai Ctrl-R (⌘-R uz Mac)
  • Google Chrome: Nospied Ctrl-Shift-R (⌘-Shift-R uz Mac)
  • Internet Explorer / Edge: Pieturi Ctrl un klikšķini uz Pārlādēt vai nospied Ctrl-F5
  • Opera: Nospied Ctrl-F5.
/* 
Kods URL saišu pielāgošanai MediaWiki prasībām (it īpaši lietojumam CS1 atsauču veidnēs)

Oriģinālavots: en:User:Mr. Stradivarius/chessboardfix.js
*/

var myContent = document.getElementsByName( 'wpTextbox1' )[0];

function replaceSelection( replace ) {
	// Replace currently selected text with the replace variable. The replace
	// variable can be a string or a callback function. The callback function
	// takes the selection text as its first and only argument, and must return
	// the string to replace the selection with.
	var len = myContent.value.length;
	var start = myContent.selectionStart;
	var end = myContent.selectionEnd;
	var sel = myContent.value.substring( start, end );

	if ( typeof( replace ) == 'function' ) {
		replace = replace( sel );
	}

	myContent.value = myContent.value.substring( 0, start )
		+ replace
		+ myContent.value.substring( end, len );
}

function urlFix() {
	replaceSelection( function ( sel ) {
		sel = sel.replace( / /g, '%20' );
		sel = sel.replace( /"/g, '%22' );
		sel = sel.replace( /'{3}/g, '%27%27%27' );
		sel = sel.replace( /'{2}/g, '%27%27' );
		sel = sel.replace( /</g, '%3c' );
		sel = sel.replace( />/g, '%3e' );
		sel = sel.replace( /\[/g, '%5b' );
		sel = sel.replace( /\]/g, '%5d' );
		sel = sel.replace( /{{!}}/g, '%7c' );
		sel = sel.replace( /{/g, '%7b' );
		sel = sel.replace( /\|/g, '%7c' );
		sel = sel.replace( /}/g, '%7d' );
		return sel;
	} );
}

function addUrlPortletLink() {
	var portletLink = mw.util.addPortletLink(
		'p-tb',
		'#',
		'Labot URL',
		't-urlfix'
	);
	$( portletLink ).click( function ( e ) {
		e.preventDefault();
		urlFix();
	});
}

if ( mw.config.get( 'wgNamespaceNumber' ) != -1 && myContent ) {
	jQuery( document ).ready( addUrlPortletLink );
}