User:LeafGreen Ranger/common.js

From Yugipedia
< User:LeafGreen Ranger
Revision as of 19:28, 15 November 2018 by LeafGreen Ranger (talk | contribs) (Created page with "console.log('User:Dinoguy1000/common.js last updated: 11:51, 7 February 2018 (UTC)'); var dgServer = mw.config.get('wgServer'), dgScriptPath = mw.config.get('wgScriptPath'),...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.

console.log('User:Dinoguy1000/common.js last updated: 11:51, 7 February 2018 (UTC)');

var dgServer = mw.config.get('wgServer'),
	dgScriptPath = mw.config.get('wgScriptPath'),
	dgPageName = mw.config.get('wgPageName'),
	dgArticleId = mw.config.get('wgArticleId'),
	dgCanonicalNamespace = mw.config.get('wgCanonicalNamespace'),
	dgTitle = mw.config.get('wgTitle');
	dgLink = mw.util.addPortletLink;

/* begin stuff from [[User:Deltaneos/monobook.js]]
	(some of this would probably be better off moved to a gadget
	so people could more easily use it themselves, but eeeeeh)
*/

// CSS refresh and null edit tabs
$('#ca-purge').after('<li><a href="#" id="ca-css_refresh" title="Refresh the CSS on this page">css</a></li>');


$('#ca-css_refresh').click(function(ev)
{
	ev.preventDefault();
	var link = document.querySelector('link[href*="only=styles"]');
	if (link !== null)
	{
		link.href += '&ts='+Date.now();
	}
});

/* end copied from [[User:Deltaneos/monobook.js]] */

//importScript('User:Dinoguy1000/newmessageshistory.js');
if ( document.getElementsByClassName('usermessage')[0] ) {
	document.getElementsByClassName('usermessage')[0].innerHTML += ' (<a href="https://yugipedia.com/wiki/User_talk:Dinoguy1000?action=history">history</a>)';
}

// finish this up some time and add variants for semiprotection (and maybe cascade protection?)
//if ( mw.config.get("wgRestrictionEdit") == "sysop" ) { $('#ca-edit a').before(edit-fullprot-padlock) }
//if ( mw.config.get("wgRestrictionMove") == "sysop" ) { $('#ca-move a').before(move-fullprot-padlock) }

/* some mw.util.addPortletLink() documentation:

	function mw.util.addPortletLink(
		'HTML ID of the menu box to add the link to',
		'full URL of the link's target page[1]',
		'text to be displayed for the link',
		'HTML ID for the inserted link',
		'tooltip description to be displayed when hovering over the link',
		'keyboard shortcut for the link' );

	[1] This can be a literal URL, or JS variables such as wgServer can be used to enhance the code's portability.
		A little bit of cleverness can be used to generate dynamic URLs as well; for instance,
		"wgServer + '/index.php?title=Special:Log&page=' + wgPageName" will link to the current page's logs.
*/
$( function() {
	if( dgCanonicalNamespace != 'Special' ) {
		dgLink( 'p-tb', dgServer + '/index.php?title=Special:Log&page=' + dgPageName, 'Page logs', 'ca-viewlogs',
									'View the logs for this page, such as renames, deletions, and protections', '' );
	}

	dgLink( 'p-tb', dgServer + '/wiki/User:Dinoguy1000/boilerplate', 'Boilerplate', 'ca-boilerplate', 'Article boilerplate', '' );

	dgLink( 'p-random', dgServer + '/wiki/Special:RandomInCategory/All cards',			'Card (all)',	'ca-randomcardall',		'View a random card (from all cards)',	'' );
	dgLink( 'p-random', dgServer + '/wiki/Special:Random/Template',						'Template',		'ca-randomtemplate',	'View a random template',				'' );
	dgLink( 'p-random', dgServer + '/wiki/Special:Random/Module',						'Module',		'ca-randommodule',		'View a random module',					'' );
	dgLink( 'p-random', dgServer + '/wiki/Special:Random/Category',						'Category',		'ca-randomcategory',	'View a random category',				'' );
	dgLink( 'p-random', dgServer + '/wiki/Special:Random/File',							'File',			'ca-randomfile',		'View a random file',					'' );
	dgLink( 'p-random', dgServer + '/wiki/Special:Random/Property',						'Property',		'ca-randomproperty',	'View a random property',				'' );
	dgLink( 'p-random', dgServer + '/wiki/Special:Random/Forum',						'Forum',		'ca-randomforum',		'View a random forum',					'' );

	if( dgCanonicalNamespace == 'Category' ) {
		dgLink( 'p-random', dgServer + '/wiki/Special:RandomInCategory/' + dgTitle, 'Page in category', 'ca-randomincat',
									'View a random page or file in this category', '' );
	}
});

// <nowiki>
/**
 * Category pages mass null edit.
 * Stand-alone script.
 * @description:
 *     Null edits all pages in a category.
 * @author:
 *     [[User:Becasita]]
 */
(function(window, $, mw, log, delay) {
	"use strict";
	
	/**
	 * Global objects.
	 */
	const conf = mw.config.get([
		'wgNamespaceNumber',
		'wgPageName'
	]);
	conf.API = {
		init: new mw.Api(),
		exec: function(query, callbacks, method) {
			this.init[method || 'get'](query)
				.done(callbacks[0] || callbacks.done)
				.fail(callbacks[1] || callbacks.fail)
				.always(callbacks[2] || callbacks.always);
		}
	};

	const page = {
		$rightNavUl: $('#right-navigation').find('#p-views').find('ul')
	};
	
	/**
	 * Create button.
	 */
	const $massNullEditButton = $('<li>', {
		id: 'ca-massNullEdit',
		class: 'collapsible',
		html: $('<span>', {
			html: $('<a>', {
				href: '#',
				title: 'Null edit all pages in this category.',
				text: 'Mass null edit'
			})
		})
	});
	// Append:
	if(conf.wgNamespaceNumber === 14) {
		page.$rightNavUl.append($massNullEditButton);
	}
	
	/**
	 * Functions.
	 */
	function nullEdit(page, pagesLeft) {
		const query = {
			action: 'edit',
			title: page,
			token: mw.user.tokens.get('editToken'),
			summary: 'Something bad happened! Please leave me a message!',
			prependtext: ''
		};
		const callbacks = {
			done: function() {
				log('%cNull edit success on page «' + page + '»!', 'color: green;');
			},
			fail: function(message, errorObject) {
				log('%cNull edit error!', 'color: red;', '(message, errorObject)');
				log(message);
				log(errorObject);
			},
			always: function() {
				log('%cNull edit finished!', 'color: blue;');
				log('\tEdited:', page);
				log('%c--------------', 'color: blue;');
				if(!pagesLeft) {
					const styles = {
						'font-size': '1.2em',
						'color': 'white',
						'background-color': 'lightgreen',
						'border-radius': '1em',
					};
					$('#siteNotice').prepend(
						$('<div>', {
							class: 'massNullEditMessage',
							text: 'Done mass null editing!'
						}).css(styles)
					);
				}
			}
		};
		const method = 'post';
		conf.API.exec(query, callbacks, method);
	}

	function massNullEdit(pagesArray) {
		(function editLoopWithDelay(pagesArray) {
			delay(30000, function() {
				const page = pagesArray.shift();
				const pagesLeft = pagesArray.length;
				log('%c--------------', 'color: blue;');
				nullEdit(page, pagesLeft);
				if(pagesLeft) {
					editLoopWithDelay(pagesArray);
				}
			});
		})(pagesArray);
	}

	function init() {
		// Get pages.
		const query = {
			action: 'query',
			list: 'categorymembers',
			cmtitle: conf.wgPageName,
			cmlimit: 'max',
			cmprop: 'title',
			format: 'json'
		};
		const callbacks = {
			done: function(data) {
				massNullEdit(data.query.categorymembers.map(function(item) {
					return item.title;
				}));
			},
			fail: function(message, errorObject) {
				log('%Error getting pages!', 'color: red;', '(message, errorObject)');
				log(message);
				log(errorObject);
			},
			always: function() {
				log('Got pages!');
			}
		};
		conf.API.exec(query, callbacks);
	}

	/**
	 * Event.
	 */
	// Click:
	$massNullEditButton.click(function(event) {
		event.preventDefault();
		init();
	});

})(this, this.jQuery, this.mediaWiki, this.console.log, function(n, f) {
	this.setTimeout(f, n);
});