Utilisateur:LD/IPCheck.js

Une page de Wikipédia, l'encyclopédie libre.
Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;

Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.
 /**
 * @name IPCheck
 * @version 2.1 11-2022
 * @see [[:en:Trey314159/homoglyphHunter.js]]
 * @authors User:LD, User:DreZhsh 
 * Importation dans votre commons.js : importScript('Utilisateur:LD/IPCheck.js');
 * OR import : mw.loader.load('//fr.wikipedia.org/w/index.php?title=User:LD/IPCheck.js&action=raw&ctype=text/javascript');
 */

/* jshint esversion: 6 */
mw.loader.using( [ 'mediawiki.util', 'oojs-ui-windows', 'mediawiki.api' ], () => {
	const portletLink = mw.util.addPortletLink( 'p-tb', '#', 'IP Checker', 'IPCPortlet' );
	let IPCheckWindowManager, bloquer;

	mw.util.addCSS( `
	.ipcheck-ipblocked {
		opacity: 0.4;
		text-decoration: line-through;
	}
` );

	if ( portletLink === null ) {
		return;
	}

	function sortIps( localblock, globalblock ) {
		const locallist = [],
			globallist = [];
		for ( const block of localblock ) {
			locallist.push( block.user );
		}
		for ( const block of globalblock ) {
			globallist.push( block.address );
		}
		document.querySelectorAll( '.ipcheckIp' ).forEach( ( element ) => {
			const ipAddress = element.textContent;
			if ( locallist.indexOf( ipAddress ) !== -1 || globallist.indexOf( ipAddress ) !== -1 ) {
				element.parentElement.setAttribute( 'class', 'ipcheck-ipblocked' );
				element.parentElement.setAttribute( 'title', 'IP Bloquée' );
			}
		} );

	}

	function getBlocks() {
		let arrayIp = mw.storage.session.get( 'ipcheck-Latest' ).split( ',' ),
			arrayLoop = [];
		while ( arrayIp.length !== 0 ) {
			arrayLoop = arrayIp.slice( 0, 50 );
			arrayIp = arrayIp.slice( 50 );
			const ipToCheck = arrayLoop.join( '|' );
			new mw.Api()
				.get( {
					action: 'query',
					format: 'json',
					list: 'blocks',
					formatversion: '2',
					bkusers: ipToCheck,
					bkprop: 'user'
				} )
				.then( ( localblock ) => {
					new mw.ForeignApi( 'https://meta.wikimedia.org/w/api.php' )
						.get( {
							action: 'query',
							format: 'json',
							list: 'globalblocks',
							formatversion: '2',
							bgaddresses: ipToCheck,
							bgprop: 'address'
						} )
						.then( ( globalblock ) => {
							if (
								localblock.query.blocks.length === 0 &&
				globalblock.query.globalblocks.lenth === 0
							) {
								return;
							}
							sortIps( localblock.query.blocks, globalblock.query.globalblocks );
						} );
				} );
		}
	}
	/**
	 * ipLinks
	 *
	 * Crée les liens à insérer dans la panneau
	 *
	 * @param {string} ipAddress - L'adresse IP
	 * @param {string} [introLink] - Le lien d'introduction
	 */
	function ipLinks( ipAddress, introLink = `<a class="ipcheckIp" href="/wiki/Spécial:Contributions/${ipAddress}" target="_blank">${ipAddress}</a>` ) {
		const isSysop = mw.config.get( 'wgUserGroups' ).indexOf( 'sysop' ) !== -1;
		if ( isSysop ) {
			bloquer = ` - <a href="https://fr.wikipedia.org/w/index.php?title=Sp%C3%A9cial:Bloquer&wpAutoBlock=1&wpCreateAccount=1&wpEditingRestriction=sidewide&wpHardBlock=1&wpReason=Autre&wpReason-other=[[Wikipédia:NOPROXY|Proxy ouvert]]&wpTarget=${ipAddress}" target="_blank">Bloquer</a>`;
		}
		const whois = `<a href="https://whois-referral.toolforge.org/gateway.py?lookup=true&ip=${ipAddress}" target="_blank">Whois</a> - <a href="https://bullseye.toolforge.org/ip/${ipAddress}" target="_blank">Bullseye</a>`,
			proxy = `<a href="https://www.ipqualityscore.com/free-ip-lookup-proxy-vpn-test/lookup/${ipAddress}" target="_blank">IPQualityScore</a> - <a href="https://www.spur.us/context/${ipAddress}" target="_blank">Spur</a> - <a href="https://ipcheck.toolforge.org/index.php?ip=${ipAddress}" target="_blank">IPCheck</a>`,
			wikimedia = `<a href="https://meta.toolforge.org/stalktoy/${ipAddress}" target="_blank">Blocages</a> - <a href="https://guc.toolforge.org/?by=date&user=${ipAddress}" target="_blank">Contributions globales</a>`,
			li = document.createElement( 'li' );
		li.innerHTML = `${introLink}${bloquer ?? ''}<br>${proxy}<br>(<small>${whois} - ${wikimedia}</small>)`;
		document.getElementById( 'ipCheckScriptList' ).append( li );
	}

	/**
	 * getIpInRc
	 *
	 * Obtient les IPs en fonction de la page
	 * que l'utilisateur consulte
	 */
	function getIpInRc() {
		const ipAlreadyChecked = [];
		const avaibleSpecialPage = [ 'BlockList', 'AbuseLog', 'Newpages', 'Log', 'Recentchanges', 'Watchlist' ];

		document.getElementById( 'ipCheckScriptList' ).replaceChildren();
		if ( avaibleSpecialPage.indexOf( mw.config.get( 'wgCanonicalSpecialPageName' ) ) !== -1 ) {
			document.querySelectorAll( '.mw-anonuserlink' ).forEach( ( element ) => {
				const ipAddress = element.children[ 0 ].textContent;
				if (
					ipAlreadyChecked.indexOf( ipAddress ) === -1 &&
					mw.util.isIPAddress( ipAddress )
				) {
					ipAlreadyChecked.push( ipAddress );
					ipLinks( ipAddress );
				}
			} );
		} else if ( mw.config.get( 'wgNamespaceNumber' ) === 0 ) {
			new mw.Api().get( {
				action: 'query',
				formatversion: 2,
				prop: 'revisions',
				titles: mw.config.get( 'wgPageName' ),
				rvprop: 'ids|user',
				rvlimit: 500
			} ).then( ( data ) => {
				const revisions = data.query.pages[ 0 ].revisions;
				for ( const rev of revisions ) {
					const ipAddress = rev.user;
					if ( rev.anon === true &&
			ipAlreadyChecked.indexOf( ipAddress ) === -1
					) {
						ipAlreadyChecked.push( ipAddress );
						ipLinks( ipAddress, `(<a href="/wiki/Spécial:Diff/${rev.revid}" target="_blank">Diff</a>) <a href="/wiki/Spécial:Contributions/${ipAddress}" target="_blank" class="ipcheckIp">${ipAddress}</a>` );
					}
				}
			} );
		} else {
			new mw.Api().get( {
				action: 'query',
				format: 'json',
				list: 'recentchanges',
				utf8: 1,
				formatversion: '2',
				rcprop: 'user',
				rcshow: 'anon',
				rclimit: '100'
			} ).then( ( data ) => {
				const recentchanges = data.query.recentchanges;
				for ( const rc of recentchanges ) {
					const ipAddress = rc.user;
					if ( ipAlreadyChecked.indexOf( ipAddress ) === -1 ) {
						ipAlreadyChecked.push( ipAddress );
						ipLinks( ipAddress );

					}
				}
			} );
		}
		mw.storage.session.set( 'ipcheck-Latest', ipAlreadyChecked );

	}

	/**
	 * DefinePageName
	 *
	 * Définit le nom à utiliser dans le panneau
	 *
	 * @return {string} Le nom à utiliser dans le panneau
	 */
	function definePageName() {
		if ( mw.config.get( 'wgNamespaceNumber' ) === 0 ) {
			mw.config.set( 'wgPageName', mw.config.get( 'wgPageName' ).replace( /_/g, ' ' ) );
			return `l'historique de ${mw.config.get( 'wgPageName' )}`;
		} else if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'BlockList' ) {
			return 'la liste des blocages';
		} else if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'AbuseLog' ) {
			return 'le journal des filtrages';
		} else if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Newpages' ) {
			return 'les nouvelles pages';
		} else if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Log' ) {
			return 'les journaux';
		} else if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Watchlist' ) {
			return 'la liste de suivi';
		} else {
			return 'les modifications récentes';
		}

	}

	/**
	 * initializeIPC
	 *
	 * Création du panneau d'IPCheck
	 */
	function initializeIPC() {
		function LongProcessDialog( config ) {
			LongProcessDialog.super.call( this, config );
		}
		OO.inheritClass( LongProcessDialog, OO.ui.ProcessDialog );
		LongProcessDialog.static.name = 'LongProcessDialog';
		LongProcessDialog.static.title = 'IP Check';
		LongProcessDialog.static.actions = [
			{ action: 'save', label: 'Cherchons', icon: 'reload', flags: [ 'primary', 'progressive' ] },
			{ action: 'cancel', label: 'Fermer la fenêtre', flags: [ 'safe', 'close' ] },
			{ action: 'sort', label: 'Filtrer les IPs bloquées', icon: 'funnel' }
		];
		LongProcessDialog.prototype.initialize = function () {
			LongProcessDialog.super.prototype.initialize.apply( this, arguments );
			const htmlElement = mw.html.element( 'div', { style: 'text-align:center; font-size:1.5em', id: 'ipCheckDiv' }, new mw.html.Raw( mw.html.element( 'strong', {}, `Recherche des IPs dans ${definePageName()}` ) ) );
			const htmlElement2 = mw.html.element( 'ul', { id: 'ipCheckScriptList', style: 'column-count:2;' }, '' );
			const htmlContent = ( htmlElement + htmlElement2 );
			this.content = new OO.ui.PanelLayout( { padded: true, expanded: false } );
			this.content.$element.append( htmlContent );
			this.$body.append( this.content.$element );
		};
		LongProcessDialog.prototype.getActionProcess = function ( action ) {
			const dialogAction = this;
			if ( action === 'cancel' ) {
				dialogAction.close();
			} else if ( action === 'save' ) {
				getIpInRc();
			} else if ( action === 'sort' ) {
				getBlocks();
			}
			return LongProcessDialog.super.prototype.getActionProcess.call( this, action );
		};
		IPCheckWindowManager = new OO.ui.WindowManager();
		$( document.body ).append( IPCheckWindowManager.$element );

		window.IPCheckDialog = new LongProcessDialog( { size: 'full' } );
		IPCheckWindowManager.addWindows( [ window.IPCheckDialog ] );
		IPCheckWindowManager.openWindow( window.IPCheckDialog );
	}

	portletLink.addEventListener( 'click', ( e ) => {
		e.preventDefault();
		if ( IPCheckWindowManager === undefined ) {
			initializeIPC();
		} else {
			IPCheckWindowManager.openWindow( window.IPCheckDialog );
		}
	} );
} );