User:Datumizer/importWiki.js

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.
/**
 * This user script automatically adds the value "English Wikipedia" (Q328) as a default value whenever
 * you add a snak with the property "imported from" (P143).
 * See also [[User:TMg/currentDate.js]].
 */
( function ( $ ) {
	$( '.wikibase-statementgrouplistview' ).on(
		'valueviewafterstartediting',
		function( event ) {
			// Separate multiple property IDs with pipe characters.
			var currentDateProperties = 'P143';

			var $valueview = $( event.target ),
				valueview = $valueview.data( 'valueview' ),
				expert = valueview.expert();

			if ( expert.$input.val()
			) {
				return;
			}

			var $snakview = $valueview.closest( '.wikibase-snakview' ),
				snakview = $snakview.data( 'snakview' ),
				regex = new RegExp( '^(' + currentDateProperties
					.replace( /[^P\d]+/gi, '|' )
					.replace( /^\||\|$/gi, '' ) + ')$', 'i' );

			if ( !regex.test( snakview.value().property ) ) {
				return;
			}

			var input = expert.$input[0];

			expert.$input.val( 'English Wikipedia' );
			// FIXME: This accesses a protected property.
			expert._viewNotifier.notify( 'change' );
			input.focus();
			input.select();
		} );
} )( jQuery );