I'm trying to script PhantomJS in ClojureScript. I'm targeting Node and using phantomjs-node [1]. I have a basic example working:
(def phantom (js/require "phantom"))
(defn -main [& args]
  (-> phantom
    (.create (fn [browser]
      (-> browser
        (.createPage (fn [page]
          (-> page
            (.open "http://google.com" (fn [status]
              (if (= status "success")
                (-> page (.render "example.png")))
              (-> browser .exit)))))))))))
Now, if I use evaluate function [2] of PhantomJS webpage object, I the following error:
phantom stdout: ReferenceError: Can't find variable: <namespace here>
When compiling into JavaScript the code to be evaluated contains the CLJS namespace and hence doesn't properly eval within the context of PhantomJS' webpage object. Here is an example:
(defn -main [& args]
  (-> phantom
    (.create (fn [browser]
      (-> browser
        (.createPage (fn [page]
          (-> page                                                                                      
            (.open "http://google.com" (fn [status]
              (println (str "opened google? " status))
                (-> page
                  (.evaluate #(-> document .-title)
                    #(do
                      (println (str "Page title is " %))
                      (-> browser .exit))))))))))))))                 
How can I prevent the code to be evaluated within the webpage object of PhantomJS from being namespaced with the CLJS namespace? Or secondly do I have any other options?
You need to use js/document instead of document.
document is a reference to clojurescript variable, js/document is a document in js world.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With