[go: up one dir, main page]

Jump to content

User:Panamitsu/script/wikischedule.js

From Wikipedia, the free encyclopedia
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';
	}
} );