injectme.coffee 739 B

1234567891011121314151617181920212223
  1. # Use 'page.injectJs()' to load the script itself in the Page context
  2. if phantom?
  3. page = require('webpage').create()
  4. # Route "console.log()" calls from within the Page context to the main
  5. # Phantom context (i.e. current "this")
  6. page.onConsoleMessage = (msg) -> console.log(msg)
  7. page.onAlert = (msg) -> console.log(msg)
  8. console.log "* Script running in the Phantom context."
  9. console.log "* Script will 'inject' itself in a page..."
  10. page.open "about:blank", (status) ->
  11. if status is "success"
  12. if page.injectJs("injectme.coffee")
  13. console.log "... done injecting itself!"
  14. else
  15. console.log "... fail! Check the $PWD?!"
  16. phantom.exit()
  17. else
  18. alert "* Script running in the Page context."