3

I'm tried to put a iframe inside Ionic page but with no success

the src comes from a json value

<iframe src="{{PostContent.link}}"></iframe>

I got an error

ERROR Error: unsafe value used in a resource URL context

Edit: I included my ts page ts page

import { Component, ViewChild } from '@angular/core';
import { ApiProvider } from '../../providers/api/api.service';
import {  ActivatedRoute } from '@angular/router';


  postid: string;
PostContent:any = [];
  theInAppBrowser: any;

  constructor(
        public api:ApiProvider,
private route: ActivatedRoute,

  ) {

    this.postid = this.route.snapshot.paramMap.get('id');

   }



   getPostContent(){


      this.api.get('doctor/' + this.postid  ).subscribe((data) => {
        this.PostContent = data;


        });


  }



 ngOnInit() {

    this.getPostContent();
}


}
BabyDriver
  • 183
  • 2
  • 15

2 Answers2

0

You should to use DomSanitizer for displaying that URL. This will enable you to create an exception for displaying the URL you want in the iFrame.

You can create a new variable and assign it the sanitized value, then display it in the HTML like this:

Warning! Using bypassSecurityTrustResourceUrl might expose your page to XSS attacks. You should minimize and control this as much as possible. Check the documentation to ensure that you are protecting yourself properly when using DomSanitizer: https://angular.io/api/platform-browser/DomSanitizer

HTML

<iframe [src]="sanitizedURL"></iframe>

JS

  //Wherever you are setting the URL, use DomSanitizer. For example:

      var URL = this.PostContent.link;

      // Use sanitizer for URL
      this.sanitizedURL = this.domSanitizer.bypassSecurityTrustResourceUrl(URL);       

Your JS full code, including new imports

    import { Component, ViewChild } from '@angular/core';
    import { ApiProvider } from '../../providers/api/api.service';
    import {  ActivatedRoute } from '@angular/router';

    //Import SafeResourceURL & DomSanitizer
    import { SafeResourceUrl, DomSanitizer } from '@angular/platform-browser';

  @Component({
    selector: 'iFrameComponent',
    templateUrl: './iFrameComponent.component.html',
    styleUrls: ['./iFrameComponent.component.scss'],
  })

  export class iFrameComponent {

    //Declare URL variable
      sanitizedURL: SafeResourceUrl;

      postid: string;
     PostContent:any = [];
      theInAppBrowser: any;

      constructor(
            public api:ApiProvider,
            private route: ActivatedRoute,
            public domSanitizer: DomSanitizer,

      ) {

        this.postid = this.route.snapshot.paramMap.get('id');

       }



       getPostContent(){


          this.api.get('doctor/' + this.postid  ).subscribe((data) => {
            this.PostContent = data;

              //Set URL
              var URL = this.PostContent.link;

              // Use sanitizer for URL
              this.sanitizedURL = this.domSanitizer.bypassSecurityTrustResourceUrl(URL);


            });


      }
cglegaspi
  • 73
  • 5
0

You need to use DomSanitizer for iframe src for that, you need to create a pipe using angular CLI

ng generate pipe safe

Open safe.pipe.ts file and paste code below:

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeHtml, SafeStyle, SafeScript, SafeUrl, SafeResourceUrl } from '@angular/platform-browser';

@Pipe({
  name: 'safe'
})
export class SafePipe implements PipeTransform {

  constructor(protected sanitizer: DomSanitizer) {}

 public transform(value: any, type: string): SafeHtml | SafeStyle | SafeScript | SafeUrl | SafeResourceUrl {
    switch (type) {
            case 'html': return this.sanitizer.bypassSecurityTrustHtml(value);
            case 'style': return this.sanitizer.bypassSecurityTrustStyle(value);
            case 'script': return this.sanitizer.bypassSecurityTrustScript(value);
            case 'url': return this.sanitizer.bypassSecurityTrustUrl(value);
            case 'resourceUrl': return this.sanitizer.bypassSecurityTrustResourceUrl(value);
            default: throw new Error(`Invalid safe type specified: ${type}`);
        }
  }
}

After that you just need to use safe pipe like this:

<iframe src="{{PostContent.link | safe: 'url'}}"></iframe>

Anwarul Islam
  • 561
  • 7
  • 29