User:Panamitsu/script/wikischedule.js
Appearance
From Wikipedia, the free encyclopedia
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.
This code will be executed when previewing this page.
This user script seems to have a documentation page at User:Panamitsu/script/wikischedule.
// 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) conststartEditing=newURLSearchParams(document.currentScript.src).get("startEditing"); constendEditing=newURLSearchParams(document.currentScript.src).get("endEditing"); constignoreScheduleOnWeekends=newURLSearchParams(document.currentScript.src).get("weekend"); $(document).ready(function(){ constBLOCK_TOKENS=["Special:Watchlist","Special:RecentChanges","Talk:","Special:Contributions","User:","User_talk:","&action=history","action=edit"]; constEXEMPT_TOKENS=["User_talk:Panamitsu",".js","/script/","wikischedule"]; // IDs of elements that will be hidden when outside the editing schedule constELEMENTS_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; } constcurrentTime=getCurrentTime(); url=window.location.href; if(!isInEditingSchedule(startEditing,endEditing,currentTime)){ if(isOnEditorPage(url)){ blockPage(currentTime); }elseif(!isOnExemptPage(url)){ hideUIElements(); } } functionisValidTime(time){ consttimeInt=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.`); returnfalse; } 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:59 pm).`); returnfalse; } 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.`) returnfalse; } returntrue; } // Gets the current time in the 4 digit integer format functiongetCurrentTime(){ letdateObj=newDate(); returndateObj.getHours()*100+dateObj.getMinutes(); } functionisInEditingSchedule(start,end,now){ letday=newDate().getDay(); letisWeekend=(day==0||day==6); return((now>start&&now<end)||(ignoreScheduleOnWeekends&&isWeekend)); } functionisOnEditorPage(url){ if(isOnExemptPage(url))returnfalse; returnBLOCK_TOKENS.some(t=>url.includes(t)); } functionisOnExemptPage(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))){ returntrue; } } functionblockPage(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"; } functionhideUIElements(){ for(constidofELEMENTS_TO_HIDE){ hideElementById(id); } } functionhideElementById(id){ document.getElementById(id).style.display='none'; } });