       delete tabLoadedHandlers[tabId]
 
   initializeTopFrameUIComponents: ({tabId}) ->
-    topFramePortForTab[tabId].postMessage handler: "initializeTopFrameUIComponents"
+    portsForTab[tabId][0]?.postMessage handler: "initializeTopFrameUIComponents"
 
 handleFrameFocused = ({tabId, frameId}) ->
   frameIdsForTab[tabId] ?= []

Frames =
  onConnect: (sender, port) ->
    [tabId, frameId] = [sender.tab.id, sender.frameId]
    topFramePortForTab[tabId] = port if frameId == 0
    frameIdsForTab[tabId] ?= []
    frameIdsForTab[tabId].push frameId unless frameId in frameIdsForTab[tabId]
    portsForTab[tabId] ?= {}

frameIdsForTab = {}
portsForTab = {}
root.urlForTab = {}

# This is exported for use by "marks.coffee".
root.tabLoadedHandlers = {} # tabId -> function()
