Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute JS from Python, that uses 'Document' and/or 'Window'

I am currently working on getting JavaScript to execute successfully from within Python. I have implemented a JS engine (v8) using the PyV8 package. From here I can execute primitive JavaScript ("1+2", etc). However, for JavaScript that uses references to "document" or "window" the code will throw an error. I am looking, ideally, for a Python implementation however a JavaScript implementation would work as well as I could prepend it to my script before executing it what my JavaScript engine.

To summarize: How can I execute JavaScript, that uses 'Document' and/or 'Window', from within JavaScript?

like image 591
Julio Avatar asked Oct 20 '25 13:10

Julio


1 Answers

I was having the same problem when using Spidermonkey (a command-line JavaScript interpreter) and trying to run a script that relied on the non-existent document and window objects.

I solved it by using the Env-JS project, which sets up independent "fake" objects for them.

like image 89
amphetamachine Avatar answered Oct 23 '25 05:10

amphetamachine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!