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

From Yugipedia
Jump to: navigation, search
(Fix populating description form at Special:Upload.)
(Wait for document ready event.)
 
Line 6: Line 6:
 
(function(window, $, mw, console) {
 
(function(window, $, mw, console) {
  
const action = mw.util.getParamValue('action');
+
$(function() {
const nsNumber = mw.config.get('wgNamespaceNumber');
+
const action = mw.util.getParamValue('action');
 +
const nsNumber = mw.config.get('wgNamespaceNumber');
  
// Get the card name on the galleries and place it on upload links:
+
// Get the card name on the galleries and place it on upload links:
(function getName(MutationObserver, encode, delay) {
+
(function getName(MutationObserver, encode, delay) {
  
// Only for "Card Gallery" and "Set Card Galleries" namespaces:
+
// Only for "Card Gallery" and "Set Card Galleries" namespaces:
if(![3004, 3024].includes(nsNumber)) {
+
if(![3004, 3024].includes(nsNumber)) {
return;
+
return;
}
+
}
  
// Function to guess the card name
+
// Function to guess the card name
// and append it to the empty galleries' links:
+
// and append it to the empty galleries' links:
const guessCardName = function() {
+
const guessCardName = function() {
$('.gallerybox').each(function() {
+
$('.gallerybox').each(function() {
const $this = $(this);
+
const $this = $(this);
const cardName = (function(gallery) {
+
const cardName = (function(gallery) {
var brFlag = false;
+
var brFlag = false;
if(gallery === 'card') {
+
if(gallery === 'card') {
// Card galleries:
+
// Card galleries:
const $navBoxHeader = $('.navbox-title').children('div');
+
const $navBoxHeader = $('.navbox-title').children('div');
return $navBoxHeader.text() + $navBoxHeader.find('a').attr('title').replace(/.+?( \(.*\))?/g, function(_, $1) {
+
return $navBoxHeader.text() + $navBoxHeader.find('a').attr('title').replace(/.+?( \(.*\))?/g, function(_, $1) {
return $1 || '';
+
return $1 || '';
});
+
});
} else {
+
} else {
// Set galleries:
+
// Set galleries:
return $this.find('.gallerytext').find('p').contents().filter(function() {
+
return $this.find('.gallerytext').find('p').contents().filter(function() {
const isBR = this.nodeName === 'BR';
+
const isBR = this.nodeName === 'BR';
brFlag = isBR ? !brFlag : brFlag;
+
brFlag = isBR ? !brFlag : brFlag;
return brFlag && !isBR;
+
return brFlag && !isBR;
}).filter('a').first().text();
+
}).filter('a').first().text();
}
+
}
})(nsNumber === 3004 ? 'card' : 'set');
+
})(nsNumber === 3004 ? 'card' : 'set');
  
// Append the name to the url, in the form of «_name=cardName»:
+
// Append the name to the url, in the form of «_name=cardName»:
$this.find('.noFile').attr('href', function(_, href) {
+
$this.find('.noFile').attr('href', function(_, href) {
return [
+
return [
href,
+
href,
[
+
[
'_name',
+
'_name',
encode(cardName)
+
encode(cardName)
].join('=')
+
].join('=')
].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() {
  
// Execute guessing the card name:
+
// (jQuery) Node to observe:
guessCardName();
+
const $targetNode = $('.wikiEditor-ui-view-preview').find('.wikiEditor-preview-contents');
  
// Check if there were changes in the DOM
+
// Options for the observer (which mutations to observe).
// to keep the links with the card name in there (when previewing edits, etc.):
+
// We only want to see when content is loaded from the backend
if(MutationObserver && ['edit', 'submit'].includes(action)) {
+
// and then appended.
 +
// Therefore, observe when new child nodes are placed or removed:
 +
const options = {
 +
childList: true
 +
};
  
// Wait for the editor to load:
+
// Create an observer instance and observe:
mw.loader.using('jquery.wikiEditor').then(function() {
+
new MutationObserver(function(mutationsList) {
 +
// No need to iterate over mutationList,
 +
// since we just want to execute the following function once.
 +
delay(function() {
 +
guessCardName();
 +
});
 +
}).observe($targetNode.get(0), options);
  
// (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
+
})(window.MutationObserver, window.encodeURIComponent, window.setTimeout);
// 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.
 
delay(function() {
 
guessCardName();
 
});
 
}).observe($targetNode[0], options);
 
  
});
+
// Populate Special:Upload descrption with the name from the url:
 +
(function populate(name) {
  
}
+
// Fill in the upload description box at Special:Upload:
 +
// <nowiki>
 +
$('#wpUploadDescription').val(
 +
name ? '{{OCG-TCG card image\n| name = ' + name + '\n}}' : null
 +
);
 +
// </nowiki>
  
})(window.MutationObserver, window.encodeURIComponent, window.setTimeout);
+
})(mw.util.getParamValue('_name'));
  
// Populate Special:Upload descrption with the name from the url:
+
});
(function populate(name) {
 
  
// Fill in the upload description box at Special:Upload:
+
console.log('%cUser JS last updated: 16:35, 24 December 2018 (UTC)', 'color: blue;');
$('#wpUploadDescription').val(
 
name ? '{{OCG-TCG card image\n| name = ' + name + '\n}}' : null
 
);
 
  
})(mw.util.getParamValue('_name'));
 
 
console.log('%cUser JS last updated: 14:29, 8 March 2018 (UTC)', 'color: blue;');
 
 
 
})(window, window.jQuery, window.mediaWiki, window.console);
 
})(window, window.jQuery, window.mediaWiki, window.console);

Latest revision as of 16:35, 24 December 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) {

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

		// Get the card name on the galleries and place it on upload links:
		(function getName(MutationObserver, encode, delay) {

			// 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',
								encode(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.
						delay(function() {
							guessCardName();
						});
					}).observe($targetNode.get(0), options);

				});

			}

		})(window.MutationObserver, window.encodeURIComponent, window.setTimeout);

		// Populate Special:Upload descrption with the name from the url:
		(function populate(name) {

			// Fill in the upload description box at Special:Upload:
			// <nowiki>
			$('#wpUploadDescription').val(
				name ? '{{OCG-TCG card image\n| name = ' + name + '\n}}' : null
			);
			// </nowiki>

		})(mw.util.getParamValue('_name'));

	});

	console.log('%cUser JS last updated: 16:35, 24 December 2018 (UTC)', 'color: blue;');

})(window, window.jQuery, window.mediaWiki, window.console);