Pecoes Wiki
Advertisement
1 /**

2 * @fileOverview 19 June 2012 -- This file defines the lightweight Railgun server script, 3 * which processes localStorage requests issued by the Railgun client via postMessage. 4 * The Railgun server consists of 3 pieces: this script, a page in the MediaWiki namespace, 5 * and a page in the content namespace. The MediaWiki page carries this server script, 6 * and in turn is included into the content page with verbatim tags. The content page 7 * (the server) is then loaded within an iframe by the Railgun client. 8 * @author Jeff Bradford (User:Mathmagician) 9 * @version 1.1.0 10 */ 11 12 /** 13 * @namespace A wrapper object for all Railgun server methods. The main method is 14 * processRequest(), which is the server's message event handler. The event handler 15 * delegates various client requests to the other methods in the namespace. 16 */ 17 RailgunServer = { 18 /** 19 * <code>true</code> if the server is in debug mode, <code>false</code> otherwise 20 * @type Boolean 21 * @default false 22 */ 23 isDebug : false, 24 25 /** 26 * A list of keys associated with localStorage data. 27 * @type Array 28 * @default ["friends", "siderailHidden"] 29 */ 30 keys : ["friends", "siderailHidden"], 31 32 /** 33 * The version of the Railgun server source code. 34 * @type String 35 */ 36 version : "1.1.0", 37 38 /** 39 * Retrieves a single item from localStorage. 40 * @param {String} key the key to query localStorage for 41 * @returns the JSON.parsed value associated with the given key 42 */ 43 getItem : function (key) { 44 return JSON.parse(window.localStorage.getItem(key)); 45 }, 46 47 /** 48 * Associates a value with a given key in localStorage. 49 * @param {String} key a key to associate with the given value 50 * @param value some value of arbitrary type to be stored 51 * @returns {Boolean} <code>true</code> if the operation was successful 52 * <code>false</code> otherwise 53 */ 54 setItem : function (key, value) { 55 if (-1 === this.keys.indexOf(key)) { 56 return false; 57 } else { 58 window.localStorage.setItem(key, JSON.stringify(value)); 59 return true; 60 } 61 }, 62 63 /** 64 * Removes all items from localStorage associated with native list of keys. 65 * @see RailgunServer.keys 66 */ 67 reset : function () { 68 for (var i = 0; i < this.keys.length; i++) { 69 window.localStorage.removeItem(this.keys[i]); 70 } 71 }, 72 73 /** 74 * Retrieves all items from localStorage associated with the native list of keys. 75 * @returns {Object} a mapping of (key, value) pairs in localStorage 76 * @see RailgunServer.keys 77 */ 78 retrieve : function () { 79 var result = {}; 80 for (var key in window.localStorage) { 81 if (-1 !== this.keys.indexOf(key)) 82 result[key] = this.getItem(key); 83 } 84 return result; 85 }, 86 87 /** 88 * The Railgun server's message event listener which processes requests made 89 * by the Railgun client. This method reads instructions from the client, 90 * delegates the task to an appropriate method, and then sends the server's 91 * response back to the client. To make a request, the client must send the 92 * server a JSON.stringified request object with instruction, key and value 93 * properties: <code>{ instruction : "setItem", key : "siderailHidden", 94 * value : true }</code> 95 * @param {MessageEvent} event an event corresponding to a postMessage 96 * request made by the client 97 */ 98 processRequest : function (event) { 99 // only process requests from wikia.com 100 if (-1 === event.origin.indexOf(".wikia.com")) 101 return; 102 103 var request = JSON.parse(event.data); 104 var response = request; 105 response.status = "success"; 106 107 switch (request.instruction) 108 { 109 // process "setItem" request 110 case "setItem" : 111 if (RailgunServer.setItem(request.key, request.value)) 112 response.status = "failure: unrecognized key"; 113 break; 114 // process "getItem" request 115 case "getItem" : 116 response.value = RailgunServer.getItem(request.key); 117 break; 118 // process "reset" request 119 case "reset" : 120 RailgunServer.reset(); 121 break; 122 // process "retrieve" request 123 case "retrieve" : 124 // set server mode 125 RailgunServer.isDebug = request.isDebug ? true : false; 126 127 // server data to send back to the client 128 response.serverState = { 129 isDebug : RailgunServer.isDebug, 130 keys : RailgunServer.keys, 131 version : RailgunServer.version 132 }; 133 134 // storage data to send back to the client 135 response.storageState = RailgunServer.retrieve(); 136 break; 137 default : 138 console.log("Server did not recognize " + request.instruction + " instruction."); 139 } 140 141 // console.log for debugging 142 if (RailgunServer.isDebug) { 143 console.log("Server has finished processing " + request.instruction + " request:"); 144 console.log(response); 145 } 146 147 // Send JSON.stringify(response) to the client as the response 148 event.source.postMessage(JSON.stringify(response), event.origin); 149 } 150 }; 151 152 // register processRequest() as the server's event listener 153 if (window.addEventListener) { 154 window.addEventListener("message", RailgunServer.processRequest, false); 155 } else if (window.attachEvent) { 156 window.attachEvent("message", RailgunServer.processRequest);

157
}
Advertisement