Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rafaeldelrey opened this issue Dec 7, 2022 · 2 comments

Comments

@rafaeldelrey
Copy link

No description provided.

@worstperson
Copy link
Collaborator

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")''')

@rafaeldelrey
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants