User:Gryllida/js/processExternalLinks.js

From Wikinews, the free news source you can write!
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes. Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac); IE: hold Ctrl while clicking Refresh, or press Ctrl-F5; Konqueror: simply click the Reload button, or press F5; Opera users may need to completely clear their cache in Tools→Preferences. — More skins

/*
Author : Svetlana Tkachenko svetlana@members.fsf.org
Licence: GPLv3+
Description: [beta] removes external links from article body
TODO: 
  [ ] 

* http://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar customization
  technical details of customizing the toolbar
* http://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar customization/Library
  snippets of code for common additions to the toolbar

*/ 

mw.loader.using(['mediawiki.api'], function () {
	"use strict";
	var getRidOfExternalLinks= function(){
		var text = $( '#wpTextbox1' ).val();
		alert(text);
		alert('1');
		var re = new RegExp('\[(http://[^\ ]*)\ (.*)\]', 'gi');
		alert('2');
		text = text.replace(re, 'Bla $1 $2 Foo');
		alert('3');
	};
	var customizeToolbar = function () {
		$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
			'section': 'main',
			'group': 'insert',
			'tools': {
				'getRidOfExternalLinks': {
					label: 'get Rid Of External Links',
					type: 'button',
					icon: '//upload.wikimedia.org/wikipedia/commons/thumb/f/f9/Remove_document_icon_%28the_Noun_Project_27892%29.svg/22px-Remove_document_icon_%28the_Noun_Project_27892%29.svg.png',
					'action': {
				        'type': 'callback',
						execute: function(context){
							getRidOfExternalLinks();
						}
					},
				}
			}
		});
	};
	/* 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 ) {
		mw.loader.using( 'user.options' ).then( function () {
			// This can be the string "0" if the user disabled the preference ([[phab:T54542#555387]])
			if ( mw.user.options.get( 'usebetatoolbar' ) == 1 ) {
				$.when(
					mw.loader.using( 'ext.wikiEditor' ), $.ready
				).then( customizeToolbar );
			}
		} );
	}
});