Skip to content

Instantly share code, notes, and snippets.

@Moonbase59
Created December 22, 2023 11:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Moonbase59/fbe49e4be4ea9e833f29e94c8526455f to your computer and use it in GitHub Desktop.
Save Moonbase59/fbe49e4be4ea9e833f29e94c8526455f to your computer and use it in GitHub Desktop.
Responsive AzuraCast Embed Widget iframes
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Iframe widget resizing test</title>
</head>
<body>
<h1>Iframe widget resizing test</h1>
<!-- Show what’s happening -->
<p>Window width: <span id="ww"></span>, height: <span id="wh"></span></p>
<p>Iframe width: <span id="iw"></span>, height: <span id="ih"></span></p>
<!-- Put embed widget code from station here, remove its "min-height" styling -->
<!-- Use id="widget" to simplify test -->
<!-- We use the "Request" widget as an example: switch it between 10/25 items/page -->
<iframe id="widget" src="https://demo.azuracast.com/public/azuratest_radio/embed-requests?theme=light" frameborder="0" allowtransparency="true" style="width: 100%; border: 0;"></iframe>
<hr/>
<p>Some page footer here.</p>
<script>
// wrap in an IIFE so not to pollute namespaces
(function () {
let iframe = document.querySelector("#widget");
let ww = document.querySelector("#ww");
let wh = document.querySelector("#wh");
let iw = document.querySelector("#iw");
let ih = document.querySelector("#ih");
// initial display
ww.textContent = window.innerWidth;
wh.textContent = window.innerHeight;
iw.textContent = iframe.clientWidth;
ih.textContent = iframe.clientHeight;
// AzuraCast widgets give us a postMessage containing their actual height and width
// starting at Rolling Release #8b15a55 (2023-12-21 23:00)
window.addEventListener('message', function(e) {
// message passed from iframe page has .height and .width
let message = e.data;
// only adjust height here, we have set "width: 100%;" on iframe
iframe.style.height = message.height + 'px';
//iframe.style.width = message.width + 'px';
// show it
ih.textContent = iframe.clientHeight;
});
// handle window resize
window.addEventListener("resize", function(e) {
console.log("Resize: width", window.innerWidth, "height", window.innerHeight);
ww.textContent = window.innerWidth;
wh.textContent = window.innerHeight;
iw.textContent = iframe.clientWidth;
ih.textContent = iframe.clientHeight;
});
// end wrapper
})();
</script>
</body>
</html>
<script>
// AzuraCast widgets give us a postMessage containing their actual height and width
// starting at Rolling Release #8b15a55 (2023-12-21 23:00)
// wrap in an IIFE so not to pollute namespaces
(function () {
let iframe = document.querySelector("#widget");
window.addEventListener('message', function(e) {
// message passed from iframe page has .height and .width
let message = e.data;
// only adjust height here, we have set "width: 100%;" on iframe
iframe.style.height = message.height + 'px';
//iframe.style.width = message.width + 'px';
});
})();
</script>
@Moonbase59
Copy link
Author

Moonbase59 commented Dec 22, 2023

Responsive AzuraCast Embed Widget iframes

AzuraCast has implemented a postMessage in the iframe embed widgets starting from Rolling Release #8b15a55 (2023-12-21 23:00).

This allows using the station embed widgets (Player, Song History, Schedule, Song Request, On Demand) to be used in your website, responsive, seamlessly, and without extra scrollbars! It can even resize the iframes when listeners rotate their smartphone or tablet, so you get a real neat user experience.

Example that shows sizing

Use above iframe-resizer.html as an example to show how it works. Just put it somewhere in your web space and try it out. A good widget for testing is the "Song Request" iframe, since the listener can switch that to display 10 or 25 items/page.

Actual Javascript to use in your page

The file iframe-resizer.js contains the minimum needed to include on a page that uses AzuraCast iframe embeds. I recommend setting the width to 100% in the iframe (it will be as wide as the container element you use it in), and only let the script modify the iframe’s height, as shown above.

This will give you a seamless integration of the iframe into your web page, avoiding any extra scrollbars.

You can simply copy the iframe embedding code from your station’s embed widgets page, remove its min-height styling, and add an id, say id="widget" as shown above.

So, as an example, to embed the "Song Request" page into your website, you’d copy the embed code from your station:

<iframe src="https://demo.azuracast.com/public/azuratest_radio/embed-requests?theme=light" frameborder="0" allowtransparency="true" style="width: 100%; min-height: 850px; border: 0;"></iframe>

remove the min-height: 850px; and add the id="widget" so it becomes

<iframe id="widget" src="https://demo.azuracast.com/public/azuratest_radio/embed-requests?theme=light" frameborder="0" allowtransparency="true" style="width: 100%; border: 0;"></iframe>

and then add the above iframe-resizer.js near the end of your web page body.

Done! Enjoy a beautiful integration!

Thanks to @BusterNeece of AzuraCast for his time and efforts in making this work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment