1

I'm looking for a way to use JavaScript to store debug messages that appear in the Chrome console when xmlhttprequests are performed. Example output provided below:

enter image description here

Thanks in advance!

compsciguy.
  • 65
  • 1
  • 6
  • Do you simply want to do something every time any XHR load completes, like jQuery's [`.ajaxComplete`](http://api.jquery.com/ajaxcomplete/)? Or do you want to persist the log information for debugging reasons? – apsillers Apr 07 '17 at 16:31
  • I believe the second option is what I need. I want to keep track of the URL's that the console is putting out, but as you can see, the XHR GET events in the first two lines belong to a different HTML and is inaccessible because of CORS. All I want to do is retrieve the URL string and store it in JavaScript. – compsciguy. Apr 07 '17 at 17:01

2 Answers2

2

You cannot read console messages from JavaScript. You will not be able to read these messages.

However, using the same general concept as John Culviner's answer to Add a “hook” to all AJAX requests on a page, you can detect the events in JavaScript that cause these messages to appear.

(function() {
    var origOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function(method, url) {
        this.addEventListener('load', function() {
            console.log('XHR finished loading', method, url);
        });

        this.addEventListener('error', function() {
            console.log('XHR errored out', method, url);
        });
        origOpen.apply(this, arguments);
    };
})();

This overwrites every XHR object's open method with a new function that adds load and error listeners to the XHR request. When the request completes or errors out, the functions have access to the method and url variables that were used with the open method.

You can do something more useful with method and url than simply passing them into console.log if you wish.

Community
  • 1
  • 1
apsillers
  • 112,806
  • 17
  • 235
  • 239
0

possible dublicate How to read from Chrome's console in JavaScript

Community
  • 1
  • 1
TypedSource
  • 708
  • 4
  • 12
  • I suspect this is question isn't well-aligned as a duplicate of that question, since this question asks about a subset of console logging that is directly tied to a JS-visible event. That is, the answer here is probably "override `XMLHttpRequest.prototype.send` to bind a `load` listener to all XHR objects" whereas that's not an answer for the general problem of reading console output. It's more likely a duplicate of [Add a “hook” to all AJAX requests on a page](http://stackoverflow.com/q/5202296/710446) but I can't be sure without further clarification from the OP – apsillers Apr 07 '17 at 16:39
  • i think there are not enough information about whats realy wanted by the question. definitly it's not possible to get access to the console of the browser. if he/she want's to capture the events he has to it with the basic event listeners – TypedSource Apr 07 '17 at 16:45
  • @TypedSource I'd like to retrieve the URLs being called by the html resource. It is from a different host, so there is a CORS restriction as well. – compsciguy. Apr 07 '17 at 17:04
  • the console has a trace function. but from outside you can not access this is think. – TypedSource Apr 07 '17 at 17:12