js/startup.js
author Mahlon E. Smith <mahlon@martini.nu>
Mon, 09 Feb 2015 09:20:01 -0800
changeset 0 52d30e6014a0
permissions -rw-r--r--
Initial commit. Experimental cross-platform frontend for yubikey programming.

/*######################################################################
### YubiKey global namespace
######################################################################*/
var Y = {

	// Information for the currently inserted key.
	keyinfo: {},

	// Declared windows, for easy referencing
	window: {},


	// Convert a string to hexadecimal.
	//
	toHex: function( string ) {
		var hex = '';
		for( var i = 0; i < string.length; i++ )
			hex += string.charCodeAt(i).toString(16);
		return hex;
	},


	// Return a random string of +length+.
	//
	randString: function( length ) {
		var text = '';
		var set  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

		for( var i = 0; i < length; i++ )
			text += set.charAt( Math.floor(Math.random() * set.length) );
		return text;
	},


	// Generic delayed callback that returns the timer handle.
	//
	delay: (function() {
		var timer = null;
		return function( callback, ms, clear_previous ) {
			if ( clear_previous) clearTimeout( timer );
			timer = setTimeout( callback, ms );
			return timer;
		};
	})(),

	// Stop the default webkit behavior (viewer) when you drag a file into a window.
	//
	disableDragDrop: function( window ) {
		window.ondragover = function(e) { e.preventDefault(); return false };
		window.ondrop     = function(e) { e.preventDefault(); return false };
	},

	// Node's filesystem interface.
	fs: require( 'fs' ),

	// Manage child processes.
	spawn: require( 'child_process' ).spawn
};


// Make the 'Y' namespace available elsewhere.
global.Y = Y;