Gayngel

Persistent_URL_Google_App_Script

Oct 21st, 2021 (edited)
551
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)
  31. {
  32.  
  33. var props = PropertiesService.getScriptProperties(); // Properties service can store properties/metadata. You can find properties by selecting File > Project Properties > Script Properties tab
  34. var url;  
  35.  
  36. if(e.parameter.server_url)  
  37. {
  38. url = e.parameter.server_url;  // gets the url of the server
  39. var obj_key = e.parameter.obj_key;
  40. var prop_obj = props.getProperty('prop_obj');  // get the prop_obj property from script properties
  41.  
  42. if(prop_obj)
  43. {  
  44.  
  45. if(obj_key == prop_obj)  // prevents two servers sharing the same app script/database
  46. {  
  47. props.setProperty('URL',url);
  48.  
  49.  var payload =
  50.    {
  51.      "URL_OK":"URL_OK"  // body of method (POST,GET,PUT,etc). Sent as key/value pair. Can be parsed by llParseString2List(body,["="],[])
  52.    };
  53.  
  54.      var options =
  55.    {
  56.      "method" : "post",  // Method like HTTP_METHOD in the llHTTPRequest() function. We'll make a POST.
  57.      "payload" : payload  // what variables we are sending back to the server/client.
  58.    };
  59.  
  60.      var response = UrlFetchApp.fetch(url,options); // Make a http request back to the URL of the server or client that made the request.
  61.  }
  62.  
  63.   else  // server uuid is different to stored server uuid. can not share same app script / database
  64.   {
  65.  
  66.     var payload =
  67.    {
  68.      "URL_INVALID":"URL_INVALID" // body of method (POST,GET,PUT,etc). Sent as key/value pair. Can be parsed by llParseString2List(body,["="],[])
  69.    };
  70.  
  71.      var options =
  72.    {
  73.      "method" : "post",  // Method like HTTP_METHOD in the llHTTPRequest() function. We'll make a POST.
  74.      "payload" : payload  // what variables we are sending back to the server/client.
  75.    };
  76.  
  77.      var response = UrlFetchApp.fetch(url,options); // Make a http request back to the URL of the server or client that made the request.
  78.  
  79.   }
  80.  
  81.  
  82. }
  83.  
  84.   else
  85.   {
  86.     props.setProperty('prop_obj', obj_key); // sets a property prop_obj as object_key
  87.     props.setProperty('URL',url);
  88.  
  89.  var payload =
  90.    {
  91.      "URL_OK":"URL_OK" // body of method (POST,GET,PUT,etc). Sent as key/value pair. Can be parsed by llParseString2List(body,["="],[])
  92.    };
  93.  
  94.      var options =
  95.    {
  96.      "method" : "post",  // Method like HTTP_METHOD in the llHTTPRequest() function. We'll make a POST.
  97.      "payload" : payload  // what variables we are sending back to the server/client.
  98.    };
  99.  
  100.      var response = UrlFetchApp.fetch(url,options); // Make a http request back to the URL of the server or client that made the request.
  101.    
  102.   }
  103.  
  104. }
  105.  
  106.   else if(e.parameter.client_url)
  107.   {
  108.    
  109.     url = e.parameter.client_url; // gets the url of the client
  110.     var ssNew;
  111.     var ssId = props.getProperty('ssId'); //gets the id of the spreadsheet by retrieving the stored id in script properties
  112.     ssNew = SpreadsheetApp.openById(ssId); //open the spreadsheet
  113.     var sheet = ssNew.getSheets()[0];  // get the first sheet in the spreadsheet
  114.    var cell = sheet.getRange("A1"); // gets the range of cells.
  115.    
  116.    
  117.      var payload =
  118.    {
  119.      "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.
  120.    };
  121.  
  122.      var options =
  123.    {
  124.       "method" : "post",  // Method like HTTP_METHOD in the llHTTPRequest() function. We'll make a POST.
  125.      "payload" : payload  // what variables we are sending back to the server/client.
  126.    };
  127.  
  128.      var response = UrlFetchApp.fetch(url,options); // Make a http request back to the URL of the server or client that made the request.
  129.    
  130.    
  131.   }
  132.  
  133. }
  134.  
  135. function doPost(e)
  136. {
  137.   var url = e.parameter.url;
  138.   var props = PropertiesService.getScriptProperties(); // Properties service can store properties/metadata. You can find properties by selecting File > Project Properties > Script Properties tab
  139.   var ssNew;
  140.   var ssId = props.getProperty('ssId');   // get the ssId property from script properties
  141.  
  142.   if(ssId) // if the spreadsheet is created. I.E If the property ssId exists
  143.   {
  144.    ssNew = SpreadsheetApp.openById(ssId); // open the spreadsheet
  145.    var sheet = ssNew.getSheets()[0];   // get first sheet of spreadsheet
  146.    var cell = sheet.getRange("A1"); // target the first cell in the spreadsheet - A1
  147.     cell.clear(); // clear the data in A1
  148.    cell.setValue(e.parameter.url); // set the value of the A1 cell to the url of the server
  149.    props.setProperty('URL',url); // sets a property URL as the url of the server
  150.    
  151.    
  152.     var payload =
  153.    {
  154.      "url_added":"url_added" // body of method (POST,GET,PUT,etc). Sent as key/value pair. Can be parsed by llParseString2List(body,["="],[])
  155.    };
  156.  
  157.      var options =
  158.    {
  159.       "method" : "post",  // Method like HTTP_METHOD in the llHTTPRequest() function. We'll make a POST.
  160.      "payload" : payload  // what variables we are sending back to the server/client.
  161.    };
  162.  
  163.      var response = UrlFetchApp.fetch(url,options); // Make a http request back to the URL of the server or client that made the request.
  164.  
  165.   } // if(ssId)
  166.  
  167.   else // if spreadsheet is not created i.e there is no property named ssId in script properties
  168.   {
  169.    var ssName = e.parameter.spreadsheet;  // the name of the spreadsheet set in the server script
  170.    
  171.     ssNew = SpreadsheetApp.create(ssName); // create a new spread sheet
  172.    
  173.     props.setProperty('ssId', ssNew.getId()); // sets a property ssID and set the vaue of the property to the id of the spreadsheet
  174.    
  175.      var payload =
  176.    {
  177.      "SpreadsheetReady":ssNew.getId() // body of method (POST,GET,PUT,etc). Sent as key/value pair. Can be parsed by llParseString2List(body,["="],[])
  178.    };
  179.  
  180.      var options =
  181.    {
  182.       "method" : "post",  // Method like HTTP_METHOD in the llHTTPRequest() function. We'll make a POST.
  183.      "payload" : payload  // what variables we are sending back to the server/client.
  184.    };
  185.  
  186.      var response = UrlFetchApp.fetch(url,options); // Make a http request back to the URL of the server or client that made the request.
  187.    
  188.    
  189.   }
  190.  
  191.  
  192. }
RAW Paste Data