Access iframe from route function after iframe has been rendered on page

hi!

it might not be the best way to go for in the end, but:

i am rendering an iframe object that i get back from an api call. i’m using the following route function for it, and what i’d like to do after i return the data, is to access via php’s DOM library, the iframe’s #document and grab a link from it.

should i make a new route with the same pattern and put it right after this one below?

<?php

include('vendor/rmccue/requests/library/Requests.php');
Requests::register_autoloader();

kirby()->routes([
[
    'method' => 'GET',
    'pattern' => 'mxc-latest-em',
    'action' => function () {
      $root_url = 'https://api.mixcloud.com/';
      $base_url = 'OFOUT/';
      $em_html = 'embed-html/';
      
      $r = Requests::get($root_url . $base_url . '/abstractism-4-20052018/' . $em_html);
      $data = $r->body;

      return new Response($data);
    }
  ]
  ]);

};

or how should i handle the rendering of the data?

thanks!
af

Is it not possible to get what you need from the JSON data instead of the iframe? Or is that information only available within the iframe?

Did you know you can use the Toolkit’s Remote class instead of loading another one?

$r = remote::request($root_url . $base_url . '/abstractism-4-20052018/' . $em_html);
$data = $r->content;

Edit:

A new route with the same pattern would overwrite your first route, so that doesn’t make sense.

I think your best option is to use JavaScript to get the information from the rendered page. Other than that, the only other way would probably be to get the iframe src attribute and then make a remote request to this URL, then use DOM Document to get the required link. Sounds like a lot of effort…

Wondering what link it could be that you’d like to get from the iframe, there don’t seem to be that many that could be of any interest and not calculated from the information you already pass to make your remote request :thinking:

thanks!

yes, i know it’s a bit of a bizarre idea.

what i’m trying to get from the iframe is the src of the audio tag, and build a new custom player around it (mixcloud provides a library to control the embedded player and i hope i can use the same library to control only the <audio src> i’m trying to get from the api call).

af

But the audio tag is only injected into the DOM once you start playing?

no, but it lives within the iframe.

and when i call the mixcloud api to get the embed code, i got an html callback with only the iframe code. so i need to somehow render that iframe before being able to jump inside it and catch the audio tag…

What. I’m trying to say is that. there. is no audio tag. when the iframe. is. rendered. but only injected once you start. playing.

:sweat:

sorry. my mind was looping over some other things.