如何让 cordova inappbrowser 等到加载 iframe

How to make cordova inappbrowser wait until iframe is loaded

AFAIK,addEventListener('loadstop', ...),InAppBrowser API 之一,不适用于 iframe。

我需要由 executeScript 执行的脚本,等待 iframe 加载完毕。我同意术语 'loaded' 含糊不清,因此可以将其视为 iframe 元素的 onload 事件。但我不知道如何注册事件处理程序(在 executeScript 中)并从中检索 return 值。因为executeScript将最后一条语句的值传递给了它的回调,所以我觉得没有办法传到外面去。

ref.executeScript({code:String.raw`
   document.iframe.addEventListener("load", function(){
       //How can I pass this outside executeScript?
       document.iframe.contentDocument.innerHTML
   })
`})

那么等待 iframe 加载的正确方法是什么

您可以通过 "polling" 来加载值:

一次:

ref.executeScript({code:String.raw`
   window.iframeContent = null;
   document.iframe.addEventListener("load", function(){
       window.iframeContent = document.iframe.contentDocument.innerHTML;
   })
`})

然后重复调用以下 executeScript 直到获得一个值(使用 setInterval 或类似方法):

ref.executeScript({code:String.raw`
   return window.iframeContent;
`})