Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compile code ClojureScript for use in PhantomJS?

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?

  • [1] https://github.com/sgentle/phantomjs-node
  • [2] http://phantomjs.org/api/webpage/method/evaluate.html
like image 338
branch14 Avatar asked Jul 27 '15 17:07

branch14


1 Answers

You need to use js/document instead of document. document is a reference to clojurescript variable, js/document is a document in js world.

like image 74
edbond Avatar answered Oct 11 '22 15:10

edbond