MediaWiki:Gadget-Clipboard.js

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 15:40, 11 March 2022 by Paisley Park (talk | contribs)
Jump to navigation Jump to search

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/**
 * Code to manage the content of the clipboard, due to the constant
 * plagiarism by the FANDOM wiki editors.
 * Created by Ciencia Al Poder on wikidex.net
 **/
(function( $, document ) {
	var _notice = '\nThis content comes from JoJo\'s Bizarre Encyclopedia (https://jojowiki.com), and must be attributed to its authors, as specified in the license. \nJoJo\'s Bizarre Wiki (the FANDOM wiki) is prohibited from using it, for repeatedly copying without giving credit.',
	_init = function() {
		if ( mw.config.get( 'wgUserGroups', [] ).length < 1 ) {
			$( '#mw-content-text' ).on( 'cut copy', _copyevent );
			$( document.body ).on( 'paste', '[contenteditable],input,textarea', _pasteevent );
		}
	},
	_copyevent = function( e ) {
		var text, $target;
		$target = $( e.target );
		if ( $target.closest( '.ace_editor' ).length > 0 ) {
			return;
		} else if ( $target.is( 'input,textarea' ) ) {
			if ( e.type == 'cut' ) {
				return;
			}
			text = $target.val().substring( e.target.selectionStart, e.target.selectionEnd );
		} else {
			text = document.getSelection().toString();
		}
		if ( text.length < 40 ) {
			return;
		}
		text = text + _notice;
		( e.originalEvent.clipboardData || window.clipboardData ).setData( 'text/plain', text );
		e.preventDefault();
		console.log( 'Content to be copied modified. Report any problems to Vish.' );
	},
	_pasteevent = function( e ) {
		var paste, selection, input, startPos, endPos, text;
		paste = ( e.originalEvent.clipboardData || window.clipboardData ).getData( 'text/plain' );
		if ( paste.indexOf( _notice ) == -1 ) {
			return;
		}
		paste = paste.replace( _notice, '');
		if ( $( event.target ).is( '[contenteditable]' ) ) {
			selection = window.getSelection();
			if ( !selection.rangeCount ) {
				return false;
			}
			selection.deleteFromDocument();
			selection.getRangeAt( 0 ).insertNode( document.createTextNode( paste ) );
		} else {
			input = event.target;
			if ( input.selectionStart !== undefined ) {
				startPos = input.selectionStart;
				endPos = input.selectionEnd;
				text = $( input ).val();
				text = text.substring( 0, startPos ) + paste + text.substring( endPos );
				$( input ).val( text );
				endPos = startPos + paste.length;
				input.setSelectionRange( endPos, endPos );
			}
		}
		console.log( 'Content to paste modified. Report any problems to Vish.' );
		e.preventDefault();
	};
	
	$( _init );
})( jQuery, document );