User:Erdvilla/common.js

From Yugipedia
< User:Erdvilla
Revision as of 15:33, 5 March 2018 by Becasita (talk | contribs) (Update: wait for the wiki editor to load before attaching the MutationObserver event.)
Jump to: navigation, search

Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: hold down Ctrl and click the Refresh or Reload button. Firefox: hold down ⇧ Shift while clicking Reload (or press Ctrl+⇧ Shift+R). Google Chrome and Safari users can just click the Reload button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.

/**
 * Make uploading card images easier
 * @description: Automatically fill the Special:Upload description box for card images.
 * @author [[Becasita]] (contact: [[User talk:Becasita]])
 */
(function(window, $, mw, console) {

	const action = mw.util.getParamValue('action');
	const nsNumber = mw.config.get('wgNamespaceNumber');

	// Only for "Card Gallery" and "Set Card Galleries" namespaces:
	if(![3004, 3024].includes(nsNumber)) {
		return;
	}

	// Function to guess the card name
	// and append it to the empty galleries' links:
	const guessCardName = function() {
		$('.gallerybox').each(function() {
			const $this = $(this);
			const cardName = (function(gallery) {
				var brFlag = false;
				if(gallery === 'card') {
					// Card galleries:
					const $navBoxHeader = $('.navbox-title').children('div');
					return $navBoxHeader.text() + $navBoxHeader.find('a').attr('title').replace(/.+?( \(.*\))?/g, function(_, $1) {
						return $1 || '';
					});
				} else {
					// Set galleries:
					return $this.find('.gallerytext').find('p').contents().filter(function() {
						const isBR = this.nodeName === 'BR';
						brFlag = isBR ? !brFlag : brFlag;
						return brFlag && !isBR;
					}).filter('a').first().text();
				}
			})(nsNumber === 3004 ? 'card' : 'set');

			// Append the name to the url, in the form of «_name=cardName»:
			$this.find('.noFile').attr('href', function(_, href) {
				return [
					href,
					[
						'_name',
						window.encodeURIComponent(cardName)
					].join('=')
				].join('&');
			});
		});
	};

	// Execute guessing the card name:
	guessCardName();

	// Check if there were changes in the DOM
	// to keep the links with the card name in there (when previewing edits, etc.):
	if(MutationObserver && ['edit', 'submit'].includes(action)) {

		// Wait for the editor to load:
		mw.loader.using('jquery.wikiEditor').then(function() {

			// (jQuery) Node to observe:
			const $targetNode = $('.wikiEditor-ui-view-preview').find('.wikiEditor-preview-contents');
	
			// Options for the observer (which mutations to observe).
			// We only want to see when content is loaded from the backend
			// and then appended.
			// Therefore, observe when new child nodes are placed or removed:
			const options = {
				childList: true
			};
	
			// Create an observer instance and observe:
			new MutationObserver(function(mutationsList) {
				// No need to iterate over mutationList,
				// since we just want to execute the following function once.
				window.setTimeout(function() {
					guessCardName();
				});
			}).observe($targetNode[0], options);

		});

	}

	// Fill in the upload description box at Special:Upload:
	$('#wpUploadDescription').val((function(name) {
		return name ? '{{OCG-TCG card image\n| name = ' + name + '\n}}' : null;
	})(mw.util.getParamValue('_name')));
	
	console.log('%cUser JS last updated: 15:33, 5 March 2018 (UTC)', 'color:blue;');
	
})(window, window.jQuery, window.mediaWiki, window.console);