Gayngel

Persistent_URL_Google_App_Script

Oct 21st, 2021 (edited)
1,076
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Google App Script Code by Gayngel of The Script Yard.
  3. Join The Script Yard Group: secondlife:///app/group/4921b4f0-c21e-7866-c0c2-791d10bb5502/about
  4. Visit The Script Yard Marketplace: https://marketplace.secondlife.com/stores/149734
  5.  
  6. Description:
  7.  
  8. This Google App Script keeps track of an LSL script's URL when it changes. The object the script is in can then be used as a server to retrieve persistent data from for a drop box and redelivery terminal system or scoreboard or HUD or whatever object/system you create that requires persistent stored data. The URL and stored data  is saved to a spreadsheet and the data can be retrieved by a client script.
  9.  
  10. Instructions:
  11.  
  12. 1. Open your Google Drive and select New > More > Google App Script.
  13. 2. Before doing anything in the editor you must switch to the legacy editor. This is a very important step as some features don't work yet in the new editor. On the top right corner under your account button select "Use Legacy Editor". You can ignore and close the opt out survey.
  14. 3. Copy and paste this script into the code editor.
  15. 4. Select the save icon to save the script and give it a name. N.B: It is very important you save the script before publishing it to the web.
  16. 5. Select "Publish" on the menu bar and select "Deploy as Web App"
  17. 6. Select the Project Version dropdown and select New.
  18. 7. Under "Execute App As:" select Me(youremail@gmail.com)
  19. 8. Under "Who has access to the app:" select "Anyone, even anonymous".
  20. 9. Select "Deploy" and "Review Permissions".
  21. 10. Sign in to your google account.
  22. 11. If you get the "Google hasn't verified this app" message select "Advanced" and then select "Go to app_name(unsafe)".
  23. 12. Review and Allow Permissions.
  24. 13. Copy the web app url and paste it both in the server and client scripts in the line string app = ""; paste the url in between the quotation marks.
  25.  
  26.  
  27. If you make any changes to the script you must save and publish again. You must also select a new project version.
  28.   */
  29.  
  30. function doGet(e) // The e parameter is the query data and arguments after the URL denoted by "?". For example if your http request is app_url+"?object_name="+obj_name+"&obj_key="+(string)llGetKey() then object_name and object_key are the e.parameters and can be called as e.parameter.object_name and e.parameter.object_key respectively.
  31.  
  32. {
  33.  
  34. var props = PropertiesService.getScriptProperties(); // Properties service can store properties/metadata. You can find properties by selecting File > Project Properties > Script Properties tab
  35. var url;  
  36.  
  37. if(e.parameter.server_url)  
  38. {
  39. url = e.parameter.server_url;  // gets the url of the server
  40. var obj_key = e.parameter.obj_key;
  41. var prop_obj = props.getProperty('prop_obj');  // get the prop_obj property from script properties
  42.  
  43. if(prop_obj)
  44. {  
  45.  
  46. if(obj_key == prop_obj)  // prevents two servers sharing the same app script/database
  47. {  
  48. props.setProperty('URL',url);
  49.  
  50.  var payload =
  51.    {
  52.      "URL_OK":"URL_OK"  // body of method (POST,GET,PUT,etc). Sent as key/value pair. Can be parsed by llParseString2List(body,["="],[])
  53.    };
  54.  
  55.      var options =
  56.    {
  57.      "method" : "post",  // Method like HTTP_METHOD in the llHTTPRequest() function. We'll make a POST.
  58.      "payload" : payload  // what variables we are sending back to the server/client.
  59.    };
  60.  
  61.      var response = UrlFetchApp.fetch(url,options); // Make a http request back to the URL of the server or client that made the request.
  62.  }
  63.  
  64.   else  // server uuid is different to stored server uuid. can not share same app script / database
  65.   {
  66.  
  67.     var payload =
  68.    {
  69.      "URL_INVALID":"URL_INVALID" // body of method (POST,GET,PUT,etc). Sent as key/value pair. Can be parsed by llParseString2List(body,["="],[])
  70.    };
  71.  
  72.      var options =
  73.    {
  74.      "method" : "post",  // Method like HTTP_METHOD in the llHTTPRequest() function. We'll make a POST.
  75.      "payload" : payload  // what variables we are sending back to the server/client.
  76.    };
  77.  
  78.      var response = UrlFetchApp.fetch(url,options); // Make a http request back to the URL of the server or client that made the request.
  79.  
  80.   }
  81.  
  82.  
  83. }
  84.  
  85.   else
  86.   {
  87.     props.setProperty('prop_obj', obj_key); // sets a property prop_obj as object_key
  88.     props.setProperty('URL',url);
  89.  
  90.  var payload =
  91.    {
  92.      "URL_OK":"URL_OK" // body of method (POST,GET,PUT,etc). Sent as key/value pair. Can be parsed by llParseString2List(body,["="],[])
  93.    };
  94.  
  95.      var options =
  96.    {
  97.      "method" : "post",  // Method like HTTP_METHOD in the llHTTPRequest() function. We'll make a POST.
  98.      "payload" : payload  // what variables we are sending back to the server/client.
  99.    };
  100.  
  101.      var response = UrlFetchApp.fetch(url,options); // Make a http request back to the URL of the server or client that made the request.
  102.    
  103.   }
  104.  
  105. }
  106.  
  107.   else if(e.parameter.client_url)
  108.   {
  109.    
  110.     url = e.parameter.client_url; // gets the url of the client
  111.     var ssNew;
  112.     var ssId = props.getProperty('ssId'); //gets the id of the spreadsheet by retrieving the stored id in script properties
  113.     ssNew = SpreadsheetApp.openById(ssId); //open the spreadsheet
  114.     var sheet = ssNew.getSheets()[0];  // get the first sheet in the spreadsheet
  115.    var cell = sheet.getRange("A1"); // gets the range of cells.
  116.    
  117.    
  118.      var payload =
  119.    {
  120.      "server_url":cell.getValue() // body of method (POST,GET,PUT,etc). Sent as key/value pair.  Can be parsed by llParseString2List(body,["="],[]) Sends the value of cell A1.
  121.    };
  122.  
  123.      var options =
  124.    {
  125.       "method" : "post",  // Method like HTTP_METHOD in the llHTTPRequest() function. We'll make a POST.
  126.      "payload" : payload  // what variables we are sending back to the server/client.
  127.    };
  128.  
  129.      var response = UrlFetchApp.fetch(url,options); // Make a http request back to the URL of the server or client that made the request.
  130.    
  131.    
  132.   }
  133.  
  134. }
  135.  
  136. function doPost(e)  // The e parameter is the query data and arguments after the URL denoted by "?". For example if your http request is app_url+"?object_name="+obj_name+"&obj_key="+(string)llGetKey() then object_name and object_key are the e.parameters and can be called as e.parameter.object_name and e.parameter.object_key respectively.
  137.  
  138. {
  139.   var url = e.parameter.url;
  140.   var props = PropertiesService.getScriptProperties(); // Properties service can store properties/metadata. You can find properties by selecting File > Project Properties > Script Properties tab
  141.   var ssNew;
  142.   var ssId = props.getProperty('ssId');   // get the ssId property from script properties
  143.  
  144.   if(ssId) // if the spreadsheet is created. I.E If the property ssId exists
  145.   {
  146.    ssNew = SpreadsheetApp.openById(ssId); // open the spreadsheet
  147.    var sheet = ssNew.getSheets()[0];   // get first sheet of spreadsheet
  148.    var cell = sheet.getRange("A1"); // target the first cell in the spreadsheet - A1
  149.     cell.clear(); // clear the data in A1
  150.    cell.setValue(e.parameter.url); // set the value of the A1 cell to the url of the server
  151.    props.setProperty('URL',url); // sets a property URL as the url of the server
  152.    
  153.    
  154.     var payload =
  155.    {
  156.      "url_added":"url_added" // body of method (POST,GET,PUT,etc). Sent as key/value pair. Can be parsed by llParseString2List(body,["="],[])
  157.    };
  158.  
  159.      var options =
  160.    {
  161.       "method" : "post",  // Method like HTTP_METHOD in the llHTTPRequest() function. We'll make a POST.
  162.      "payload" : payload  // what variables we are sending back to the server/client.
  163.    };
  164.  
  165.      var response = UrlFetchApp.fetch(url,options); // Make a http request back to the URL of the server or client that made the request.
  166.  
  167.   } // if(ssId)
  168.  
  169.   else // if spreadsheet is not created i.e there is no property named ssId in script properties
  170.   {
  171.    var ssName = e.parameter.spreadsheet;  // the name of the spreadsheet set in the server script
  172.    
  173.     ssNew = SpreadsheetApp.create(ssName); // create a new spread sheet
  174.    
  175.     props.setProperty('ssId', ssNew.getId()); // sets a property ssID and set the vaue of the property to the id of the spreadsheet
  176.    
  177.      var payload =
  178.    {
  179.      "SpreadsheetReady":ssNew.getId() // body of method (POST,GET,PUT,etc). Sent as key/value pair. Can be parsed by llParseString2List(body,["="],[])
  180.    };
  181.  
  182.      var options =
  183.    {
  184.       "method" : "post",  // Method like HTTP_METHOD in the llHTTPRequest() function. We'll make a POST.
  185.      "payload" : payload  // what variables we are sending back to the server/client.
  186.    };
  187.  
  188.      var response = UrlFetchApp.fetch(url,options); // Make a http request back to the URL of the server or client that made the request.
  189.    
  190.    
  191.   }
  192.  
  193.  
  194. }
Add Comment
Please, Sign In to add comment