Js2Py icon indicating copy to clipboard operation
Js2Py copied to clipboard

Is there a way to capture console output from running javascript code?

Open rafaeldelrey opened this issue 1 year ago • 2 comments

rafaeldelrey avatar Dec 07 '22 05:12 rafaeldelrey

Not entirely sure without an example, maybe override the function in Javascript and save the argument(s) to a variable to access later:

var _console_logs = [];
var _console_log = console.log
console.log = function(...args) {
    _console_logs.push(args);
    _console_log(...args);
}

Or just replace the log function with your own arbitrary Python code?

import js2py
context = js2py.EvalJs()
context.console.log = lambda *args: print(', '.join(str(arg) for arg in args))

context.execute('''console.log("test")''')

worstperson avatar Dec 07 '22 06:12 worstperson

I am running a 3rd party javascript code (an algorithm for an artificial pancreas system on Android). I m reading the js file and replacing all calls to console.log to my own console_log function, which I append to the script content. This way I m able to hide the console, but I was thinking that maybe there would be a more official way of doing it.

rafaeldelrey avatar Dec 08 '22 05:12 rafaeldelrey