File size: 6,676 Bytes
8fd7a1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mistwarp SB3 PostMessage Test</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .button {
            background-color: #4285f4;
            color: white;
            border: none;
            padding: 10px 20px;
            margin: 5px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }
        .button:hover {
            background-color: #3367d6;
        }
        .input-group {
            margin: 10px 0;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="text"], input[type="file"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-bottom: 10px;
        }
        .iframe-container {
            margin-top: 20px;
            border: 2px solid #ddd;
            border-radius: 8px;
            overflow: hidden;
        }
        iframe {
            width: 100%;
            height: 600px;
            border: none;
        }
        .info {
            background-color: #e3f2fd;
            padding: 15px;
            border-radius: 4px;
            margin-bottom: 20px;
        }
        .code {
            background-color: #f5f5f5;
            padding: 10px;
            border-radius: 4px;
            font-family: monospace;
            margin: 10px 0;
            border-left: 4px solid #4285f4;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Mistwarp SB3 PostMessage Test</h1>
        
        <div class="info">
            <h3>How to use this feature:</h3>
            <p>This page demonstrates how external applications can send SB3 files to Mistwarp using postMessage.</p>
            <p>The message format should be:</p>
            <div class="code">
{<br>
  &nbsp;&nbsp;type: 'LOAD_SB3',<br>
  &nbsp;&nbsp;data: ArrayBuffer | string (URL) | Uint8Array,<br>
  &nbsp;&nbsp;title?: string<br>
}
            </div>
        </div>

        <h3>Test Methods:</h3>

        <div class="input-group">
            <label for="urlInput">Load SB3 from URL:</label>
            <input type="text" id="urlInput" placeholder="https://example.com/project.sb3" value="">
            <input type="text" id="titleInput" placeholder="Project Title (optional)" value="">
            <button class="button" onclick="loadFromUrl()">Load from URL</button>
        </div>

        <div class="input-group">
            <label for="fileInput">Load SB3 from File:</label>
            <input type="file" id="fileInput" accept=".sb3,.sb2,.sb" onchange="handleFileSelect(event)">
            <button class="button" onclick="loadFromFile()">Load Selected File</button>
        </div>

        <div class="input-group">
            <button class="button" onclick="loadSampleProject()">Load Sample Project (Cat Flying)</button>
        </div>

        <div class="iframe-container">
            <iframe id="mistwarpFrame" src="http://localhost:8601/" title="Mistwarp Editor"></iframe>
        </div>
    </div>

    <script>
        let selectedFile = null;

        function loadFromUrl() {
            const url = document.getElementById('urlInput').value.trim();
            const title = document.getElementById('titleInput').value.trim();
            
            if (!url) {
                alert('Please enter a URL');
                return;
            }

            const message = {
                type: 'LOAD_SB3',
                data: url
            };

            if (title) {
                message.title = title;
            }

            sendMessageToMistwarp(message);
        }

        function handleFileSelect(event) {
            selectedFile = event.target.files[0];
        }

        function loadFromFile() {
            if (!selectedFile) {
                alert('Please select a file first');
                return;
            }

            const reader = new FileReader();
            reader.onload = function(e) {
                const title = selectedFile.name.replace(/\.sb[23]?$/, '');
                
                const message = {
                    type: 'LOAD_SB3',
                    data: e.target.result, // ArrayBuffer
                    title: title
                };

                sendMessageToMistwarp(message);
            };
            reader.readAsArrayBuffer(selectedFile);
        }

        function loadSampleProject() {
            // Create a simple sample project data (this would normally be actual SB3 data)
            // For demonstration, we'll use a URL to a sample project
            const message = {
                type: 'LOAD_SB3',
                data: 'https://extensions.turbowarp.org/samples/Stretch.sb3',
                title: 'Sample Project - Stretch'
            };

            sendMessageToMistwarp(message);
        }

        function sendMessageToMistwarp(message) {
            const iframe = document.getElementById('mistwarpFrame');
            
            console.log('Sending message to Mistwarp:', message);
            
            // Send message to the iframe
            iframe.contentWindow.postMessage(message, '*');
            
            // You could also specify the exact origin for security:
            // iframe.contentWindow.postMessage(message, 'http://localhost:8601');
        }

        // Listen for messages back from Mistwarp (optional)
        window.addEventListener('message', function(event) {
            if (event.origin !== 'http://localhost:8601') {
                return;
            }
            
            console.log('Received message from Mistwarp:', event.data);
        });

        // Update the default Mistwarp URL based on the current protocol
        window.addEventListener('load', function() {
            const iframe = document.getElementById('mistwarpFrame');
            const currentProtocol = window.location.protocol;
            const defaultUrl = currentProtocol === 'https:' ? 
                'https://localhost:8601/' : 
                'http://localhost:8601/';
            iframe.src = defaultUrl;
        });
    </script>
</body>
</html>