Skip to content

Commit

Permalink
sphinx-agent: Take entire websocket URL from query parameter
Browse files Browse the repository at this point in the history
The injected `webview.js` script now assumes that the `ws` query
parameter contains the entire URI to use when creating the websocket
connection
  • Loading branch information
alcarney committed Oct 7, 2024
1 parent 0594794 commit 576c53f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Expand Down
16 changes: 6 additions & 10 deletions lib/esbonio/esbonio/sphinx_agent/static/webview.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -291,7 +287,7 @@ function main() {
renderLineMarkers()
}

rewriteInternalLinks(ws)
rewriteInternalLinks(wsUrl)

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

0 comments on commit 576c53f

Please sign in to comment.