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

From Yugipedia
Jump to: navigation, search
(Update: wait for the wiki editor to load before attaching the MutationObserver event.)
(Wait for document ready event.)
 
(One intermediate revision by the same user not shown)
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');
  
// Only for "Card Gallery" and "Set Card Galleries" namespaces:
+
// Get the card name on the galleries and place it on upload links:
if(![3004, 3024].includes(nsNumber)) {
+
(function getName(MutationObserver, encode, delay) {
return;
 
}
 
  
// Function to guess the card name
+
// Only for "Card Gallery" and "Set Card Galleries" namespaces:
// and append it to the empty galleries' links:
+
if(![3004, 3024].includes(nsNumber)) {
const guessCardName = function() {
+
return;
$('.gallerybox').each(function() {
+
}
const $this = $(this);
+
 
const cardName = (function(gallery) {
+
// Function to guess the card name
var brFlag = false;
+
// and append it to the empty galleries' links:
if(gallery === 'card') {
+
const guessCardName = function() {
// Card galleries:
+
$('.gallerybox').each(function() {
const $navBoxHeader = $('.navbox-title').children('div');
+
const $this = $(this);
return $navBoxHeader.text() + $navBoxHeader.find('a').attr('title').replace(/.+?( \(.*\))?/g, function(_, $1) {
+
const cardName = (function(gallery) {
return $1 || '';
+
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('&');
 
});
 
});
} else {
+
});
// Set galleries:
+
};
return $this.find('.gallerytext').find('p').contents().filter(function() {
+
 
const isBR = this.nodeName === 'BR';
+
// Execute guessing the card name:
brFlag = isBR ? !brFlag : brFlag;
+
guessCardName();
return brFlag && !isBR;
+
 
}).filter('a').first().text();
+
// Check if there were changes in the DOM
}
+
// to keep the links with the card name in there (when previewing edits, etc.):
})(nsNumber === 3004 ? 'card' : 'set');
+
if(MutationObserver && ['edit', 'submit'].includes(action)) {
  
// Append the name to the url, in the form of «_name=cardName»:
+
// Wait for the editor to load:
$this.find('.noFile').attr('href', function(_, href) {
+
mw.loader.using('jquery.wikiEditor').then(function() {
return [
 
href,
 
[
 
'_name',
 
window.encodeURIComponent(cardName)
 
].join('=')
 
].join('&');
 
});
 
});
 
};
 
  
// 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
 
// 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);
 
  
});
+
}
 +
 
 +
})(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;');
  
// 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);
 
})(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);