Hoppa till innehållet

MediaWiki:Gadget-blockAPI.js

Från Wikipedia

OBS: Efter du har publicerat sidan kan du behöva tömma din webbläsares cache för att se ändringarna.

  • Firefox / Safari: Håll ned Skift och klicka på Uppdatera sidan eller tryck Ctrl-F5 eller Ctrl-R (⌘-R på Mac)
  • Google Chrome: Tryck Ctrl-Skift-R (⌘-Skift-R på Mac)
  • Edge Håll ned Ctrl och klicka på Uppdatera eller tryck Ctrl-F5.
  • Opera: Tryck Ctrl-F5.
$( function() {
	'use strict';
	var blocklinks = $( '.mw-usertoollinks a' ).filter( function( i, e ) {
		return $( e ).attr( 'title' ).startsWith( 'Special:Blockera/' );
	} );
	var blocktext = 'Block';
	var blocktitle = 'Block the user or IP address with the selected settings';
	var closetext = 'Close';
	var closetitle = 'Close the dialog without blocking';
	var blockingof = 'Blocking of ';
	var msgto = 'Message to ';
	var delivered = ' delivered';
	var failed = ' failed';
	var blocked = ' blocked';
	var userlabel = 'User';
	var userlabeltitle = 'The user or IP address to be blocked';
	var expirylabel = 'Expiration';
	var expirylabeltitle = 'Either relative like "2 days" or absolute like "May 3 2024"';
	var reasonlabel = 'Reason';
	var reasonlabeltitle = 'Reason for the block';
	var nocreatelabel = 'Prevent account creation';
	var noemaillabel = 'Prevent user from sending email';
	var allowusertalklabel = 'Prevent this user from editing their talk page';
	var autoblocklabel = 'Block IP addresses that the user tries to edit from';
	var watchuserlabel = 'Watch this user\'s user and talk pages';
	var anononlylabel = 'Prevent logged-in users from editing from this IP address';
	var msglabel = 'Send block message to the user\'s talk page';
	if ( mw.config.get( 'wgUserLanguage' ) === 'sv' ) {
		blocktext = 'Blockera';
		blocktitle = 'Blockera användaren eller IP-adressen med de valda inställningarna';
		closetext = 'Stäng';
		closetitle = 'Stäng dialogrutan utan att blockera';
		blockingof = 'Blockering av ';
		msgto = 'Meddelande till ';
		delivered = ' levererat';
		failed = ' misslyckades';
		blocked = ' blockerad';
		userlabel = 'Användare';
		userlabeltitle = 'Användaren eller IP-adressen som ska blockeras';
		expirylabel = 'Varaktighet';
		expirylabeltitle = 'Antingen relativt som "2 days" eller absolut som "May 3 2024"';
		reasonlabel = 'Anledning';
		reasonlabeltitle = 'Anledning till blockeringen';
		nocreatelabel = 'Förhindra skapande av användarkonton';
		noemaillabel = 'Hindra användaren från att skicka e-post';
		allowusertalklabel = 'Hindra användaren från att redigera sin diskussionssida';
		autoblocklabel = 'Blockera IP-adresser som användaren försöker redigera ifrån';
		watchuserlabel = 'Bevaka användarens användarsida och diskussionssida';
		anononlylabel = 'Förhindra inloggade användare från att redigera från den här IP-adressen';
		msglabel = 'Lägg in blockeringsmeddelande på användarens diskussionssida';
	}
	blocklinks.click( function( e ) {
		e.preventDefault();
		var isAnon;
		var userlinkscontainer = 'li';
		if ( $( '.diff' ).length ) {
			userlinkscontainer = 'div';
		}
		var user = $( e.currentTarget ).closest( userlinkscontainer ).find( '.mw-userlink' ).text();
		if ( $( e.currentTarget ).closest( 'li' ).find( '.mw-anonuserlink' ).length ) {
			isAnon = true;
		}
		var expirylist = [
			'30 minutes',
			'2 hours',
			'6 hours',
			'1 day',
			'3 days',
			'170 hours',
			'2 weeks',
			'1 month',
			'3 months',
			'6 months',
			'1 year',
			'3 years'
		];
		if ( !isAnon ) {
			expirylist.push( 'infinite' );
		}
		var expiryoptions = expirylist.map( function( v ) {
			return '<option value="' + v + '">';
		} );
		var expirystr = expiryoptions.join( '' );
		var reasonlist = [
			'Förolämpning',
			'Förvillarkonto',
			'Marionett',
			'Vandalkonto',
			'Olämpligt användarnamn',
			'Trollkonto'
		];
		if ( isAnon ) {
			reasonlist = [
				'Klotter/Vandalism',
				'Spam',
				'Öppen proxy',
				'Trolleri',
				'Nätmobbning'
			];
		}
		var reasonoptions = reasonlist.map( function( v ) {
			return '<option value="' + v + '">';
		} );
		var reasonstr = reasonoptions.join( '' );
		var str =
		'<div id="gadget-blockapi-container">' +
			'<label for="gadget-blockapi-user" id="gadget-blockapi-label-user" title="' + userlabeltitle + '">' + userlabel + '</label><input id="gadget-blockapi-user">' +
			'<label for="gadget-blockapi-expiry" id="gadget-blockapi-label-expiry" title="' + expirylabeltitle + '">' + expirylabel + '</label><input id="gadget-blockapi-expiry" list="gadget-blockapi-expiryoptions"><datalist id="gadget-blockapi-expiryoptions">' + expirystr + '</datalist>' +
			'<label for="gadget-blockapi-reason" id="gadget-blockapi-label-reason" title="' + reasonlabeltitle + '">' + reasonlabel + '</label><input id="gadget-blockapi-reason" list="gadget-blockapi-reasonoptions"><datalist id="gadget-blockapi-reasonoptions">' + reasonstr + '</datalist>' +
			'<div><input type="checkbox" id="gadget-blockapi-nocreate" checked><label for="gadget-blockapi-nocreate">' + nocreatelabel + '</label></div>' +
			'<div><input type="checkbox" id="gadget-blockapi-noemail"><label for="gadget-blockapi-noemail">' + noemaillabel + '</label></div>' +
			'<div><input type="checkbox" id="gadget-blockapi-allowusertalk"><label for="gadget-blockapi-allowusertalk">' + allowusertalklabel + '</label></div>';
		if ( !isAnon ) {
			str += '<div><input type="checkbox" id="gadget-blockapi-autoblock" checked><label for="gadget-blockapi-autoblock">' + autoblocklabel + '</label></div>';
		}
		str += '<div><input type="checkbox" id="gadget-blockapi-watchuser"><label for="gadget-blockapi-watchuser">' + watchuserlabel + '</label></div>';
		if ( isAnon ) {
			str += '<div><input type="checkbox" id="gadget-blockapi-anononly"><label for="gadget-blockapi-anononly">' + anononlylabel + '</label></div>';
		}
		str += '<div><input type="checkbox" id="gadget-blockapi-msg"><label for="gadget-blockapi-msg">' + msglabel + '</label></div>';
		str += '<button id="gadget-blockapi-block" title="' + blocktitle + '">' + blocktext + '</button>';
		str += '<button id="gadget-blockapi-close" title="' + closetitle + '">' + closetext + '</button>';
		str += '</div>';
		// Creating and opening a simple dialog window.

		// Subclass Dialog class. Note that the OOjs inheritClass() method extends the parent constructor's prototype and static methods and properties to the child constructor.

		function MyDialog( config ) {
			MyDialog.super.call( this, config );
		}
		if ( $( '.oo-ui-window-active' ).length === 0 ) {
			OO.inheritClass( MyDialog, OO.ui.Dialog );

			// Specify a title statically (or, alternatively, with data passed to the opening() method).
			MyDialog.static.name = 'gadgetblockapidialog';
			MyDialog.static.title = 'Simple dialog';

			// Customize the initialize() function: This is where to add content to the dialog body and set up event handlers.
			MyDialog.prototype.initialize = function () {
				// Call the parent method
				MyDialog.super.prototype.initialize.call( this );
				// Create and append a layout and some content.
				this.content = new OO.ui.PanelLayout( { padded: true, expanded: false } );
				this.content.$element.append( str );
				this.$body.append( this.content.$element );
				$( '#gadget-blockapi-user' ).val( user );
			};

			// Set up the ready mode of the window. 
			MyDialog.prototype.getReadyProcess = function ( data ) {
				return MyDialog.super.prototype.getReadyProcess.call( this, data )
				.next( function () {
					$( '#gadget-blockapi-reason' ).focus();
				}, this );
			};

			// Use the getTeardownProcess() method to perform actions whenever the dialog is closed.
			// This method provides access to data passed into the window's close() method
			// or the window manager's closeWindow() method.
			MyDialog.prototype.getTeardownProcess = function ( data ) {
				return MyDialog.super.prototype.getTeardownProcess.call( this, data )
				.first( function () {
					// Perform any cleanup as needed
					$( '.oo-ui-windowManager' ).remove();
				}, this );
			};

			// Make the window.
			var myDialog = new MyDialog();

			// Create and append a window manager, which will open and close the window.
			var windowManager = new OO.ui.WindowManager();
			$( 'body' ).append( windowManager.$element );

			// Add the window to the window manager using the addWindows() method.
			windowManager.addWindows( [ myDialog ] );

			// Open the window!
			windowManager.openWindow( myDialog );
			
			$( '#gadget-blockapi-block' ).click( function() {
				var obj = {
					action: 'block'
				};
				user = $( '#gadget-blockapi-user' ).val();
				var expiry = $( '#gadget-blockapi-expiry' ).val();
				var reason = $( '#gadget-blockapi-reason' ).val();
				var shouldSendMsg;
				var msgstr = 'Det här kontot är blockerat';
				if ( isAnon ) {
					msgstr = 'Den här IP-adressen är blockerad';
				}
				msgstr += '.\n\nVaraktighet: ';
				if ( expiry ) {
					expiry = expiry.trim();
					if ( expiry ) {
						obj.expiry = expiry;
						msgstr += expiry;
					}
				} else {
					msgstr += 'tills vidare';
				}
				if ( reason ) {
					reason = reason.trim();
					if ( reason ) {
						obj.reason = reason;
						msgstr += '\n\nAnledning: ' + reason;
					}
				}
				msgstr += '\n\n~~' + '~~';
				if ( $( '#gadget-blockapi-msg:checked' ).length ) {
					shouldSendMsg = true;
				}
				function onBlockError() {
					mw.notify( blockingof + user + failed );
				}
				function onMsgError() {
					mw.notify( msgto + user + failed );
				}
				if ( $( '#gadget-blockapi-nocreate:checked' ).length ) {
					obj.nocreate = true;
				}
				if ( $( '#gadget-blockapi-noemail:checked' ).length ) {
					obj.noemail = true;
				}
				if ( !$( '#gadget-blockapi-allowusertalk:checked' ).length ) {
					obj.allowusertalk = true;
				}
				if ( $( '#gadget-blockapi-autoblock:checked' ).length ) {
					obj.autoblock = true;
				}
				if ( $( '#gadget-blockapi-watchuser:checked' ).length ) {
					obj.watchuser = true;
				}
				if ( !$( '#gadget-blockapi-anononly:checked' ).length ) {
					obj.anononly = true;
				}
				if ( user ) {
					user = user.trim();
					if ( user ) {
						obj.user = user;
						( new mw.Api() ).postWithToken( 'csrf', obj ).done( function( blockdata ) {
							if ( blockdata.block ) {
								mw.notify( user + blocked );
								if ( shouldSendMsg ) {
									( new mw.Api() ).newSection( 'Användardiskussion:' + user, 'Blockerad', msgstr ).done( function( msgdata ) {
										if ( msgdata.edit ) {
											mw.notify( msgto + user + delivered );
										} else {
											onMsgError();
										}
									} ).fail( function() {
										onMsgError();
									} );
								}
							} else {
								onBlockError();
							}
						} ).fail( function() {
							onBlockError();
						} );
					}
				}
				myDialog.close();
			} );
			$( '#gadget-blockapi-close' ).click( function() {
				myDialog.close();
			} );
			$( '#gadget-blockapi-container' ).keyup( function( e ) {
				if ( e.which === 13 ) {
					$( '#gadget-blockapi-block' ).trigger( 'click' );
				}
			} );
		}
	} );
} );