upload home and index page
This commit is contained in:
		@@ -14,19 +14,24 @@ header("Allow: GET, POST, OPTIONS, PUT, DELETE");
 | 
			
		||||
</head>
 | 
			
		||||
 | 
			
		||||
<body>
 | 
			
		||||
    <iframe src="http://localhost:8080/test/embedded.html" 
 | 
			
		||||
    <iframe 
 | 
			
		||||
    id="embedded-devstar"
 | 
			
		||||
    src="http://localhost:8080/test/home.html" 
 | 
			
		||||
    width="100%" 
 | 
			
		||||
    height="50%"
 | 
			
		||||
    height="100%"
 | 
			
		||||
    frameborder="0"
 | 
			
		||||
    style="border: 0; left: 0; right: 0; bottom: 0; top: 0; position:absolute;">
 | 
			
		||||
    </iframe>
 | 
			
		||||
 | 
			
		||||
    <!-- <iframe src="http://www.devstar.cn" 
 | 
			
		||||
    <!-- <iframe 
 | 
			
		||||
    id="embedded-devstar"
 | 
			
		||||
    src="https://www.devstar.cn/spa.html" 
 | 
			
		||||
    width="100%" 
 | 
			
		||||
    height="100%"
 | 
			
		||||
    frameborder="0"
 | 
			
		||||
    style="border: 0; left: 0; right: 0; bottom: 0; top: 0; position:absolute;">
 | 
			
		||||
    </iframe> -->
 | 
			
		||||
 | 
			
		||||
    <script>
 | 
			
		||||
        const vscode = acquireVsCodeApi();
 | 
			
		||||
 | 
			
		||||
@@ -46,21 +51,69 @@ header("Allow: GET, POST, OPTIONS, PUT, DELETE");
 | 
			
		||||
            vscode.postMessage({ command: 'openRemoteFolder', host: host, path: path });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        async function biCommunication2Vscode(command, data) {
 | 
			
		||||
            return new Promise((resolve, reject) => {
 | 
			
		||||
                // request to vscode 
 | 
			
		||||
                vscode.postMessage({ command: command, data: data })
 | 
			
		||||
 | 
			
		||||
                function handleResponse(event) {
 | 
			
		||||
                    const jsonData = event.data;
 | 
			
		||||
                    if (jsonData.command === command) {
 | 
			
		||||
                        // return vscode response
 | 
			
		||||
                        console.log("biCommunication2Vscode", jsonData.data)
 | 
			
		||||
                        window.removeEventListener('message', handleResponse) // 清理监听器
 | 
			
		||||
                        resolve(jsonData.data)
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                window.addEventListener('message', handleResponse)
 | 
			
		||||
 | 
			
		||||
                setTimeout(() => {
 | 
			
		||||
                    window.removeEventListener('message', handleResponse)
 | 
			
		||||
                    reject('timeout')
 | 
			
		||||
                }, 5000); // 5秒超时
 | 
			
		||||
            })
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // 监听子页面的消息
 | 
			
		||||
        window.addEventListener('message', (event) => {
 | 
			
		||||
        window.addEventListener('message', async (event) => {
 | 
			
		||||
            // 出于安全考虑,检查 event.origin 是否是你预期的源
 | 
			
		||||
            // if (event.origin !== "http://expected-origin.com") {
 | 
			
		||||
            //     return;
 | 
			
		||||
            // }
 | 
			
		||||
        
 | 
			
		||||
            try {
 | 
			
		||||
                const data = JSON.parse(event.data);
 | 
			
		||||
                if (data.action === 'firstOpenRemoteFolder') {
 | 
			
		||||
                    console.log('Received message:', data);
 | 
			
		||||
                    firstOpenRemoteFolderWithData(data.host, data.username, data.password, data.port, data.path);
 | 
			
		||||
                } else if (data.action === 'openRemoteFolder') {
 | 
			
		||||
                    console.log('Received message:', data);
 | 
			
		||||
                    openRemoteFolderWithData(data.host, data.path);
 | 
			
		||||
                const jsonData = event.data;
 | 
			
		||||
                if (jsonData.action === 'firstOpenRemoteFolder') {
 | 
			
		||||
                    console.log('Received message:', jsonData);
 | 
			
		||||
                    firstOpenRemoteFolderWithData(jsonData.host, jsonData.username, jsonData.password, jsonData.port, jsonData.path);
 | 
			
		||||
 | 
			
		||||
                } else if (jsonData.action === 'openRemoteFolder') {
 | 
			
		||||
                    console.log('Received message:', jsonData);
 | 
			
		||||
                    openRemoteFolderWithData(jsonData.host, jsonData.path);
 | 
			
		||||
 | 
			
		||||
                } else if (jsonData.action === 'getUserToken') {
 | 
			
		||||
                    // request user token from vscode global state
 | 
			
		||||
                    const data = await biCommunication2Vscode('getUserToken', {})
 | 
			
		||||
                    var iframe = document.getElementById('embedded-devstar');
 | 
			
		||||
                    if (iframe && iframe.contentWindow) {
 | 
			
		||||
                        iframe.contentWindow.postMessage({ action: 'getUserToken', data: data}, '*');
 | 
			
		||||
                    }
 | 
			
		||||
                } else if (jsonData.action === 'setUserToken') {
 | 
			
		||||
                    // set user token to vscode global state
 | 
			
		||||
                    await biCommunication2Vscode('setUserToken', jsonData.data)
 | 
			
		||||
                    .then((data) => {
 | 
			
		||||
                        console.log(data)
 | 
			
		||||
                        var iframe = document.getElementById('embedded-devstar');
 | 
			
		||||
                        if (iframe && iframe.contentWindow) {
 | 
			
		||||
                            iframe.contentWindow.postMessage({ action: 'setUserToken', data: data}, '*');
 | 
			
		||||
                        }
 | 
			
		||||
                    })
 | 
			
		||||
                    .catch((err) => {
 | 
			
		||||
                        if (iframe && iframe.contentWindow) {
 | 
			
		||||
                            iframe.contentWindow.postMessage({ action: 'setUserToken', data: {ok: false}}, '*');
 | 
			
		||||
                        }
 | 
			
		||||
                    });
 | 
			
		||||
                }
 | 
			
		||||
            } catch (error) {
 | 
			
		||||
                console.error('Error parsing message:', error);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user