Spaces:
Running
Running
| // Sync shareable URL query params between this iframe and the HF Spaces | |
| // parent frame (huggingface.co/spaces/<owner>/<name>?...). | |
| // | |
| // Pattern borrowed from lerobot-dataset-visualizer: | |
| // - On mount, read the iframe's own query string (HF forwards the parent's | |
| // params into the iframe's URL on first load). | |
| // - On state change, update our own URL via history.replaceState AND | |
| // postMessage({ queryString }) to the parent so the shareable link on | |
| // huggingface.co reflects our state. | |
| const HF_PARENT_ORIGIN = 'https://huggingface.co'; | |
| export function getParam(key) { | |
| try { | |
| return new URLSearchParams(window.location.search).get(key); | |
| } catch { | |
| return null; | |
| } | |
| } | |
| export function setParam(key, value) { | |
| try { | |
| const params = new URLSearchParams(window.location.search); | |
| if (value == null || value === '') params.delete(key); | |
| else params.set(key, value); | |
| const qs = params.toString(); | |
| const newUrl = `${window.location.pathname}${qs ? '?' + qs : ''}`; | |
| window.history.replaceState(null, '', newUrl); | |
| postToParent(params); | |
| } catch { | |
| // Ignore — standalone deploy or cross-origin blocked. | |
| } | |
| } | |
| function postToParent(params) { | |
| try { | |
| if (window.parent && window.parent !== window) { | |
| window.parent.postMessage( | |
| { queryString: params.toString() }, | |
| HF_PARENT_ORIGIN | |
| ); | |
| } | |
| } catch { | |
| // Ignore. | |
| } | |
| } | |