Skip to content

Commit 576c53f

Browse files
committed
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
1 parent 0594794 commit 576c53f

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

lib/esbonio/esbonio/server/features/preview_manager/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,15 @@ async def show_preview_uri(self) -> Optional[Uri]:
188188
server = await self.preview
189189
webview = await self.webview
190190

191-
query_params: dict[str, Any] = dict(ws=webview.port)
191+
host = "localhost"
192+
query_params: dict[str, Any] = dict(ws=f"ws://{host}:{webview.port}")
192193

193194
if self.config.show_line_markers:
194195
query_params["show-markers"] = True
195196

196197
uri = Uri.create(
197198
scheme="http",
198-
authority=f"localhost:{server.port}",
199+
authority=f"{host}:{server.port}",
199200
path=self.build_path,
200201
query=urlencode(query_params),
201202
)

lib/esbonio/esbonio/sphinx_agent/static/webview.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,12 @@ function renderLineMarkers() {
216216
document.body.append(markerStyle)
217217
}
218218

219-
const host = window.location.hostname;
220-
const queryString = window.location.search;
221-
const queryParams = new URLSearchParams(queryString);
222-
const ws = parseInt(queryParams.get("ws"));
219+
const queryParams = new URLSearchParams(window.location.search);
223220
const showMarkers = queryParams.has("show-markers")
221+
const wsUrl = queryParams.get("ws");
224222

225-
const wsServer = `ws://${host}:${ws}`
226-
console.debug(`Connecting to '${wsServer}'...`)
227-
228-
const socket = new WebSocket(wsServer);
223+
console.debug(`Connecting to '${wsUrl}'...`)
224+
const socket = new WebSocket(wsUrl);
229225
let connected = false
230226

231227
function sendMessage(data) {
@@ -242,7 +238,7 @@ const handlers = {
242238
console.debug("Reloading page...")
243239
window.location.reload()
244240
},
245-
"view/scroll": (params) => {scrollViewTo(params.uri, params.line)}
241+
"view/scroll": (params) => { scrollViewTo(params.uri, params.line) }
246242
}
247243

248244
function handle(message) {
@@ -291,7 +287,7 @@ function main() {
291287
renderLineMarkers()
292288
}
293289

294-
rewriteInternalLinks(ws)
290+
rewriteInternalLinks(wsUrl)
295291

296292
// Are we in an <iframe>?
297293
if (window.parent !== window.top) {

0 commit comments

Comments
 (0)