简体   繁体   English

如何在离子 3/4 中获取 IFRAME url 更改事件?

[英]How to get IFRAME url change event in ionic 3/4?

My website is https://www.example.com/write , I want to use this page as IFRAME in ionic 4 application, I am already embedded.我的网站是https://www.example.com/write ,我想将此页面用作 ionic 4 应用程序中的 IFRAME,我已经嵌入了。 But I want URL change event of IFRAME, while I am working inside iframe in my app.但是我想要 IFRAME 的 URL 更改事件,而我在我的应用程序中的 iframe 内工作。 For example, inside IFRAME, I have "Publish" button, on that button, I am redirecting page, but I want that click event in my ionic 4 app, that user has clicked "Publish" button, or if I can get the new url, using that I can get user get back to the app.例如,在 IFRAME 中,我有“发布”按钮,在该按钮上,我正在重定向页面,但我希望在我的 ionic 4 应用程序中有该点击事件,该用户点击了“发布”按钮,或者我是否可以获得新的url,使用它我可以让用户回到应用程序。

Here is sample code:这是示例代码:

<ion-header>
  <ion-toolbar class="gradient-header font">
    <ion-title style="background: transparent;" class="light-back small-text-header">Write Story</ion-title>
    <ion-buttons slot="end">&nbsp;</ion-buttons>
  </ion-toolbar>
</ion-header>


<ion-content>
  <ion-grid fixed style="padding-right:0;">
    <ion-row style="padding-right:0;">
      <ion-col style="padding-right:0;">
        <iframe [src]="urlSafe" style="width: 100%; height: 100vh;border:none;"></iframe>
      </ion-col>
    </ion-row>
  </ion-grid>
</ion-content>

Here is TS file : write.page.ts这是 TS 文件:write.page.ts

import { Component, OnInit } from '@angular/core';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
import { Observable } from "rxjs";

@Component({
  selector: 'app-write',
  templateUrl: './write.page.html',
  styleUrls: ['./write.page.scss'],
})
export class WritePage implements OnInit {
  urlSafe: SafeResourceUrl;
  data:Observable<any>;
  n_link:string = 'https://www.example.com/write';

  constructor(public sanitizer: DomSanitizer) { }

  ngOnInit() {
    this.urlSafe= this.sanitizer.bypassSecurityTrustResourceUrl(this.n_link);
  }

}

It looks like you need to use window.postMessage instead of detecting url changes of your iframe.看起来您需要使用window.postMessage而不是检测 iframe 的 url 更改。 As you can see below you can detect messages from your iframe:正如您在下面看到的,您可以检测来自 iframe 的消息:

// Called sometime after postMessage is called
function receiveMessage(event)
{
  // You can validate the origin of the message for security concerns
  if (event.origin !== "http://example.com:8080")
    return;

  console.log(event.data) //Message sent from an external page of your iframe
}

window.addEventListener("message", receiveMessage, false);

And Sending a message from your external page loaded into the iframe:并从加载到 iframe 的外部页面发送消息:

window.postMessage('Sending a message when Publish button is pressed')

Happy coding!快乐编码! <3 <3

Tracking of events inside iFrames is very restricted in cross-domain sites.在跨域站点中,跟踪 iFrame 内的事件非常受限。

In order to reliably track individual events, you should have access to the content of the page you are accessing and then send the event information back to your parent app with window.postMessage为了可靠地跟踪单个事件,您应该有权访问您正在访问的页面的内容,然后使用window.postMessage将事件信息发送回您的父应用程序

However, there is a way to detect if the iframe has been clicked.但是,有一种方法可以检测 iframe 是否已被点击。 Depending on your use case, this might or might not be enough for you, but if you are tracking simple interactions it might be enough.根据您的用例,这对您来说可能还不够,但如果您正在跟踪简单的交互,这可能就足够了。 This is achieved by tracking which element is active in your HTML.这是通过跟踪 HTML 中哪个元素处于活动状态来实现的。

var monitor = setInterval(function(){
    var elem = document.activeElement;
    if(elem && elem.tagName == 'IFRAME'){
        clearInterval(monitor);
        alert('clicked!');
    }
}, 100);

You can see a working example here: JSFiddle你可以在这里看到一个工作示例: JSFiddle

A lengthy discussion on the topic here (just don't pay attention to the accepted reply, it is outdated)关于这个话题的冗长讨论here (只是不要注意接受的回复,它已经过时了)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM