How to get web server name

I'm completely out of my depth here. I'm trying to find a way for a BBC BASIC program, running in the in-browser edition of BBCSDL, to discover from where bbcsdl.html was downloaded.

For example, it needs to be able to tell whether it was run as https://wasm.bbcbasic.co.uk/bbcsdl.html or https://wasm.bbcbasic.net/bbcsdl.html. I feel that should be possible, but haven't a clue how to achieve it.

The only thing Google has come up with is this StackOverflow post, but I don't know how to apply it.

Comments

  • While I don't have any experience of WebAssembly, it appears that code snippet retrieves the detail, however the next trick is how to expose that to BASIC - perhaps putting it into a system variable such as @wwwhost$ for example?
  • Soruk wrote: »
    it appears that code snippet retrieves the detail
    But what language is that 'code snippet' written in? It doesn't look like anything I'm familiar with so I don't know how I would call it from C code or from BASIC code.

    The easiest language to use would probably be JavaScript, because I can call that from BBC BASIC code quite straightforwardly:
     SYS "emscripten_run_script", script$
    


  •  SYS "emscripten_run_script", script$
    
    Actually, since I want to get a string back it would be:
     SYS "emscripten_run_script_string", script$ TO host%
     host$ = $$host%
    
    So if there's a way to get the server name in JavaScript this should do the job nicely. But is there?
  • Soruk wrote: »
    Does this help?
    I'm not sure. The title 'How to extract the hostname portion of a URL' would suggest not, since I don't have the full page URL to start with (and if I did the task would be trivial).

    But the top-rated reply seems to be talking about how to extract the hostname of the current page's URL, which isn't what the questioner is asking for, but is what I need!

    The missing link is that I don't know how to write JavaScript code to return the wanted 'property' as a string. Maybe it's as simple as this (that would be nice!) but I'll have to try it to find out:
     SYS "emscripten_run_script_string", "window.location.hostname" TO host%
     host$ = $$host%
    
  • Maybe it's as simple as this (that would be nice!)
    It very nearly was! To find the 'window' object the JavaScript code, not surprisingly, needs to be run in the context of the main thread, not the interpreter's thread (Web Worker in JS parlance). Therefore I needed to add a dummy @memhdc% parameter to force BBC BASIC to do a cross-thread SYS call. Then it works, see below.

    Thanks very much for the pointer (and to Darren Storer at the Discussion Group).

    alwqgcsqi7d8.png
  • Nice one! Out of curiosity, what does $$var% do (specifically, the double $) ?
  • Soruk wrote: »
    Out of curiosity, what does $$var% do (specifically, the double $) ?
    NUL-terminated string, used extensively when interfacing with most OSes or code written in C. Much more common than the CR-terminated string ($var%) these days, in my experience.

    I expect RISC OS uses CR-terminated strings, but if so it's an outlier.
Sign In or Register to comment.