Difference between revisions of "User:Erdvilla/common.js"

From Yugipedia
Jump to: navigation, search
(Make uploading card images easier.)
 
(Update: wait for the wiki editor to load before attaching the MutationObserver event.)
Line 8: Line 8:
 
const action = mw.util.getParamValue('action');
 
const action = mw.util.getParamValue('action');
 
const nsNumber = mw.config.get('wgNamespaceNumber');
 
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
 
// Function to guess the card name
Line 45: Line 50:
 
};
 
};
  
// Only for "Card Gallery" and "Set Card Galleries" namespaces:
+
// Execute guessing the card name:
if([3004, 3024].includes(nsNumber)) {
+
guessCardName();
// 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() {
  
// 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)) {
 
 
// (jQuery) Node to observe:
 
// (jQuery) Node to observe:
 
const $targetNode = $('.wikiEditor-ui-view-preview').find('.wikiEditor-preview-contents');
 
const $targetNode = $('.wikiEditor-ui-view-preview').find('.wikiEditor-preview-contents');
Line 72: Line 79:
 
});
 
});
 
}).observe($targetNode[0], options);
 
}).observe($targetNode[0], options);
}
+
 
 +
});
 +
 
 
}
 
}
  
Line 80: Line 89:
 
})(mw.util.getParamValue('_name')));
 
})(mw.util.getParamValue('_name')));
 
 
console.log('%cUser JS last updated: 19:38, 22 February 2018 (UTC).','color:blue;');
+
console.log('%cUser JS last updated: 15:33, 5 March 2018 (UTC)', 'color:blue;');
 
 
 
})(window, window.jQuery, window.mediaWiki, window.console);
 
})(window, window.jQuery, window.mediaWiki, window.console);

Revision as of 15:33, 5 March 2018

/**
 * 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);