User:Gifted9/common.js: Difference between revisions

The most up-to-date image and information guide on Webkinz Classic items, events, and more!
Content deleted Content added
Tag: Reverted
Restored revision 300763 by Gifted9 (Restorer)
Tags: Undo Reverted
Line 112: Line 112:
'.restorer-loading::after {white-space: pre; content: ""; animation: restorer-loading 0.5s infinite}'
'.restorer-loading::after {white-space: pre; content: ""; animation: restorer-loading 0.5s infinite}'
);
);
});

/**
* Mass Rename
* @description Rename pages quickly.
* @author Ozuzanna
*/
mw.loader.using('mediawiki.api', function () {
if (
window.MassRenameLoaded ||
!/sysop|wiki_guardian|content-moderator|bot|bot-global|staff|helper|wiki-representative|global-discussions-moderator|content-volunteer|wiki-specialist|soap/.test(mw.config.get('wgUserGroups').join())
) {
return;
}
window.MassRenameLoaded = true;
var i18n,
placement,
renameModal,
preloads = 3,
paused = false;
/**
* @method formHtml
* @description Creates the modal HTML
*/
function formHtml () {
return $('<form>', {
'class': 'WikiaForm'
}).append(
$('<fieldset>').append(
$('<p>', {
text: i18n.msg('instructions').plain()
}),
$('<p>', {
text: i18n.msg('instructions2').plain()
}),
$('<label>', {
'for': 'redirect-check',
text: i18n.msg('redirect').plain()
}).append(
$('<input>', {
type: 'checkbox',
id: 'redirect-check'
})
),
$('<textarea>', {
id: 'text-rename',
placeholder: 'old_name new_name'
}),
$('<div>', {
id: 'text-error-output',
text: i18n.msg('outputInitial').plain(),
append: '<br/>'
})
)
).prop('outerHTML');
}
/**
* @method preload
* @description Loads the hooks and I18n messages
*/
function preload () {
if (--preloads === 0) {
placement = window.dev.placement.loader;
window.dev.i18n.loadMessages('MassRename').then(init);
}
}
/**
* @method init
* @description Initiates the script
* @param {String} i18nData - Variable for I18n-js
*/
function init (i18nData) {
i18n = i18nData;
placement.script('MassRename');
$(placement.element('tools'))[placement.type('prepend')](
$('<li>').append(
$('<a>', {
id: 't-mr',
text: i18n.msg('title').plain(),
click: click
})
)
);
}
/**
* @method click
* @description Opens the MassRename modal
*/
function click () {
if (renameModal) {
renameModal.show();
return;
}
renameModal = new window.dev.modal.Modal({
content: formHtml(),
id: 'form-mass-rename',
size: 'medium',
title: i18n.msg('title').escape(),
buttons: [
{
id: 'mr1-start',
text: i18n.msg('initiate').escape(),
primary: true,
event: 'start'
},
{
id: 'mr1-pause',
text: i18n.msg('pause').escape(),
primary: true,
event: 'pause',
disabled: true
},
{
text: i18n.msg('close').escape(),
event: 'close'
}
],
events: {
pause: pause,
start: start
}
});
renameModal.create();
renameModal.show();
}
/**
* @method pause
* @description Pauses the operation
*/
function pause () {
paused = true;
document.getElementById('mr1-pause').setAttribute('disabled', '');
document.getElementById('mr1-start').removeAttribute('disabled');
}
/**
* @method start
* @description Starts the operation
*/
function start () {
paused = false;
document.getElementById('mr1-start').setAttribute('disabled', '');
document.getElementById('mr1-pause').removeAttribute('disabled');
process();
}
/**
* @method process
* @description Analyzes the inputted data
*/
function process () {
if (paused) {
return;
}
var txt = document.getElementById('text-rename'),
pages = txt.value.split('\n'),
page = pages[0];
if (!page) {
$('#text-error-output').append(
i18n.msg('finished').escape() +
' ' +
i18n.msg('nothingLeftToDo').escape() +
'<br/>'
);
pause();
} else {
rename(page);
}
pages = pages.slice(1, pages.length);
txt.value = pages.join('\n');
}
/**
* @method rename
* @description Renames the page
* @param {String} name - The rename data
*/
function rename (name) {
if (name.split(' ').length !== 2) {
$('#text-error-output').append(i18n.msg('invalidInput', name).escape() + '<br/>');
} else {
var oldName = name.split(' ')[0],
newName = name.split(' ')[1],
config = {
action: 'move',
from: oldName.replace('_', ' '),
to: newName.replace('_', ' '),
noredirect: '',
reason:
window.massRenameSummary ||
i18n.inContentLang().msg('summary').plain(),
bot: true,
token: mw.user.tokens.get('csrfToken')
};
if (document.getElementById('redirect-check').checked) {
delete config.noredirect;
}
new mw.Api().post(config)
.done(function (d) {
if (!d.error) {
console.log(i18n.msg('renameDone', oldName, newName).plain());
} else {
console.error(i18n.msg('renameFail', oldName, newName).escape() + ': ' + d.error.code);
$('#text-error-output').append(i18n.msg('renameFail', oldName, newName).escape() + ': ' + d.error.code + '<br/>');
}
})
.fail(function (error) {
console.error(i18n.msg('renameFail', oldName, newName).plain() + ': ' + error);
$('#text-error-output').append(i18n.msg('renameFail2', oldName, newName).escape() + '<br/>');
});
}
setTimeout(process, window.massRenameDelay || 1000);
}
mw.hook('dev.i18n').add(preload);
mw.hook('dev.modal').add(preload);
mw.hook('dev.placement').add(preload);
importArticles(
{
type: 'script',
articles: [
'u:dev:MediaWiki:I18n-js/code.js',
'u:dev:MediaWiki:Modal.js',
'u:dev:MediaWiki:Placement.js'
]
},
{
type: 'style',
articles: ['u:dev:MediaWiki:MassRename.css']
}
);
});
});

