Don't use window and document

With this change, our JavaScript can also be executed by
non-brower engines.