felladin
Tera Guru

Is it Friday

I was setting up some things and decided to try out a little Easter egg; since it meant working with UI Script and GlideModalv3 I thought I'd add it here for those curious.

The Easter egg is triggered by the Konami code and checks for what day it is. Depending on whether or not it's Friday, it will show a different gif in a simple GlideModal.

I created a UI Script (and I stole the primary code from StackOverflow as it was well documented and simple in build) and named it "konami". I did not set it to Global, as I want to decide on which forms it's available (see Client Script).

// Allowed keys
var allowedKeys = {
	37: 'left',
	38: 'up',
	39: 'right',
	40: 'down',
	65: 'a',
	66: 'b'
};

// The 'official' Konami Code sequence
var konamiCode = ['up', 'up', 'down', 'down', 'left', 'right', 'left', 'right', 'b', 'a'];
// Variable to remember the 'position' the user has reached so far.
var konamiCodePosition = 0;

// Add keydown event listener
document.addEventListener('keydown', function (e) {
	// Get the value of the key code from the key map
	var key = allowedKeys[e.keyCode];
	// Get the value of the required key from the Konami code
	var requiredKey = konamiCode[konamiCodePosition];

	// Compare the key with the required key
	if (key == requiredKey) {
		// If key is correct move to the next key in the sequence
		konamiCodePosition++;
		// If the last key is reached, activate cheats
		if (konamiCodePosition == konamiCode.length) {
			// Activate GlideAjax to get correct day using internal timezone
			getDayFromServer();
			konamiCodePosition = 0;
		}
	} else {
		// Else means key was wrong for sequence and checker resets
		konamiCodePosition = 0;
	}
});
// Call script include clientHelper and function getCurrentTime
// This function takes optional parameters, otherwise it only returns current time
function getDayFromServer() {
	var ga = new GlideAjax('clientHelper');
	ga.addParam('sysparm_name', 'getCurrentTime');
	ga.getXML(unpackServerTime);
}
// Get the object from script include
function unpackServerTime(response) {
	var answer = response.responseXML.documentElement.getAttribute('answer');
	answer = answer.evalJSON();
	activateCheats(answer);
}
// Using Array to hold object is just something I do
function activateCheats(serverdate) {
	// Get the day from the object on first array location
	var thisdayfromserver = serverdate[0].now_weekday;
	// Set title variable
	var eastertitle = 'Waiting for Friday.';
	// Set variable with image name
	var easterimage = 'sad-fry.gif';
	// Turn day number into integer (no actual need, just for fun)
	var thisdayasinteger = Math.floor(thisdayfromserver.toString());
	// Check if the day is a Friday
	if (thisdayasinteger == 5) {
		// Change title and image
		eastertitle = 'IT\'S FRIDAY!';
		easterimage = 'fryday.gif';
	}
	// Create the "page" to present
	var easterpage = '<html><body><center><img src="' + easterimage + '" /></center></body></html>';
	// Open a modal with: No UI Page called, Show close button (true hides it), Width 600 px
	var eastermodal = new GlideModal('', false, 600);
	eastermodal.setTitle(eastertitle);
	// Render the modal using HTML
	eastermodal.renderWithContent(easterpage);
}

As the UI Script is not global we need to fetch it for our forms manually. This is done in a simple onLoad Client Script with the below code:

function onLoad() {
	// It is only "konami.jsdbx" as the UI Script is in Global Scope
	// If using a scoped app, add the app-name before konami
	// Note that the script is in an array, just string seems to not work in New York
	ScriptLoader.getScripts(['konami.jsdbx'], function(){});
}

I hope this shines a little light on GlideModal and non-global UI Scripts.

 

EDITS:

Since I created this I had to redo it in another instance and made some changes, as well as found that ScriptLoader wants an array in New York. I also added a client callable Script Include to get time data (there was an issue with first code as time zones were not honoured).

Part of script include:

var clientHelper = Class.create();
clientHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {

		// Returns current time and takes additional time parameters for addition
		getCurrentTime: function () {
			// Create an array to store the objects
			var answer = [];
			// Check if a parameter is present, otherwise set the variable as false
			var x_days = this.getParameter('sysparm_days') || false;
			var x_seconds = this.getParameter('sysparm_seconds') || false;
			var x_minutes = this.getParameter('sysparm_minutes') || false;
			var x_months = this.getParameter('sysparm_months') || false;
			var x_now = new GlideCalendarDateTime();
			answer.push({
				now_datetime: x_now.getDisplayValue(),
				now_time: x_now.getInternalFormattedLocalTime(),
				now_weekday: x_now.getDayOfWeekLocalTime(),
				now_dayofmonth: x_now.getDayOfMonthLocalTime()
			});
			if (x_days) {
				x_days.Math.floor();
				x_now.addDaysLocalTime(x_days);
				answer[0].added_days_datetime = x_now.getDisplayValue();
				answer[0].added_days_time = x_now.getInternalFormattedLocalTime();
				answer[0].added_days_weekday = x_now.getDayOfWeekLocalTime();
				answer[0].added_days_dayofmonth = x_now.getDayOfMonthLocalTime();
			}
			if (x_seconds) {
				x_seconds.Math.floor();
				x_now.addSeconds(x_seconds);
				answer[0].added_seconds_datetime = x_now.getDisplayValue();
				answer[0].added_seconds_time = x_now.getInternalFormattedLocalTime();
				answer[0].added_seconds_weekday = x_now.getDayOfWeekLocalTime();
				answer[0].added_seconds_dayofmonth = x_now.getDayOfMonthLocalTime();
			}
			if (x_minutes) {
				x_minutes.Math.floor();
				x_minutes = x_minutes * 60;
				x_now.addSeconds(x_minutes);
				answer[0].added_minutes_datetime = x_now.getDisplayValue();
				answer[0].added_minutes_time = x_now.getInternalFormattedLocalTime();
				answer[0].added_minutes_weekday = x_now.getDayOfWeekLocalTime();
				answer[0].added_minutes_dayofmonth = x_now.getDayOfMonthLocalTime();
			}
			if (x_months) {
				x_months.Math.floor();
				x_now.addMonthsLocalTime(x_months);
				answer[0].added_months_datetime = x_now.getDisplayValue();
				answer[0].added_months_time = x_now.getInternalFormattedLocalTime();
				answer[0].added_months_weekday = x_now.getDayOfWeekLocalTime();
				answer[0].added_months_dayofmonth = x_now.getDayOfMonthLocalTime();
			}
			return new JSON().encode(answer);
		},
Comments
felladin
Tera Guru

Updated Client Script ScripLoader to get an array instead of single string.
Fixed date to be fetched from server using GlideAjax.

Tyler Michals
Kilo Guru

Does this still work?

felladin
Tera Guru

Hello,

Yes, at least for us.
I think I had to do a tweak some time ago to get Dates to work properly, I can investigate later this week (busy).

But the base premise works (I use it for checking date API's and GlideAjax and such).

Regards
Anton

Version history
Last update:
‎06-26-2019 01:22 AM
Updated by: