                 document.body.dataset.youtubeCommentScraper = "ready"
 
           .then ->
-            console.log "ge"
             # Check data-youtube-comment-scraper is ready, which means all pages
             # are loaded and all collapsed comments are expanded.
             # If not, wait more 1000 msec.

  new Promise (resolve, reject) ->

    get_or_create_phantom().then (ph) ->
      console.log ph
      ph.createPage().then (page) ->

        page.open(url)
          .then (status) ->
            console.log status
            # Check loaded page has header section.
            # If not, wait more 1000 msec.
            new Promise (resolve, reject) ->

  else
    0

phantom_instance = null
###
PhantomJS instances.
###

get_or_create_phantom = do ->
  ###
  Get an instance of PhantomJS.

  If there are no instances, this function creates it.
  ###
  locked = false

  _get_or_create_phantom = ->
    locked = true
    if phantom_instance?
      new Promise (resolve) ->
        resolve phantom_instance
        locked = false
    else
      phantom.create().then (instance) ->
        phantom_instance = instance
        locked = false
        return instance

  ->
    if locked
      new Promise (resolve) ->
        do wait = ->
          if locked
            setTimeout wait, 100
          else
            resolve _get_or_create_phantom()

    else
      _get_or_create_phantom()


delete_phantom = ->
  ###
  Delete PhantomJS instance.

  It is safe to call this method many times.
  ###
  if phantom_instance?
    phantom_instance.exit()
    phantom_instance = null


# Register delete_phantom method so that it will be called when
