From 576c53f46f77a42248bcafa18b18ca174dc1e071 Mon Sep 17 00:00:00 2001 From: Alex Carney Date: Fri, 4 Oct 2024 20:41:22 +0100 Subject: [PATCH] sphinx-agent: Take entire websocket URL from query parameter The injected `webview.js` script now assumes that the `ws` query parameter contains the entire URI to use when creating the websocket connection --- .../server/features/preview_manager/__init__.py | 5 +++-- .../esbonio/sphinx_agent/static/webview.js | 16 ++++++---------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/lib/esbonio/esbonio/server/features/preview_manager/__init__.py b/lib/esbonio/esbonio/server/features/preview_manager/__init__.py index 1ba0ab776..6ff29d6aa 100644 --- a/lib/esbonio/esbonio/server/features/preview_manager/__init__.py +++ b/lib/esbonio/esbonio/server/features/preview_manager/__init__.py @@ -188,14 +188,15 @@ async def show_preview_uri(self) -> Optional[Uri]: server = await self.preview webview = await self.webview - query_params: dict[str, Any] = dict(ws=webview.port) + host = "localhost" + query_params: dict[str, Any] = dict(ws=f"ws://{host}:{webview.port}") if self.config.show_line_markers: query_params["show-markers"] = True uri = Uri.create( scheme="http", - authority=f"localhost:{server.port}", + authority=f"{host}:{server.port}", path=self.build_path, query=urlencode(query_params), ) diff --git a/lib/esbonio/esbonio/sphinx_agent/static/webview.js b/lib/esbonio/esbonio/sphinx_agent/static/webview.js index c659f530b..c380495f3 100644 --- a/lib/esbonio/esbonio/sphinx_agent/static/webview.js +++ b/lib/esbonio/esbonio/sphinx_agent/static/webview.js @@ -216,16 +216,12 @@ function renderLineMarkers() { document.body.append(markerStyle) } -const host = window.location.hostname; -const queryString = window.location.search; -const queryParams = new URLSearchParams(queryString); -const ws = parseInt(queryParams.get("ws")); +const queryParams = new URLSearchParams(window.location.search); const showMarkers = queryParams.has("show-markers") +const wsUrl = queryParams.get("ws"); -const wsServer = `ws://${host}:${ws}` -console.debug(`Connecting to '${wsServer}'...`) - -const socket = new WebSocket(wsServer); +console.debug(`Connecting to '${wsUrl}'...`) +const socket = new WebSocket(wsUrl); let connected = false function sendMessage(data) { @@ -242,7 +238,7 @@ const handlers = { console.debug("Reloading page...") window.location.reload() }, - "view/scroll": (params) => {scrollViewTo(params.uri, params.line)} + "view/scroll": (params) => { scrollViewTo(params.uri, params.line) } } function handle(message) { @@ -291,7 +287,7 @@ function main() { renderLineMarkers() } - rewriteInternalLinks(ws) + rewriteInternalLinks(wsUrl) // Are we in an