0

I am trying to extract the content of the console and have it sent as a pop up using javascript. The reason for this is I have lots of information being written to the console but I am unable to read the console in a particular environment. I have attempted to follow the answer to this question but have gotten errors: Can I extend the console object (for rerouting the logging) in javascript? Here is my code followed by the errors I have gotten:

<p id="output"></p>

<script>
    console.log("asdf")

    (function() {
        var exLog = console.log;
        console.log = function(msg) {
            exLog.apply(this, arguments);
            alert(msg);
        }
    })()

</script>

this is the error

Uncaught TypeError: console.log(...) is not a function

Thanks for the help

Thanks for getting rid of the errors, as I stated in the question my goal is to be able to read errors that occur while the program runs. here is an example of what I want:

<script>


    function func(){
        return y
    }

    (function() {
        var exLog = console.log;
        console.log = function(msg) {
            exLog.apply(this, arguments);
            alert(msg); 
        }
    })()


    func()


</script>

I want this to have the error that occurs to pop up
(the error is Uncaught ReferenceError: y is not defined)

thanks

Mathew
  • 894
  • 1
  • 17
  • 39
  • 3
    How about calling `console.log` **after** defining it? – Adelin Jan 16 '18 at 08:20
  • Im not sure what you mean – Mathew Jan 16 '18 at 08:20
  • Thanks for getting it working so far, I want to be able to get the errors written to the pop up though – Mathew Jan 16 '18 at 08:28
  • What is the environment in which you run your code? I tested it in Chrome and after redefining the `console.log` function as you did, I get the message logged and alerted. – Adelin Jan 16 '18 at 08:30
  • I am currently running in chrome – Mathew Jan 16 '18 at 08:31
  • Your other error is an entirely different error. What do you expect to get with `func()`? You should add another question for it – Adelin Jan 16 '18 at 08:31
  • ok, I'll make a new question, the point of func was to intentionally cause an error to test if i can write errors from console to the pop up – Mathew Jan 16 '18 at 08:32
  • my other question: https://stackoverflow.com/questions/48277040/writing-javascript-console-errors-to-a-popup – Mathew Jan 16 '18 at 08:37

2 Answers2

1

I can reproduce your error.

But if I call console.log after defining it, it works as you expect

<script>


    (function() {
        var exLog = console.log;
        console.log = function(msg) {
            exLog.apply(this, arguments);
            alert(msg);
        }
    })()

    console.log("asdf"); // calling after defining it

</script>
Adelin
  • 6,791
  • 5
  • 33
  • 58
0

Your code works as is, just change where you call console.log() (put it after)

(function() {
    var exLog = console.log;
    console.log = function(msg) {
        exLog.apply(this, arguments);
        alert(msg);
    }
})();

console.log("hello")
Tallboy
  • 11,480
  • 11
  • 67
  • 151