User:Panamitsu/script/wikischedule.js - Wikipedia


Article Images

Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.

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.

// TODO: Hide the text saying that there has been a new talk page message

// Get start and end editing times in 24 hour format (WARNING: document.currentScript does not work on Internet Explorer)
const startEditing = new URLSearchParams(document.currentScript.src).get("startEditing");
const endEditing = new URLSearchParams(document.currentScript.src).get("endEditing");
const ignoreScheduleOnWeekends = new URLSearchParams(document.currentScript.src).get("weekend");


$( document ).ready( function () {
	const BLOCK_TOKENS = ["Special:Watchlist", "Special:RecentChanges", "Talk:", "Special:Contributions", "User:", "User_talk:", "&action=history", "action=edit"];
	const EXEMPT_TOKENS = ["User_talk:Panamitsu", ".js", "/script/", "wikischedule"];
	
	// IDs of elements that will be hidden when outside the editing schedule
	const ELEMENTS_TO_HIDE = ["pt-notifications-alert", "pt-notifications-notice", "pt-watchlist-2", "left-navigation", "right-navigation"];

	
	// Make sure the times are valid
	if (!(isValidTime(startEditing) && isValidTime(endEditing))) {
		return;
	}
	if (startEditing >= endEditing ) {
		alert(`The startEditing time must be less than the endEditing time. You have startEditing=${startEditing} and endEditing=${endEditing} which is invalid.`);
		return;
	}

	const currentTime = getCurrentTime();
	url = window.location.href;
	
	if (!isInEditingSchedule(startEditing, endEditing, currentTime)) {
		if (isOnEditorPage(url)) {
			blockPage(currentTime);
		} else if (!isOnExemptPage(url)) {
			hideUIElements();
		}
	}
	
	function isValidTime(time) {
		const timeInt = parseInt(time);
		if (time.length != 4) {
			alert(`Wikischedules: The time ${time} does not have 4 digits. Two digits must be for the hour (24 hours) and the next two digits for the minute. For example, '2325' is 11:25 PM.`);
			return false;
		}
		if (timeInt < 0 | timeInt > 2399) {
			alert(`Wikischedules: The time ${time} is not in the range of 24 hours. It must be no less than 0000 (midnight) and no more than 2399 (11:59pm).`);
			return false;
		}
		
		if (isNaN(time)) {
			alert(`Wikischedules: The time ${time} is not a number. It must be a four digit number representing a 24 hour time. No seperators (e.g. colons) are allowed. For example, 9:30am is represented as 0930.`)
			return false;
		}
		
		return true;
	}
	
	// Gets the current time in the 4 digit integer format
	function getCurrentTime() {
		let dateObj = new Date();
		return dateObj.getHours()*100+dateObj.getMinutes();
	}
	
	function isInEditingSchedule(start, end, now) {
		let day = new Date().getDay();
		let isWeekend = (day == 0 || day == 6);
		return ((now > start && now < end) || (ignoreScheduleOnWeekends && isWeekend));
	}
	
	function isOnEditorPage(url) {
		
		if (isOnExemptPage(url)) return false;
		
		return BLOCK_TOKENS.some(t => url.includes(t));
	}
	
	function isOnExemptPage(url) {
		// Javascript pages are exempt incase a user accidentally schedules a wrong time (or they want to change the schedule)
		if (EXEMPT_TOKENS.some(t => url.includes(t))) {
			return true;
		}
	}
	
	function blockPage(currentTime) {
		alert(`You have been prevented from editing outside of your chosen schedule. The time is ${currentTime} which is outside your editing schedule of ${startEditing} to ${endEditing}. If this is a mistake, edit the parameters of the Wikischedule script in your common.js, or ask Panamitsu for help. Both pages are unblocked.`);
		window.location.href = "https://en.wikipedia.org/wiki/User:Panamitsu/script/wikischedule/block_message";
	}
	
	function hideUIElements() {
		for (const id of ELEMENTS_TO_HIDE) {
			hideElementById(id);
		}
	}
	
	function hideElementById(id) {
		document.getElementById(id).style.display = 'none';
	}
} );