Revision as of 17:40, 24 March 2023

// javascript wiki browser: autowikibrowser but slightly uglier and in-browser, for when i'm not on my home computer
mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Joeytje50/JWB.js/load.js&action=raw&ctype=text/javascript');

// add somewhat sensitive stuff to my user tools dropdown so it's more accessible to me and less accessible to everyone reading my userpage
// documentation at [[mw:ResourceLoader/Core modules]] and https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.util-method-addPortletLink

    // First wait for mediawiki.util to load, and the page to be ready.
    mw.loader.using('mediawiki.util', function() {

        // General usage pattern: mw.util.addPortletLink( portletId, href, text /* Optional: , id, tooltip, accesskey, nextnode */ );

        // personal common.js
        mw.util.addPortletLink(
            'p-personal',
            mw.util.getUrl('User:Gifted9/common.js'),
            'User:Gifted9/common.js');

        // all subpages of my userpage
        mw.util.addPortletLink(
            'p-personal',
            mw.util.getUrl('Special:PrefixIndex/User:Gifted9'),
            'Special:PrefixIndex/User:Gifted9');

/**
 * adds history and delete links to Special:WhatLinksHere 
 * @source: https://www.mediawiki.org/wiki/Snippets/Special:WhatLinksHere_history_link
 * @rev: 2
 */
$( '#mw-whatlinkshere-list li' ).each( function() {
	var url = mw.config.get( 'wgScript' ) + '?title=' + encodeURIComponent( $( 'a:first', this ).text() ) + '&action=';
	$( '.mw-whatlinkshere-tools a:last', this )
		.after( $( '<a>' ).attr( 'href', url + 'delete' ).text( 'delete' ) ).after( ' | ' )
		.after( $( '<a>' ).attr( 'href', url + 'history' ).text( 'hist' ) ).after( ' | ' )
});
    });

/*** Restorer ***/

// Easily restore an older version of a page
// Documentation at [[w:User:BrandonXLF/Restorer]]
// By [[w:User:BrandonXLF]]

$(function() {
	if (mw.config.get('wgAction') != 'history') return;

	window.restorerSummary = window.restorerSummary ||
		'Restored revision $ID by [[Special:Contributions/$USER|$USER]] ([[w:User:BrandonXLF/Restorer|Restorer]])';

	function restore(revid) {
		var api = new mw.Api();

		return api.get({
			action: 'query',
			revids: revid,
			prop: 'revisions',
			rvprop: 'user',
			format: 'json',
			formatversion: '2'
		}).then(function(res) {
			var user = res.query.pages[0].revisions[0].user;

			return api.postWithEditToken({
				action: 'edit',
				pageid: mw.config.get('wgArticleId'),
				undo: mw.config.get('wgCurRevisionId'),
				undoafter: revid,
				summary: window.restorerSummary.replace(/\$ID/g, revid).replace(/\$USER/g, user)
			});
		}).then(
			function() {
				mw.notify('Restored revision successfully.');
				location.reload();
			},
			function(_, data) {
				mw.notify(api.getErrorMessage(data), {type: 'error'});
			}
		);
	}

	function addLink(item) {
		var revid = item.getAttribute('data-mw-revid');
		if (revid == mw.config.get('wgCurRevisionId')) return;

		var links = item.querySelector('.comment + .mw-changeslist-links');
		if (!links) return;

		var parent = document.createElement('span'),
			el = document.createElement('a');

		el.addEventListener('click', function() {
			el.className = 'restorer-loading';

			restore(revid).always(function() {
				el.className = '';
			});
		});

		el.textContent = 'restore';
		parent.appendChild(el);
		links.appendChild(parent);
	}

	var parents = document.querySelectorAll('li[data-mw-revid]');

	for (var i = 0; i < parents.length; i++) {
		addLink(parents[i]);
	}

	mw.loader.addStyleTag(
		'@keyframes restorer-loading {' +
		'0%, 100% {content: " ⡁"} 16% {content: " ⡈"} 33% {content: " ⠔"} 50% {content: " ⠒"} 66% {content: " ⠢"} 83% {content: " ⢁"}}' +
		'.restorer-loading::after {white-space: pre; content: ""; animation: restorer-loading 0.5s infinite}'
	);
});