   if (summary.canMerge && summary.status == "pending")
     addButton summary, $('.commit-form-actions')
 
-# Github doesn't do full reloads on state changes, but rather uses push
+# Github doesn't do full reloads on state changes, but rather uses pushState
+# poll for that
+callAtLatest = (timeout, handler) ->
+  canHappen = true
+  happened = false
+  setTimeout(->
+    canHappen = false
+    if !happened
+      console.log "it didn't happen!"
+      handler()
+  , timeout)
+  (args...) ->
+    if canHappen
+      console.log "called", args, $("title").text()
+      canHappen = false
+      happened = true
+      handler args...
+
 onPageChange = (callInitial, callback) ->
   if !callback?
     callback = callInitial
     callInitial = false
-  oldUrl = if callInitial then undefined else document.URL
+  title = -> $("title").text()
+  resolve = ->
+    oldTitle = title()
+    callback()
+
+  if callInitial
+    callback()
+
+  oldUrl = document.URL
+  oldTitle = title()
   setInterval ->
     if document.URL != oldUrl
+      # Wait until the title changes or up to 5 seconds before continuing
+      # This should fix the race if a page loads slowly
+      if title() == oldTitle
+        $("title").one 'DOMSubtreeModified', callAtLatest(5000, resolve)
+      else
+        resolve()
       oldUrl = document.URL
-      callback()
   , 400
 
 onPageChange true, ->
   console.debug("page changed", document.URL)
+  $(".extn-lgtm").remove()
   if isPullRequestPage()
     updateSite()
-  else
-    $(".extn-lgtm").remove()
 
 if location.search == "?mergenow"
   $("form.merge-branch-form").submit()

'use strict'

isPullRequestPage = (url) ->
  /github\.com\/.*\/.*\/pull\/\d+\/?$/.test(url || document.URL)

sendTask = (summary) ->
  message =


addButton = (summary, $parent) ->
  button = $('''
    <button class="btn btn-outline extn-lgtm" style="float: left;">
      <span class="octicon octicon-primitive-dot text-pending"></span>
      Merge when tests pass
    </button>
