Difference between revisions of "User:Erdvilla/common.js"
(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 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); | })(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);