• 24Nov

    Serving simple HTML by LSL script

    I’ve written an LSL script which “serves” custom-generated HTML via a neat trick I discovered. But don’t bust out the champagne just yet — this method is extremely limited, so I don’t expect this to revolutionize HUDs or anything like that. Still, it’s a fun curiosity.

    The trick is this: If you use text of the form “data:text/html,[html code here]” as the web URL, Firefox (and maybe other browsers?) will render the HTML code as a web page. For example, visit data:text/html,<html><body><h1>Oh hai!</h1></body></html>, and Firefox will render the words “Oh hai!”. It’s not accessing a web page, and it’s not loading a local HTML file, it’s loading the HTML code from the pseudo-URL.

    The same trick works in SL as well, by setting the parcel media URL to the “data:text/html” string. You can use an LSL script to generate the HTML code and set the parcel URL, or even set the URL for an individual avatar. The result is that your LSL script acts as a very simple web server!

    However, as I mentioned, there are some serious limitations: SL won’t let you set the web URL to a string longer than 254 letters! That means you have to cram all the HTML code into that tiny string, which severely limits the complexity of the HTML you can display.

    Another problem is that the method may not work for other web engines besides Firefox / Mozilla. Linden Lab is (or was recently) working on switching to WebKit, the engine used by Safari, for rendering HTML. That’s good news, since WebKit is some great software, but this little trick might stop working when they switch. So, I wouldn’t rely on it.

    So, I don’t expect this method to be more than a silly toy for programmers to play around with. But it is that! And on the plus side, there are signs that we might get proper HTTP server functionality in LSL sometime in the future, which would be a great boon to many scripting industries in SL.

    You can get a full-perm copy of the object and script in SL at my sandbox or copy the following code (below the fold, if you’re viewing this on my blog front page) into an LSL script in your own object (in which case you should apply the “*Default Media Texture” to your prim from the Library, or you won’t see anything).

    // VERY SIMPLE HTML SERVER
    //   by Jacek Antonelli, 2008-11-24.
    //
    // Description: When an avatar touches the object, it sets the
    // avatar's media URL to a custom-generated HTML string,
    // effectively serving HTML from an LSL script.
    //
    // However, this method is limited by the maximum allowed
    // URL length of 254 characters. Thus it's only possible to
    // serve very simple HTML with this method.
    //
    // This script has been released to the PUBLIC DOMAIN
    // by its author. 
    
    default
    {
        touch_start( integer n )
        {
            string html = 
                "<html><body>"+
                "<p style="font-family:sans-serif;font-size:30px;">"+
                "Your name is "+ llDetectedName(0)+".<br/>"+
                "Your position is "+ (string)llDetectedPos(0)+ ".<br/>"+
                "This HTML is generated by the LSL script.<br/>"+
                "Have a nice day. :)"+
                "</p></body></html>";
            
            llParcelMediaCommandList(
                [PARCEL_MEDIA_COMMAND_TEXTURE,
                "8b5fec65-8d8d-9dc5-cda8-8fdf2716e361",
                PARCEL_MEDIA_COMMAND_URL,
                "data:text/html,"+html,
                PARCEL_MEDIA_COMMAND_TYPE,
                "text/html",
                PARCEL_MEDIA_COMMAND_SIZE,
                600, 300,
                PARCEL_MEDIA_COMMAND_AGENT,
                llDetectedKey(0),
                PARCEL_MEDIA_COMMAND_PLAY
                ]);
        }
    }
    

    Posted by Jacek Antonelli @ 3:22 pm

3 Responses

WP_Cloudy