FANDOM


//
/**
 * @fileOverview 19 June 2012 -- This file defines the lightweight Railgun server script,
 * which processes localStorage requests issued by the Railgun client via postMessage.
 * The Railgun server consists of 3 pieces: this script, a page in the MediaWiki namespace,
 * and a page in the content namespace. The MediaWiki page carries this server script,
 * and in turn is included into the content page with verbatim tags. The content page
 * (the server) is then loaded within an iframe by the Railgun client.
 * @author Jeff Bradford (User:Mathmagician)
 * @version 1.1.0
 */
 
/**
 * @namespace A wrapper object for all Railgun server methods. The main method is
 * processRequest(), which is the server's message event handler. The event handler
 * delegates various client requests to the other methods in the namespace.
 */
RailgunServer = {
    /**
     * <code>true</code> if the server is in debug mode, <code>false</code> otherwise
     * @type Boolean
     * @default false
     */
    isDebug : false,
 
    /**
     * A list of keys associated with localStorage data.
     * @type Array
     * @default ["friends", "siderailHidden"]
     */
    keys : ["friends", "siderailHidden"],
 
    /**
     * The version of the Railgun server source code.
     * @type String
     */
    version : "1.1.0",
 
    /**
     * Retrieves a single item from localStorage.
     * @param {String} key the key to query localStorage for
     * @returns the JSON.parsed value associated with the given key
     */
    getItem : function (key) {
        return JSON.parse(window.localStorage.getItem(key));
    },
 
    /**
     * Associates a value with a given key in localStorage.
     * @param {String} key a key to associate with the given value
     * @param value some value of arbitrary type to be stored
     * @returns {Boolean} <code>true</code> if the operation was successful
     * <code>false</code> otherwise
     */
    setItem : function (key, value) {
        if (-1 === this.keys.indexOf(key)) {
            return false;
        } else {
            window.localStorage.setItem(key, JSON.stringify(value));
            return true;
        }
    },
 
    /**
     * Removes all items from localStorage associated with native list of keys.
     * @see RailgunServer.keys
     */
    reset : function () {
        for (var i = 0; i < this.keys.length; i++) {
            window.localStorage.removeItem(this.keys[i]);
        }
    },
 
    /**
     * Retrieves all items from localStorage associated with the native list of keys.
     * @returns {Object} a mapping of (key, value) pairs in localStorage
     * @see RailgunServer.keys
     */
    retrieve : function () {
        var result = {};
        for (var key in window.localStorage) {
            if (-1 !== this.keys.indexOf(key))
                result[key] = this.getItem(key);    
        }
        return result;
    },
 
    /**
     * The Railgun server's message event listener which processes requests made
     * by the Railgun client. This method reads instructions from the client,
     * delegates the task to an appropriate method, and then sends the server's
     * response back to the client. To make a request, the client must send the
     * server a JSON.stringified request object with instruction, key and value
     * properties: <code>{ instruction : "setItem", key : "siderailHidden",
     * value : true }</code>
     * @param {MessageEvent} event an event corresponding to a postMessage
     * request made by the client
     */
    processRequest : function (event) {
        // only process requests from wikia.com
        if (-1 === event.origin.indexOf(".wikia.com"))
            return;
 
        var request = JSON.parse(event.data);
        var response = request;
        response.status = "success";
 
        switch (request.instruction)
        {
        // process "setItem" request
        case "setItem" :
            if (RailgunServer.setItem(request.key, request.value))
                response.status = "failure: unrecognized key";
            break;
        // process "getItem" request
        case "getItem" :
            response.value = RailgunServer.getItem(request.key);
            break;
        // process "reset" request
        case "reset" :
            RailgunServer.reset();
            break;
        // process "retrieve" request
        case "retrieve" :
            // set server mode
            RailgunServer.isDebug = request.isDebug ? true : false;
 
            // server data to send back to the client
            response.serverState = {
                isDebug : RailgunServer.isDebug,
                keys : RailgunServer.keys,
                version : RailgunServer.version
            };
 
            // storage data to send back to the client
            response.storageState = RailgunServer.retrieve();
            break;
        default :
            console.log("Server did not recognize " + request.instruction + " instruction.");
        }
 
        // console.log for debugging
        if (RailgunServer.isDebug) {
            console.log("Server has finished processing " + request.instruction + " request:");
            console.log(response);
        }
 
        // Send JSON.stringify(response) to the client as the response
        event.source.postMessage(JSON.stringify(response), event.origin);
    }
};
 
// register processRequest() as the server's event listener
if (window.addEventListener) {
    window.addEventListener("message", RailgunServer.processRequest, false);
} else if (window.attachEvent) {
    window.attachEvent("message", RailgunServer.processRequest);
}
//

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.