Jump to content

User:Chaotic Enby/RecentUnblockHighlighter.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Chaotic Enby (talk | contribs) at 16:17, 1 January 2025 (thisn't). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <nowiki>

// Initialize the MediaWiki API
var ruhMediaWikiApi = new mw.Api();
		
// Setting up global-ish variables
trackingTime = 7776000000; // Exactly 90 days in millisections

class UserHighlighterSimple {
	/**
	 * @param {jQuery} $ jquery
	 * @param {Object} mw mediawiki
	 * @param {Window} window
	 */
	constructor( $, mw, window ) {
		// eslint-disable-next-line no-jquery/variable-pattern
		this.$ = $;
		this.mw = mw;
		this.window = window;
	}

	async execute() {
		if ( !this.window.userHighlighterSimpleNoColors ) {
			this.setHighlightColors();
		}
		const $links = this.$( '#article a, #bodyContent a, #mw_contentholder a' );
		$links.each( async ( index, element ) => {
			this.$link = this.$( element );
			if ( !this.linksToAUser() ) {
				return;
			}
			this.user = this.getUserName();
			const isUserSubpage = this.user.includes( '/' );
			if ( isUserSubpage ) {
				return;
			}
			await this.addClassesAndHoverTextToLinkIfNeeded(this.$link);
		} );
	}

	addCSS( htmlClass, cssDeclaration ) {
		// .plainlinks is for Wikipedia Signpost articles
		// To support additional custom signature edge cases, add to the selectors here.
		this.mw.util.addCSS( `
			.plainlinks .${ htmlClass }.external,
			.${ htmlClass },
			.${ htmlClass } b,
			.${ htmlClass } big,
			.${ htmlClass } font,
			.${ htmlClass } kbd,
			.${ htmlClass } small,
			.${ htmlClass } span {
				${ cssDeclaration }
			}
		` );
	}

	async getWikitextFromCache( title ) {
		const api = new this.mw.ForeignApi( 'https://en.wikipedia.org/w/api.php' );
		let wikitext = '';
		await api.get( {
			action: 'query',
			prop: 'revisions',
			titles: title,
			rvslots: '*',
			rvprop: 'content',
			formatversion: '2',
			uselang: 'content', // needed for caching
			smaxage: '86400', // cache for 1 day
			maxage: '86400' // cache for 1 day
		} ).then( ( data ) => {
			wikitext = data.query.pages[ 0 ].revisions[ 0 ].slots.main.content;
		} );
		return wikitext;
	}

	hasHref( url ) {
		return Boolean( url );
	}

	isAnchor( url ) {
		return url.charAt( 0 ) === '#';
	}

	isHttpOrHttps( url ) {
		return url.startsWith( 'http://', 0 ) ||
			url.startsWith( 'https://', 0 ) ||
			url.startsWith( '/', 0 );
	}

	/**
	 * Figure out the wikipedia article title of the link
	 *
	 * @param {string} url
	 * @param {mw.Uri} urlHelper
	 * @return {string}
	 */
	getTitle( url, urlHelper ) {
		// for links in the format /w/index.php?title=Blah
		const titleParameterOfUrl = this.mw.util.getParamValue( 'title', url );
		if ( titleParameterOfUrl ) {
			return titleParameterOfUrl;
		}

		// for links in the format /wiki/PageName. Slice off the /wiki/ (first 6 characters)
		if ( urlHelper.path.startsWith( '/wiki/' ) ) {
			return decodeURIComponent( urlHelper.path.slice( 6 ) );
		}

		return '';
	}

	notInUserOrUserTalkNamespace() {
		const namespace = this.titleHelper.getNamespaceId();
		const notInSpecialUserOrUserTalkNamespace = this.$.inArray( namespace, [ 2, 3 ] ) === -1;
		return notInSpecialUserOrUserTalkNamespace;
	}

	linksToAUser() {
		let url = this.$link.attr( 'href' );

		if ( !this.hasHref( url ) || this.isAnchor( url ) || !this.isHttpOrHttps( url ) ) {
			return false;
		}

		url = this.addDomainIfMissing( url );

		// mw.Uri(url) throws an error if it doesn't like the URL. An example of a URL it doesn't like is https://meta.wikimedia.org/wiki/Community_Wishlist_Survey_2022/Larger_suggestions#1%, which has a section link to a section titled 1% (one percent).
		let urlHelper;
		try {
			urlHelper = new this.mw.Uri( url );
		} catch {
			return false;
		}

		// Skip links that aren't to user pages
		const isUserPageLink = url.includes( '/w/index.php?title=User' ) || url.includes( '/wiki/User' );
		if ( !isUserPageLink ) {
			return false;
		}

		// Even if it is a link to a userpage, skip URLs that have any parameters except title=User, action=edit, and redlink=. We don't want links to diff pages, section editing pages, etc. to be highlighted.
		const urlParameters = urlHelper.query;
		delete urlParameters.title;
		delete urlParameters.action;
		delete urlParameters.redlink;
		const hasNonUserpageParametersInUrl = !this.$.isEmptyObject( urlParameters );
		if ( hasNonUserpageParametersInUrl ) {
			return false;
		}

		const title = this.getTitle( url, urlHelper );

		// Handle edge cases such as https://web.archive.org/web/20231105033559/https://en.wikipedia.org/wiki/User:SandyGeorgia/SampleIssue, which shows up as isUserPageLink = true but isn't really a user page.
		try {
			this.titleHelper = new this.mw.Title( title );
		} catch {
			return false;
		}

		if ( this.notInUserOrUserTalkNamespace() ) {
			return false;
		}

		const isDiscussionToolsSectionLink = url.includes( '#' );
		if ( isDiscussionToolsSectionLink ) {
			return false;
		}

		return true;
	}

	// Brandon Frohbieter, CC BY-SA 4.0, https://stackoverflow.com/a/4009771/3480193
	countInstances( string, word ) {
		return string.split( word ).length - 1;
	}

	/**
	 * mw.Uri(url) expects a complete URL. If we get something like /wiki/User:Test, convert it to https://en.wikipedia.org/wiki/User:Test. Without this, UserHighlighterSimple doesn't work on metawiki.
	 *
	 * @param {string} url
	 * @return {string} url
	 */
	addDomainIfMissing( url ) {
		if ( url.startsWith( '/' ) ) {
			url = window.location.origin + url;
		}
		return url;
	}

	/**
	 * @return {string}
	 */
	getUserName() {
		const user = this.titleHelper.getMain().replace( /_/g, ' ' );
		return user;
	}

    async recentUnblock( userName ) {
    	// Retrieve the logs of blocks directed to this specific user
    	var params = {
		    action: 'query',
		    format: 'json',
		    list: 'logevents',
		    letype: 'block',
		    letitle: 'User:' + userName
		};
		
		var blocks = await ruhMediaWikiApi.get( params );
		
		console.log(userName);
		console.log(blocks.query.logevents.length);
		
		if(blocks.query.logevents.length == 0) {
			return false;
		}
		console.log("everything works");
		
		var mostRecentBlock = blocks.query.logevents[0];
		console.log("x");
		
		if(mostRecentBlock.action == "unblock") {
			console.log("y");
			console.log(Date.now());
			console.log(Date.parse(mostRecentBlock.timestamp) + trackingTime);
			
			if(Date.now() > Date.parse(mostRecentBlock.timestamp) + trackingTime) {
				console.log("a");
				return false;
			}
			if(blocks.query.logevents.length > 1 && blocks.query.logevents[1].params.duration != "infinity" && Date.now() > Date.parse(blocks.query.logevents[1].params.expiry)) {
				console.log("b");
				return false;
			}
			return true;
		}
		
		if(mostRecentBlock.action == "block") {
			return false; // We do not want to track people still being blocked or whose block expired naturally
		}
		
        return false;
    }

	async checkForPermission( functionCheck, className, descriptionForHover, link ) {
		var check = await functionCheck(this.user);
		console.log(check);
		if ( check  ) {
			console.log("adding");
			this.addClassAndHoverText( className, descriptionForHover, link );
		}
	}

	addClassAndHoverText( className, descriptionForHover, link ) {
		console.log(link);
		link.addClass( className );
		console.log("added");

		const title = this.$link.attr( 'title' );
		if ( !title || title.startsWith( 'User:' ) ) {
			link.attr( 'title', descriptionForHover );
		}
		
		// If the user has been unblocked recently, we want to track them, so be aggressive about overriding the background and foreground color. That way there's no risk their signature is unreadable due to background color and foreground color being too similar.
		link.addClass( link.attr( 'class' ) + ' UHS-override-signature-colors' );
	}

	async addClassesAndHoverTextToLinkIfNeeded( link ) {
        // Adds the class, less classes than needed as it is just "recently unblocked - less than 1 month (default)" and "recently unblocked with custom ROPE"
		this.checkForPermission( this.recentUnblock, 'UHS-recent-unblock', 'Recently unblocked account', link );
	}

	setHighlightColors() {
		// Highest specificity goes on bottom. So if you want an admin+steward to be highlighted steward, place the steward CSS below the admin CSS in this section.
		this.addCSS( 'UHS-override-signature-colors', `
			color: #0645ad !important;
			background-color: transparent !important;
			background: unset !important;
		` );
		this.addCSS( 'UHS-recent-unblock', 'text-decoration:underline wavy rgb(148, 38, 139) !important;' );
	}
}

// Fire after wiki content is added to the DOM, such as when first loading a page, or when a gadget such as the XTools gadget loads.
mw.hook( 'wikipage.content' ).add( async () => {
	await mw.loader.using( [ 'mediawiki.util', 'mediawiki.Uri', 'mediawiki.Title' ], async () => {
		await ( new UserHighlighterSimple( $, mw, window ) ).execute();
	} );
} );

// Fire after an edit is successfully saved via JavaScript, such as edits by the Visual Editor and HotCat.
mw.hook( 'postEdit' ).add( async () => {
	await mw.loader.using( [ 'mediawiki.util', 'mediawiki.Uri', 'mediawiki.Title' ], async () => {
		await ( new UserHighlighterSimple( $, mw, window ) ).execute();
	} );
} );

// </nowiki>