0

I'm working with a very limited web platform which makes use of 3rd party Javascript libraries. Without listing the extraneous details of how and why it operates the way it does, I'm curious if it is possible to do something/anything (e.g. execute Javascript function) after a specific console.log entry appears. The system is very locked down but I'm trying to improve the overall user experience of the application.

A typical console log entry from this Javascript library usually looks like this:

data access_token=XXXXXXXXXXXXXXXX&type_name=user&attributes=["uuid","displayName","email"]

Googling yields a large amount of unrelated results. I found this which involves overwriting the console.log method but that's not my desired approached: How to get the console.log content as string in JavaScript

Any ideas? I'm assuming you convert the current contents into an array or string and parse for results.

Community
  • 1
  • 1
sparecycle
  • 2,038
  • 5
  • 31
  • 58
  • 1
    why not overriding console.log? – Stepan Yakovenko Mar 28 '16 at 23:55
  • That looks like it might be the only option. I was just hoping to leave the console.log entries in place for other developers on this system. I believe I can do that without hindering the console.log() entries they may have setup. – sparecycle Mar 28 '16 at 23:56

1 Answers1

1

You can override console.log() AND, keep it still logging to the console so you can both capture the output and leave logging in place.

Here's a way to do that:

var logEntries = [];

(function(oldLog) {

    console.log = function(arg) {
        // captures only first argument to console.log()
        // could be expanded to concatenate all entries
        logEntries.push(arg.toString());

        // now call original console.log() with original arguments
        return oldLog.apply(console, arguments);
    };
})(console.log);

So, now at any given time, the logEntries variable will be an accumulated array of everything sent to console.log(). This is a simple version that does not expand objects or enumerate arrays. More features in that area could be added, but it appears you're just looking for a specific string.

You could also add any filtering before adding to the logEntries array if you only want to capture things containing "access_token", for example.


If you wanted to only capture the lines that contain "access_token=", then you could modify the above code like this:

var logEntries = [];

(function(oldLog) {

    console.log = function(arg) {
        if (typeof arg === "string" && arg.indexOf("access_token=") !== -1) {
            logEntries.push(arg);
        }

        // now call original console.log() with original arguments
        return oldLog.apply(console, arguments);
    };
})(console.log);
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • @sparecycle - Did you get a solution to your issue? If so, please mark this (or some other answer) as your selected answer by checking the green checkmark to the left of the answer and this will indicate to the community that your question has been answered and will earn you some reputation points that can earn you more privileges over time here. – jfriend00 Apr 01 '16 at 00:05