代码之家  ›  专栏  ›  技术社区  ›  alexa

localhost在我的远程VS Code的Web视图中不起作用

  •  0
  • alexa  · 技术社区  · 3 年前

    我正在尝试实现自己的VSCode扩展。扩展将启动一些子流程,创建 localhost:port .网站 localhost:端口 应加载 WebView 。目前它在本地机器上完全可以工作,但当我使用SSH连接时,webview没有加载到远程机器上。

    据我所知,VSCode有一些 problems 从web视图使用localhost。我已经尝试过使用上述两种解决方法。但不幸的是,没有人工作。

    例如,我从网站上实现了第二项工作。我创建 panel 使用以下代码:

    let panel = vscode.window.createWebviewPanel(view_type, tab_name,
                vscode.ViewColumn.One,
                {
                    enableScripts: true,
                    retainContextWhenHidden: true
                });
    

    然后,我尝试从子流程中可视化localhost:

    const process = subprocess.spawn(cmd, [model_path]);
    process.stdout.on('data', async (data) => {
                const output = data.toString();
                const url_path = output;
                const web_uri = await vscode.env.asExternalUri(vscode.Uri.parse(url_path));
                panel.webview.html = this.getHTMLForWebview(web_uri);
            });
    
    private getHTMLForWebview(web_uri: vscode.Uri): string {
        return `
            <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                <title>Visualization</title>
            </head>
            <body>
                <iframe
                    width="100%"
                    height="800" 
                    src="${web_uri}"
                ></iframe>
            </body>
            </html>`;
    }
    

    正如我之前所说的,它可以在我的本地Windows机器上运行。但我在远程Linux机器上看到了空的webview(只是一个空帧): enter image description here

    这是我的第一次延期,所以我可能错过了什么。

    我应该添加/重写什么来在远程机器上渲染localhost?

    我也已经看过了 another 解决方案这只是一个已知网站的链接。

    0 回复  |  直到 3 年前
        1
  •  0
  •   Mike Lischke    3 年前

    默认情况下,VS代码对可以加载的内容非常严格。添加内容安全策略以启用更多功能。以下是我用来允许加载外部资源的内容:

    <meta http-equiv="Content-Security-Policy" content="default-src *; img-src http: https:;
        script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'unsafe-inline' http: https: data: *;">
    
    推荐文章