Talk:LlSetPrimMediaParams/Tricks

From Second Life Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Here is my quick hack of your code to allow forms to be submitted using the javascript trick.

<lsl> string url; // Our base http-in url integer r; // Used for uniqueness in PrimMedia urls integer page; // The page number.

show(string html) {

   html += "";

   llSetPrimMediaParams(0,                  // Side to display the media on.
           [PRIM_MEDIA_AUTO_PLAY,TRUE,      // Show this page immediately
            PRIM_MEDIA_CURRENT_URL,html,    // The url if they hit 'home'
            PRIM_MEDIA_HOME_URL,html,       // The url currently showing
            PRIM_MEDIA_HEIGHT_PIXELS,512,   // Height/width of media texture will be
            PRIM_MEDIA_WIDTH_PIXELS,512]);  //   rounded up to nearest power of 2.

}

// This creates a data: url that will render the output of the http-in url // given. string build_url(string url) {

   return "data:text/html," 
       + llEscapeURL("<html><head><script src='" + url 
       + "' type='text/javascript'></script></head><body onload='init()'></body></html>");

}

// This wraps the html you want to display so that it will be shown from links // made with build_url string build_response(string body) {

   return "function init() {document.getElementsByTagName('body')[0].innerHTML='" + body + "';}";

}


string replace_all(string src, string target, string replace) {

   return llDumpList2String(llParseString2List(src,[target],[]),replace);

}

string get_query(key id, string name) {

   string query = llGetHTTPHeader(id,"x-query-string");
   query = replace_all(query,"+"," ");
   query = llUnescapeURL(query);
   list q = llParseString2List(query,["=","&",";"],[]);
   integer i = llListFindList(q,[name]);
   if (i != -1)
   {
       return llList2String(q,i+1);
   }

   return "";

}


default {

   state_entry()
   {
       llRequestURL();
       llOwnerSay("Ready");
   }

   http_request(key id, string method, string body)
   {
       if (method == URL_REQUEST_GRANTED)
       {
           url = body + "/";

           show(build_url(url + "page" + (string)(page)));
       }
       else if (method == "GET")
       {
           //Check for submited form value here
           string float_text=get_query(id,"text");
           if (float_text!="")
           {                
               llSetText(float_text,<1,1,0>,1);
               show(build_url(url + "page" + (string)(page)));
               llHTTPResponse(id,200,"Loading...");
               return;
           }       
           
           string path = llGetHTTPHeader(id,"x-path-info");

string content = "

path: " + path + "

";

           page = (integer)llGetSubString(path,5,5);
           if (page > 0) content += "<a href=\"" + build_url(url + "page" + (string)(page - 1)) + "\">Previous</a> ";
           if (page < 9) content += "<a href=\"" + build_url(url + "page" + (string)(page + 1)) + "\">Next</a>
"; integer k; for(;k<10;++k) { if (k == page) content += "
Page " + (string)k; else content += "
<a href=\"" + build_url(url + "page" + (string)(k)) + "\">Page " + (string)k + "</a>"; }

content += "

<form action=\""+url+"\" method=\"GET\">Floating Text:<input type=\"text\" name=\"text\">
<input type=\"submit\" value=\"Set\"></form>

";

           content += "

This page is " + (string)(llStringLength(build_response(content)) + 37) + " bytes long."; llHTTPResponse(id,200,build_response(content)); } }

} </lsl>

In regards to needing to use llEscapeURL.

There are specific characters that must be encoded when included in a URL. llEscapeURL encodes MORE than what is needed.

Specifically llEscapeURL only leaves [0-9a-zA-Z] as not encoded.

Other characters can be left not encoded as well - $-_.+!*'(),

See the following link for more information.

URL Encoding


Thraxis Epsilon 03:22, 31 August 2010 (UTC)