title: Capture window.postMessage in Swift WKWebView
body: <p>I am developing a swift ios app that uses <code>WKWebView</code> to load up an ecommerce site.<br>
  When a user purchases a product here, the checkout page allows the user to pay in cryptocurrency.  </p>
  
  <p>When the user clicks "Open in Wallet", the site shoots off a <code>
  window.postMessage(paymentData)</code> 
  where payment data is a js object with a bitcoin url in it. </p>
  
  <p>I am using a <code>WKUserScript</code> with <code>WKWebConfiguration</code> to inject a script that listens for a window message and then fires off data to my <code>webkit.messageHandler.</code>  </p>
  
  <pre><code>let source = """
      window.addEventListener('message', function(e) { window.webkit.messageHandlers.iosListener.postMessage(JSON.stringify(e.data)) } )
      """
  </code></pre>
  
  <p>Unfortunately this code never triggers.</p>
  
  <p>When I use chrome or safari devtools to inject the same javascript, it works just fine.  </p>
  
  <p>I have scoured stack overflow to see if there is a special condition for <code>window.postMessage</code> in <code>WKWebView</code> but have had no luck thus far. </p>
  
  <p>Is it possible to capture a <code>window.postMessage()</code> event and pipe the event data back to my ios app?</p>
  
  <p>Thanks in advance!!!!
  Here is my existing code.</p>
  
  <pre><code>  let webConfiguration = WKWebViewConfiguration()
      let source = """
      window.addEventListener('message', function(e) { window.webkit.messageHandlers.iosListener.postMessage(JSON.stringify(e.data)) } )
      """
      let script = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: false)
      userContentController.addUserScript(script)
  
      userContentController.add(self, name: "iosListener")
      webConfiguration.userContentController = userContentController
      webView = WKWebView(frame: .zero, configuration: webConfiguration)
      webView.uiDelegate = self
      webView.navigationDelegate = self
      webView.translatesAutoresizingMaskIntoConstraints = false
      view.addSubview(webView)
  </code></pre>
  
  <pre><code> func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
      print("message body: \(message.body)")
      print("message frameInfo: \(message.frameInfo)")
    }
  </code></pre>