Jump to content

User:Perhelion/checkTitleExists.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/** [[File:Perhelion_checkTitleExists.js]]
* @Description: Virtual Text-Linker (Title-Check)
* Eng: Checks marked text, if this exists as an article.
* Deu: Prüft markierten Text, ob dieser als Artikel existiert.
* @Revision: 23:04, 21 February 2018 (UTC) (beta)
* @Authors: created 2018-02-03 [[:de:User:Perhelion]]
*	 	tested only on modern browsers
* 		required modules: mediawiki.api, mediawiki.util, mediawiki.Title
* <nowiki>
**/
/* global mediaWiki:false, jQuery:false*/
/* jshint bitwise:true, curly:false, eqeqeq:true, forin:false, laxbreak:true,
trailing:true, undef:true, unused:true, white:false, smarttabs:true */

( function ( $, mw ) {
'use strict';

var $content = $( '#mw-content-text' ),
	$el,
	AET = {
	selectedText: function selectedText() {
		var t;
		if ( window.getSelection )
			t = window.getSelection();
		else if ( document.getSelection )
			t = document.getSelection();
		else if ( document.selection )
			t = document.selection.createRange().text;
		return t;
	},

	mouseup: function ( e ) {
		var selText = '',
			sel = this.selectedText();
		e.stopPropagation();

		// if ( sel.type === 'Range' ) selText = sel.toString();

		if ( sel.rangeCount && sel.getRangeAt ) {
			$el = sel.getRangeAt( 0 );
			selText = $el.toString();
			if ( $el )
				sel.removeAllRanges(); // remove all selctions
			// sel.addRange( $el ); // re-add selection

		} else {
			selText = sel;
			$el = e.target;
		}

		selText = $.trim( selText );
		$el = $( $el.startContainer );
		if ( selText && $el[ 0 ] )
			this.chkExist( selText );
	// this.markSelection( selText, 1 );
	// return false;
	},

	chkExist: function ( target ) {
		var TiObj = new mw.Title.newFromText( target ),
			exist = false,
			AET = this,
			title = TiObj ? TiObj.getNameText() : null; // sanitize

		if ( !title ) return;

		// Check only once
		if ( mw.Title.exists( title ) !== null ) {
			return mw.notify( title + ' aborted, already checked', {
				type: 'warn'
			} );
		}

		mw.log( 'selected text', target, title );

		new mw.Api().get( {
			titles: title
		} ).done( function ( data ) {
			if ( data.query && data.query.pages && !data.query.pages[ -1 ] )
				exist = true;
			mw.Title.exist.set( target, exist );
			return AET.markSelection( target, exist ); // fire
		} ).fail( function ( XHR, status, err ) {
			mw.notify( XHR + ', ' + status + ', ' + err, {
				type: 'warn'
			} );
		} );
	},

	markSelection: function ( selText, exist ) {
		var textHTML = '<a href="' + mw.util.getUrl( selText ) +
			'" ' + ( exist ? '' : ' class="new"' ) +
			'" style="background:#ffa">' + selText + '</a>',
			orig = $el.text(),
			start = 0,
			end = 0;
		// pos = $el.scrollTop(),
		// Get replace position
		if ( orig ) {
			start = orig.indexOf( selText );
			end = start + selText.length;
			$el.replaceWith( orig.substr( 0, start ) + textHTML + orig.substr( end ) );
			// $el.scrollTop( pos );
		} // else no value or empty string
	}
};

function init() {
	mw.loader.using( [ 'mediawiki.api', 'mediawiki.util', 'mediawiki.Title' ], function () {
		if ( !$content[ 0 ] )
			$content = mw.util.$content;

		$( mw.util.addPortletLink( 'p-tb', '#', 'Virtual-Linker', 't-chkTiExists' ) )
			.find( 'a' )
			.css( 'background', '#ffa' )
			.on( 'click', function ( e ) {
				var t = this.textContent;
				e.preventDefault();
				if ( t.indexOf( 'Stop ' ) ) {
					$content.on( 'mouseup.chkTiExists', function ( e ) { AET.mouseup( e ); } );
					t = 'Stop ' + t;
					mw.notify( ( mw.config.get( 'wgUserLanguage' ) === 'de' ) ? 'Du kannst nun Text selektieren.' : 'You can now select text.' );
				} else {
					$content.off( 'mouseup.chkTiExists' );
					t = t.replace( 'Stop ', '' );
				}
				this.textContent = t;
				// return false;
			} );
	} );
}

if ( mw.config.get( 'wgPageContentModel' ) === 'wikitext' && !mw.config.get( 'wgNamespaceNumber' ) )
	return $( init );

}( jQuery, mediaWiki ) );
// EOF </nowiki>