Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get IFrame img in angular 6

I have an iframe in my Angular app. The iframe has an image in base64 format. Now I am trying to get that image’s base64 code and store it in a variable in the parent page/Angular app. Can anyone assist, please? I found this article

How to get data from Iframe with Angular 6

Here is my HTML code for the iframe

<iframe src="http://eohdigitalappstore.co.za/test/test.html"></iframe>

I managed to get a bit further by being able to get elements in the iframe page but am now trying to get to the image in the elements tree

const doc =  this.iframe.nativeElement.contentDocument || this.iframe.nativeElement.contentWindow;
console.log(doc.body);

I can get to point in the HTML elements tree which looks like below enter image description here

like image 344
skydev Avatar asked Dec 04 '18 05:12

skydev


People also ask

Can I use iframe in angular?

The on-load trigger in the iframe tag calls the necessary code for this. Data binding is done by using the instance property and both Input and Output emitters can be handled accordingly. This way even while inside an iframe the angular application is able to reference its components.

How do I capture an iframe event?

Answer: Use the jQuery contents() method If you try to detect or capture a click event inside an iframe simply using jQuery click() method it will not work, because iframe embed a web page within another web page. However, you can still do it on the same domain utilizing the jQuery contents() and load() method.

How do I use iframe tags?

Definition and UsageThe <iframe> tag specifies an inline frame. An inline frame is used to embed another document within the current HTML document. Tip: Use CSS to style the <iframe> (see example below). Tip: It is a good practice to always include a title attribute for the <iframe> .

How do I check if an element is an iframe?

In short, to check if a page is in an iframe, you need to compare the object's location with the window object's parent location. If they are equal, then the page is not in an iframe; otherwise, a page is in an iframe.


1 Answers

Use a view child, it will give you ElementRef from which you can access a nativeElement object exactly same as you have it in a normal JavaScript application.

Example (source):

import { Component, OnInit, ViewChild, ElementRef, AfterViewInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {

  @ViewChild('iframe') iframe: ElementRef;

  ngAfterViewInit() {
    const nativeEl =  this.iframe.nativeElement;
    if ( (nativeEl.contentDocument || nativeEl.contentWindow.document).readyState === 'complete' )        {
        nativeEl.onload = this.onIframeLoad.bind(this);
    } else {
      if (nativeEl.addEventListener) {
        nativeEl.addEventListener('load', this.onIframeLoad.bind(this), true);
      } else if (nativeEl.attachEvent) {
        nativeEl.attachEvent('onload', this.onIframeLoad.bind(this));
      }
    }
  }


  onIframeLoad() {
    const base64String = this.iframe.nativeElement.contentWindow.document.body.innerHTML;
    console.log(this.iframe.nativeElement.contentWindow.document.body.children[1].currentSrc);
  }
}

Additional interesting readings: https://aakashgoel.com/capturing-the-load-event-of-an-iframe-a-case-study-1a3761c871fe

Note: All this is possible if you observe the same-origin policy.

like image 193
Ravinder Payal Avatar answered Oct 16 '22 07:10

Ravinder Payal