File size: 1,615 Bytes
7aec436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<!DOCTYPE html>
<html>
  <head>
    <style>

      iframe {

        display: block;

      }

    </style>
    <meta name="robots" content="noindex">
  </head>
  <body>
    <p>Iframe:</p>
    <iframe width="600" height="600"></iframe>

    <p>
      <a href="" target="_blank" rel="opener">Open packager in new tab</a>
    </p>

    <script>

      const frame = document.querySelector('iframe');

      const link = document.querySelector('a');



      const url = `/?import_from=${location.origin}`;

      frame.src = url;

      link.href = url;



      const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));



      // In production, the expected origin is 'https://packager.turbowarp.org'

      const expectedOrigin = location.origin;



      window.addEventListener('message', async (e) => {

        if (e.origin !== expectedOrigin) {

          return;

        }



        const data = e.data && e.data.p4;

        if (!data) {

          return;

        }



        if (data.type === 'ready-for-import') {

          e.source.postMessage({

            p4: {

              type: 'start-import'

            }

          }, expectedOrigin);



          await sleep(Math.random() * 2000);



          const res = await fetch('./example.sb3');

          const buffer = await res.arrayBuffer();

          e.source.postMessage({

            p4: {

              type: 'finish-import',

              data: buffer,

              name: 'example.sb3'

            }

          }, expectedOrigin, [buffer]);

        }

      });

    </script>
  </body>
</html>