File size: 1,400 Bytes
74918fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 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.
  }
}