Ignore:
Timestamp:
10/15/12 14:53:44 (12 years ago)
Author:
pharms
Message:
  • changed implementation so that java script is served by server itself and that it determines the servers location through its own URL
Location:
trunk/autoquest-htmlmonitor
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/autoquest-htmlmonitor

    • Property svn:ignore set to
      target
  • trunk/autoquest-htmlmonitor/src/main/js/autoquest-htmlmonitor.js

    r876 r879  
    2525 * the server to send the recorded data to 
    2626 */ 
    27 var destination = "http://localhost:8090"; // change to the location of your server 
     27var autoquestDestination; 
    2828 
    2929/** 
    3030 * the maximum number of recorded events to be put into one package sent to the server 
    3131 */ 
    32 var packageSize = 10; 
     32var autoquestPackageSize = 10; 
    3333 
    3434/** 
     
    3636 * event handling action to be monitored 
    3737 */ 
    38 var actionConfig = [ 
     38var autoquestActionConfig = [ 
    3939    { "tag": "body", "actions": [ "onunload", "onscroll" ] }, 
    4040    { "tag": "a", "actions": [ "onclick", "ondblclick", "onfocus" ] }, 
     
    4545 * a possibility to trace, what is going on 
    4646 */ 
    47 var doLog = false; 
     47var autoquestDoLog = false; 
    4848 
    4949/*var matchedTags = ["A", "ABBR", "ACRONYM", "ADDRESS", "AREA", "B", "BIG", "BLOCKQUOTE", "BODY", 
     
    6161 * stores events, which were recorded but not sent to the server yet 
    6262 */ 
    63 var recordedEvents = []; 
     63var autoquestRecordedEvents = []; 
    6464 
    6565/** 
     
    7777    if (document.body) { 
    7878        log("adding event handling attributes"); 
     79        determineDestination(); 
    7980        addEventHandlingAttributes(document.documentElement, ""); 
    8081         
     
    9091 
    9192/** 
    92  * traverses the DOM-structure of the HTML-site, and adds event handling attributes to each 
     93 * traverses the DOM-structure of the HTML-site and determines the URL of this script. Based on 
     94 * this URL, it calculates the destination to which the traced interactions must be sent 
     95 */ 
     96function determineDestination() { 
     97    var scriptElements = document.getElementsByTagName("script"); 
     98    var i; 
     99    var index; 
     100     
     101    for (i = 0; i < scriptElements.length; i++) { 
     102        if ((scriptElements[i].type === "text/javascript") && (scriptElements[i].src)) { 
     103            index = scriptElements[i].src.lastIndexOf("script/autoquest-htmlmonitor.js"); 
     104            if (index > -1) { 
     105                autoquestDestination = scriptElements[i].src.substring(0, index - 1); 
     106                log("using destination " + autoquestDestination); 
     107            } 
     108        } 
     109    } 
     110} 
     111 
     112/** 
     113 * traverses the DOM-structure of the HTML-site and adds event handling attributes to each 
    93114 * relevant node 
    94115 *  
     
    105126     
    106127    if (node.nodeType === Node.ELEMENT_NODE) { 
    107         for (i = 0; i < actionConfig.length; i++) { 
    108             if (node.tagName.toLowerCase() === actionConfig[i].tag.toLowerCase()) { 
    109                 for (k = 0; k < actionConfig[i].actions.length; k++) { 
    110                     value = "handleEvent('" + actionConfig[i].actions[k] + "', '" + nodePath + 
    111                         "', event);"; 
    112  
    113                     if (!node.getAttribute(actionConfig[i].actions[k])) { 
    114                         node.setAttribute(actionConfig[i].actions[k], value); 
     128        for (i = 0; i < autoquestActionConfig.length; i++) { 
     129            if (node.tagName.toLowerCase() === autoquestActionConfig[i].tag.toLowerCase()) { 
     130                for (k = 0; k < autoquestActionConfig[i].actions.length; k++) { 
     131                    value = "handleEvent('" + autoquestActionConfig[i].actions[k] + "', '" + 
     132                        nodePath + "', event);"; 
     133 
     134                    if (!node.getAttribute(autoquestActionConfig[i].actions[k])) { 
     135                        node.setAttribute(autoquestActionConfig[i].actions[k], value); 
    115136                    } 
    116137                    else { 
    117                         var oldValue = node.getAttribute(actionConfig[i].actions[k]); 
     138                        var oldValue = node.getAttribute(autoquestActionConfig[i].actions[k]); 
    118139                        if (oldValue.indexOf(value) < 0) { 
    119                             node.setAttribute(actionConfig[i].actions[k], value + ' ' + oldValue); 
     140                            node.setAttribute(autoquestActionConfig[i].actions[k], 
     141                                              value + ' ' + oldValue); 
    120142                        } 
    121143                    } 
     
    189211 * of the nodes. These attributes are generated by the 
    190212 * {@link #addEventHandlingAttributes(node,parentPath)} function. It creates a new Event object and 
    191  * add it to the list of <code>recordedEvents</code>. If this list achieves the maximum 
    192  * <code>packageSize</code> the events in the list are sent to the server asynchronously through 
    193  * calling {@link #sendRequest()}. 
     213 * add it to the list of <code>autoquestRecordedEvents</code>. If this list achieves the maximum 
     214 * <code>autoquestPackageSize</code> the events in the list are sent to the server asynchronously 
     215 * through calling {@link #sendRequest()}. 
    194216 *  
    195217 * @param eventName the name of the event, e.g. onscroll 
     
    198220 */ 
    199221function handleEvent(eventName, nodePath, event) { 
     222    log("handling event " + eventName); 
     223     
     224    if (!autoquestDestination) { 
     225        // do nothing if we have no destination to send data to 
     226        return; 
     227    } 
     228     
    200229    var eventType = eventName.toLowerCase(); 
    201230     
    202     var eventObj = recordedEvents.pop(); 
     231    var eventObj = autoquestRecordedEvents.pop(); 
    203232     
    204233    // reuse previous on scroll events to prevent too many events 
    205234    if ((eventName !== "onscroll") || (!eventObj) || (eventObj.type !== "onscroll")) { 
    206235        if (eventObj) { 
    207             recordedEvents.push(eventObj); 
     236            autoquestRecordedEvents.push(eventObj); 
    208237        } 
    209238        eventObj = new Event(eventType, nodePath); 
     
    224253    } 
    225254 
    226     recordedEvents.push(eventObj); 
    227  
    228     if ((recordedEvents.length >= packageSize) || (eventType === "onunload")) { 
     255    log("storing event " + eventName); 
     256    autoquestRecordedEvents.push(eventObj); 
     257 
     258    if (autoquestRecordedEvents.length >= autoquestPackageSize) { 
     259        log("initiating sending events"); 
    229260        setTimeout(sendRequest, 100); 
    230261    } 
     262    else if (eventType === "onunload") { 
     263        log("initiating sending events"); 
     264        sendRequest(); 
     265    } 
    231266 
    232267} 
     
    234269/** 
    235270 * sends the collected data to the server, named in the destination-variable. For this it generates 
    236  * a JSON formatted message and uses Ajax and the <code>destination</code> to send it to the server 
     271 * a JSON formatted message and uses Ajax and the <code>autoquestDestination</code> to send it to 
     272 * the server 
    237273 */ 
    238274function sendRequest() { 
     
    243279    var request; 
    244280     
    245     if (recordedEvents.length > 0) { 
    246         eventList = recordedEvents; 
    247         recordedEvents = []; 
     281    if (autoquestRecordedEvents.length > 0) { 
     282        log("creating message"); 
     283        eventList = autoquestRecordedEvents; 
     284        autoquestRecordedEvents = []; 
    248285         
    249286        message = "{\"message\":{\"clientInfos\":{"; 
     
    283320        } 
    284321         
    285         request.open("POST", destination, true); 
     322        log("sending message"); 
     323        request.open("POST", autoquestDestination, true); 
    286324 
    287325        log("sending " + message); 
     
    434472 */ 
    435473function log(text) { 
    436     if (doLog) { 
     474    if (autoquestDoLog) { 
    437475        var loggingInfo = document.getElementById("loggingInfoParagraph"); 
    438476         
Note: See TracChangeset for help on using the changeset viewer.