User:Labgo/transcripthelper.js

var lastPressed = ''; var treated = false;

function addOnLoadEvent(fnc) { if ( typeof window.addEventListener != "undefined" ) window.addEventListener( "load", fnc, false ); else if ( typeof window.attachEvent != "undefined" ) { window.attachEvent( "onload", fnc ); } else { if ( window.onload != null ) { var oldOnload = window.onload; window.onload = function ( e ) { oldOnload( e ); window[fnc]; };   }    else window.onload = fnc; } }

function addEvent( obj, type, fn ) { if (obj.addEventListener) { obj.addEventListener( type, fn, false ); EventCache.add(obj, type, fn); }	else if (obj.attachEvent) { obj["e"+type+fn] = fn; obj[type+fn] = function { obj["e"+type+fn]( window.event ); } obj.attachEvent( "on"+type, obj[type+fn] ); EventCache.add(obj, type, fn); }	else { obj["on"+type] = obj["e"+type+fn]; } }

var EventCache = function{ var listEvents = []; return { listEvents : listEvents, add : function(node, sEventName, fHandler){ listEvents.push(arguments); },		flush : function{ var i, item; for(i = listEvents.length - 1; i >= 0; i = i - 1){ item = listEvents[i]; if(item[0].removeEventListener){ item[0].removeEventListener(item[1], item[2], item[3]); };				if(item[1].substring(0, 2) != "on"){ item[1] = "on" + item[1]; };				if(item[0].detachEvent){ item[0].detachEvent(item[1], item[2]); };				item[0][item[1]] = null; };		}	}; }; addEvent(window,'unload',EventCache.flush);

function setKeyboardEvents {

var Tb = document.getElementById("wpTextbox1"); if (!Tb) return; if (wgPageName.indexOf("/Transcript") != -1) {       addEvent(document.getElementById("wpTextbox1"), "keyup", showUp); addEvent(document.getElementById("wpTextbox1"), "keypress", showPress); } }

function showPress(evt) {

evt = (evt) ? evt : ((event) ? event : null); if (evt) { var typed; if (evt.which == null) { typed = String.fromCharCode(evt.keyCode); }   // IE            else if (evt.which > 0) { typed = String.fromCharCode(evt.which); }   // others else {typed = null;} // special key if (typed == null) {evt.returnValue = true; return;}

if (lastPressed == '' || lastPressed != typed) { treated = false; lastPressed = typed; evt.returnValue = true; return; } if(treated == false) {

lastPressed = typed; var star; switch (typed) {               case 'o': star = "OLIVIA: "; break; case 'w': star = "WALTER: "; break; case 'p': star = "PETER: "; break; case 'a': star = "ASTRID: "; break; case 'b': star = "BROYLES: "; break; case 'n': star = "NINA: "; break; case 'n': star = "CHARLIE: "; break; case 'j': star = "JOHN: "; break; default: star = "none";

}

if (star != "none") { if (navigator.appName == "Microsoft Internet Explorer") {                   var myRange = document.selection.createRange; myRange.text = "|*|*|"

var fullRange = document.getElementById("wpTextbox1").createTextRange; fullRange.findText("|*|*|"); fullRange.moveStart("character", -1); fullRange.text = star; }               else {                   var sel = window.getSelection; document.getElementById("wpTextbox1").selectionStart = document.getElementById("wpTextbox1").selectionStart - 1; var startPos = document.getElementById("wpTextbox1").selectionStart; var endPos = document.getElementById("wpTextbox1").selectionEnd; var rg = sel.getRangeAt(0); rg.deleteContents; var newElement = document.createTextNode(star); rg.insertNode(newElement); document.getElementById("wpTextbox1").selectionStart = startPos + star.length; document.getElementById("wpTextbox1").selectionEnd = startPos + star.length;

}               treated = true; evt.returnValue = false; return; }       }

}

if (treated) { evt.returnValue = false; } else { evt.returnValue = true; } return;

}

function showUp(evt) {   evt = (evt) ? evt : ((event) ? event : null); if (evt) { lastPressed = ''; treated = false; } }

// do event binding now that elements exist addOnLoadEvent(setKeyboardEvents);