从InAppBrowser PhoneGap获取HTML?

4

我是一个在 PhoneGap 上开发应用的开发者。当我点击 InAppBrowser 工具栏上的关闭按钮时,需要从 InAppBrowser 中获取 HTML 内容,但是代码并没有生效。

这是我的代码:

var handleClose = function() {
    // get content html from here
}

ref.addEventListener('exit', handleClose);

有人能帮我吗?


你是否已经触发了 handleClose 函数?请指明:哪里出了问题 - SweetWisher ツ
什么出了问题?是获取HTML还是方法执行? - AtanuCSE
是的,函数handleClose工作正常,但我需要获取HTML内容。 - quang dung
什么是应用内浏览器?你想要得到什么?请具体说明。分享更多的代码。 - SweetWisher ツ
2个回答

10
为了帮助以后的任何人,我刚偶然发现了同样的问题并找到了解决方案。

var inAppRef = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
inAppRef.addEventListener('loadstop', function() {
  inAppRef.executeScript({
    code: "document.getElementsByTagName('html')[0].innerHTML"
  }, function(html) {
    alert(html);
  });
});

请记住,在使用 PhoneGap / Cordova 时,需要引用和包含 InAppBrowser。


4

在Ralphonzo的回答基础上,对于目前版本的in-app-browser(v3.0.0)和ionic(v4.6.0)以及cordova(v7.1.0)和Angular(v7.2.8),我使用以下方法:

ngOnInit() {
  this.browser = this.iab.create(domain + sso_url, '_blank', {});

  this.browser.on('loadstart').subscribe((event: InAppBrowserEvent) => {
    this.browser.executeScript({code: 'document.getElementsByTagName("html")[0].innerHTML'}).then( html => { console.log(html) });
  }
}

在我的情况下,console.log 从未显示。有什么想法吗?Android 设备,iab 3.2 ionic 4... - Hanzo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接