             { port1, port2 } = new MessageChannel
             @iframeElement.contentWindow.postMessage vimiumSecret, chrome.runtime.getURL(""), [ port2 ]
             port1.onmessage = (event) =>
-              switch event?.data?.name ? event?.data
+              eventName = null
+              if event
+                eventName = (if event.data then event.data.name) || event.data
+              switch eventName
                 when "uiComponentIsReady"
                   # If any other frame receives the focus, then hide the UI component.
                   chrome.runtime.onMessage.addListener ({name, focusFrameId}) =>
-                    if name == "frameFocused" and @options?.focus and focusFrameId not in [frameId, @iframeFrameId]
+                    if name == "frameFocused" and @options and @options.focus and
+                        not [frameId, @iframeFrameId].includes(focusFrameId)
                       @hide false
                     false # We will not be calling sendResponse.
                   # If this frame receives the focus, then hide the UI component.
                   window.addEventListener "focus", (forTrusted (event) =>
-                    if event.target == window and @options?.focus
+                    if event.target == window and @options and @options.focus
                       @hide false
                     true # Continue propagating the event.
                   ), true

      # Firefox doesn't support createShadowRoot, so guard against its non-existance.
      # https://hacks.mozilla.org/2018/10/firefox-63-tricks-and-treats/ says
      # Firefox 63 has enabled Shadow DOM v1 by default
      @shadowDOM = shadowWrapper.attachShadow?( mode: "open" ) ?
        shadowWrapper.createShadowRoot?() ? shadowWrapper
      @shadowDOM.appendChild styleSheet
      @shadowDOM.appendChild @iframeElement
      @toggleIframeElementClasses "vimiumUIComponentVisible", "vimiumUIComponentHidden"

class UIComponent

  constructor: (iframeUrl, className, @handleMessage) ->
    @iframeElement = null
    @iframePort = null
    @showing = false
    @iframeFrameId = null
    # TODO(philc): Make the @options object default to {} and remove the null checks.
    @options = null
    @shadowDOM = null

    DomUtils.documentReady =>
      styleSheet = DomUtils.createElement "style"
      styleSheet.type = "text/css"
