var events = require('events');
var util = require('util');
var Chrome = require('chrome-remote-interface');
var common = require('./common.js');
var Page = require('./page.js');
var har = require('./har.js');

var NEUTRAL_URL = 'about:blank';

var CLEANUP_SCRIPT_FIRST =
    'chrome.benchmarking.clearCache();' +
    'chrome.benchmarking.clearHostResolverCache();' +
    'chrome.benchmarking.clearPredictorCache();' +
    'chrome.benchmarking.closeConnections();';

var CLEANUP_SCRIPT_REPEAT =
    'chrome.benchmarking.clearHostResolverCache();' +
    'chrome.benchmarking.clearPredictorCache();' +
    'chrome.benchmarking.closeConnections();';

var PAGE_DELAY = 1000;

function Client(urls, options) {
    var self = this;
    var pages = [];
    options = options || {};
    options.fetchContent = !!(options.fetchContent) || false;
    options.repeatView = !!(options.repeatView) || false;
    options.onLoadDelay = options.onLoadDelay || 0;
    // start the instrumentation
    Chrome(options, function (chrome) {
        function loadUrl(index) {
            if (index < urls.length) {
                var url = urls[index];
                var page = new Page(index, url, chrome, options.fetchContent);
                var loadEventTimeout;
                pages[index] = page;
                // load a neutral page before the user provided URL since
                // there's no way to stop pending loadings using the protocol
                chrome.Page.navigate({'url': NEUTRAL_URL}, function (error, response) {
                    if (error) {
                        // probably never emitted...
                        self.emit('error', new Error('Cannot load URL'));
                        chrome.close();
                    }
                });
                // wait its completion before starting with the next user-defined URL
                var neutralFrameid;
                chrome.on('event', function (message) {
                    switch (message.method) {
                    case 'Page.frameNavigated':
                        // save the frame id of the neutral URL
                        var frame = message.params.frame;
                        if (frame.url === NEUTRAL_URL) {
                            neutralFrameid = frame.id;
                        }
                        break;
                    case 'Page.frameStoppedLoading':
                        // load the next URL when done
                        if (message.params.frameId === neutralFrameid) {
                            chrome.removeAllListeners('event');
                            // inject the JavaScript code and load this URL
                            common.dump('--- Start: ' + url);
                            self.emit('pageStart', url);
                            var CLEANUP_SCRIPT = CLEANUP_SCRIPT_FIRST;
                            if (options.repeatView) {
                                CLEANUP_SCRIPT = CLEANUP_SCRIPT_REPEAT;
                                common.dump('--- repeatView enabled!');
                            }
                            chrome.Runtime.evaluate({'expression': CLEANUP_SCRIPT}, function (error, response) {
                                // error with the communication or with the JavaScript code
                                if (error || (response && response.wasThrown)) {
                                    var errorDetails = JSON.stringify(response, null, 4);
                                    var errorMessage = 'Cannot inject JavaScript: ' + errorDetails;
                                    common.dump(errorMessage);
                                    self.emit('error', new Error(errorMessage));
                                    chrome.close();
                                } else {
                                    chrome.Page.navigate({'url': url}, function (error, response) {
                                        if (error) {
                                            self.emit('error', new Error('Cannot load URL'));
                                            chrome.close();
                                        }
                                    });
                                }
                            });
                            // then process events
                            chrome.on('event', function (message) {
                                page.processMessage(message);
                                // check if done with the current URL
                                if (page.isFinished() && typeof loadEventTimeout === 'undefined') {
                                    // keep listening for events for a certain amount of time
                                    // after the load event is triggered
                                    loadEventTimeout = setTimeout(function () {
                                        common.dump('--- End: ' + url);
                                        self.emit(page.isFailed() ? 'pageError' : 'pageEnd', url);
                                        chrome.removeAllListeners('event');
                                        // TODO: inject RT data here:
                                         for (var requestId in page.objects) {
                                            var object = page.objects[requestId];
                                            if (!object.responseMessage || !object.responseFinishedMs ||
                                                !object.responseMessage.response.timing ||
                                                object.requestMessage.request.url.match('^data:')) {
                                                continue;
                                            }
                                            var obj_url = object.requestMessage.request.url;
                                            chrome.Runtime.evaluate({'expression': "performance.performance.getEntriesByName("+obj_url+").responseEnd", 'returnByValue': true}, function (e, r){
                                                common.dump('--- RT: responseEnd: '+ r.result.value + " resFinMs: " + object.responseFinishedMs + ' for ' + obj_url);
                                            });
                                        }
                                        // start the next URL after a certain delay
                                        // so to "purge" any spurious requests
                                        setTimeout(function () {
                                            loadUrl(index + 1);
                                        }, PAGE_DELAY);
                                    }, options.onLoadDelay);
                                }
                            });
                        }
                        break;
                    }
                });
            } else {
                // no more URLs to process
                chrome.close();
                self.emit('end', har.create(pages));
            }
        }
        self.emit('connect');
        // preliminary global setup
        chrome.Page.enable();
        chrome.Network.enable();
        //chrome.Network.setCacheDisabled({'cacheDisabled': true});
        // start!
        chrome.once('ready', function () {
            loadUrl(0);
        });
    }).on('error', function (err) {
        common.dump("Emitting 'error' event: " + err.message);
        self.emit('error', err);
    });
}

util.inherits(Client, events.EventEmitter);

module.exports = Client;

@@ -665,16 +665,360 @@
  1000;%0A%0A
+function checkEnd(c, url, obj, page) %7B%0A%09c.Runtime.evaluate(%7B'expression': %22performance.getEntriesByName('%22+url+%22')%5B0%5D.responseEnd;%22, 'returnByValue': true%7D, function (error, response)%7B%0A%09%09common.dump('--- RT: responseEnd: '+ response.result.value + %22 resFinMs: %22 + (obj.responseFinishedMs - page.originalRequestMs)  + ' for ' + url);%0A    %7D);%0A%7D%0A%0A
 function
@@ -5097,335 +5097,8 @@
 red%0A
-                                    loadEventTimeout = setTimeout(function () %7B%0A                                        common.dump('--- End: ' + url);%0A                                        self.emit(page.isFailed() ? 'pageError' : 'pageEnd', url);%0A                                        chrome.removeAllListeners('event');%0A
     
@@ -5163,16 +5163,16 @@
 a here:%0A
+
         
@@ -5822,412 +5822,351 @@
 rl;%0A
-                                            chrome.Ru
+%09%09%09%09%09%09%09%09%09%09%09checkEnd(chrome, obj_url, object, page);%0A                                        %7D%0A%0A                                    loadEve
 nt
+T
 ime
-.evaluate(%7B'expression': %22performance.performance.getEntriesByName(%22+obj_url+%22).responseEnd%22, 'returnByValue': true%7D, function (e, r)%7B%0A                                                common.dump('--- RT: responseEnd: '+ r.result.value + %22 resFinMs: %22 + object.responseFinishedMs + ' for ' + obj_url);%0A                                            %7D
+out = setTimeout(function () %7B%0A                                        common.dump('--- End: ' + url);%0A                                        self.emit(page.isFailed() ? 'pageError' : 'pageEnd', url
 );%0A 
@@ -6196,33 +6196,67 @@
                 
-%7D
+chrome.removeAllListeners('event');
 %0A               

Fix Rt retrivial