Jump to content

User:Novem Linguae/Scripts/NotSoFast.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Novem Linguae (talk | contribs) at 09:13, 10 July 2021 (comment). 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>

/*
In [[Special:NewPagesFeed]], this script highlights articles newer than 15 minutes RED, and articles newer than 1 hour YELLOW. This is to remind you not to patrol those articles yet. [[WP:NPP]] says that articles should not be patrolled until at least 15 minutes have elapsed. This is to give the writer time to work on the article without getting CSD tagged, maintenance tagged, edit conflicted, etc.
*/

mw.loader.using('mediawiki.storage').then(function () {
	mw.storage.session.set( 'client-error-opt-out', '1' );
});

$(function() {
	function getArticleName() {
		// returns the pagename, including the namespace name, but with spaces replaced by underscores
		return mw.config.get('wgPageName');
	}
	
	function runScript(obj) {
		$(obj).find('.mwe-pt-creation-date').each(function(i) {
			let dateTimeString = $(this).html().trim();
			let timestamp = Date.parse(dateTimeString)/1000;
			if ( timestamp > fifteenMinutesAgo ) {
				$(this).css("background-color", "#F79C8F");
			} else if ( timestamp > oneHourAgo ) {
				$(this).css("background-color", "#F5F591");
			}
		});
	}
	
	function inAFCMode() {
		return $('#mwe-pt-radio-afc').is(':checked');
	}
	
	let title = getArticleName();
	if ( title != 'Special:NewPagesFeed' ) return;
	
	let currentTimestamp = Math.floor(Date.now() / 1000);
	let fifteenMinutesAgo = currentTimestamp - 60*15;
	let oneHourAgo = currentTimestamp - 60*60;
	
	// run it once in case the NPP applet loaded BEFORE this script (race condition)
	$('.mwe-pt-list-item').each(function() {
		if ( ! inAFCMode() ) {
			runScript(this);
		}
	});
	
	// then run it again whenever a DOM node is inserted (the list refreshes as you scroll down, so this can be anytime you scroll down). could also be because this script loads BEFORE the the NPP applet (race condition)
	$('body').on('DOMNodeInserted', '.mwe-pt-list-item', function() {
		if ( ! inAFCMode() ) {
			runScript(this);
		}
	});
});

// </nowiki>