Spaces:
Running
Running
Download hf-space-parent-listener.js from rycerzes/inside-beam: direct link, hf CLI and curl.
- Browser
- Download file 1.63 kB
-
https://huggingface.co/spaces/rycerzes/inside-beam/resolve/main/hf-space-parent-listener.js
- Command line
-
hf download hf://spaces/rycerzes/inside-beam/hf-space-parent-listener.js
-
curl -L -o hf-space-parent-listener.js https://huggingface.co/spaces/rycerzes/inside-beam/resolve/main/hf-space-parent-listener.js
1.63 kB
| /** | |
| * Script for Hugging Face Spaces parent window | |
| * This script listens to iframe messages and updates the parent window URL | |
| * | |
| * Usage instructions: | |
| * 1. Add this script to your Hugging Face Space in app.py or in a Gradio component | |
| * 2. Or use it in an HTML page that contains your iframe | |
| */ | |
| (function () { | |
| 'use strict'; | |
| // Listen to iframe messages | |
| window.addEventListener('message', function (event) { | |
| // Check message type | |
| if (event.data && event.data.type) { | |
| switch (event.data.type) { | |
| case 'urlChange': | |
| case 'anchorChange': | |
| case 'HF_SPACE_URL_UPDATE': | |
| handleUrlChange(event.data); | |
| break; | |
| default: | |
| // Unknown message type, ignore | |
| } | |
| } | |
| }); | |
| function handleUrlChange(data) { | |
| try { | |
| const hash = data.hash || data.anchorId; | |
| if (hash) { | |
| // Update URL with new anchor | |
| const newUrl = new URL(window.location); | |
| newUrl.hash = hash; | |
| // Use replaceState to avoid adding an entry to history | |
| window.history.replaceState(null, '', newUrl.toString()); | |
| } | |
| } catch (error) { | |
| // Silent error when updating URL | |
| } | |
| } | |
| // Utility function to test communication | |
| window.testIframeCommunication = function () { | |
| const iframe = document.querySelector('iframe'); | |
| if (iframe) { | |
| iframe.contentWindow.postMessage({ type: 'test' }, '*'); | |
| } | |
| }; | |
| })(); | |