Using Parse's JavaScript framework with Google Apps Script - javascript

I would like to use the Parse framework directly in Google Apps Script and copied the following source code from Parse.com directly into my project.
However, it seems that there are some adjustments required to get this to work correctly. e.g. when running the following sample code...
function upload()
{
Parse.initialize("wCxiu8kyyyyyyyyyyyyyyyyy", "bTxxxxx8bxxxxxxxxx");
var TestObject = Parse.Object.extend("TestObjectJSSSSS");
var testObject = new TestObject();
testObject.save({foo: "bar"}, {
success: function(object) {
alert("yay! it worked");
}
});
}
… I get the error message TypeError: Cannot call method "getItem" of undefined.
which seems to relate to localStorage. I believe I should replace localStorage with a similar storage type available in Google Apps Script. Would that be possible and how would I need to adjust the Parse code?
What other adjustments would I need to get to the Parse framework to work in my Google Apps Script project?

I would suggest that using Parse REST API would be a far simpler solution and it is meant for such cases.

Related

doPost(e) with no result - Google script as API

I can't seem to get the simplest doPost to reply in Scripts, tried a few things from around the internet, and cannot get a Post to reply.
I have reduced it to as simple as possible to get a reply, as below.
function doPost(e) {
var HTMLString = "<style> h1,p {font-family: 'Helvitica', 'Arial'}</style>"
+ "<h1>Hello World!</h1>"
+ "<p>Welcome to the Web App";
HTMLOutput = HtmlService.createHtmlOutput(HTMLString);
Logger.log(HTMLOutput);
// Return plain text Output
return ContentService.createTextOutput(HTMLOutput);
}
Deployment settings:
I am using postman to send the POST
No result.
Any ideas?
I understood your setting of Web Apps from I have edited the question to show the deployment settings are "me" and "anyone".
About Not sure what you mean by "Reflect your latest script" to obtain a result sorry, can you explain more?, when you use Web Apps, it is required to prepare a script with Google Apps Script. In this case, when you modify your script, it is required to reflect the latest script to the current or a new version of the deployed Web Apps. Here, when the script is reflected in Web Apps as a new version, the deployment ID is changed. By this, the endpoint of Web Apps is also changed. I thought that in this case, it might not be useful for your situation. So, I proposed to reflect the latest script in Web Apps without changing the endpoint of Web Apps. If you cannot understand it, I thought that this post might be useful. Ref (Author: me)
About The goal is to create an API, that will do things, but I haven't been able to get a result for a doPost at all. I have managed to doGet with a result, which is lovely, but I want the Post version., unfortunately, in your current script, an object of "HtmlOutput" is returned by return ContentService.createTextOutput(HTMLOutput);. By this, a text of "HtmlOutput" is returned. If you want to put a value to Web Apps as the POSt method and you want to retrieve the returned value, how about the following modification?
function doPost(e) {
return ContentService.createTextOutput(JSON.stringify(e));
}
When you modified your script of Web Apps, please reflect the latest script to the Web Apps. In this case, when you want to use the endpoint of Web Apps without changing the endpoint, please check this post.
For example, when the HTTP request is run by including the request body of '{"key":"value"}' as a text with the POST method, {"parameter":{},"postData":{"contents":"'{\"key\":\"value\"}'","length":17,"name":"postData","type":"text/plain"},"parameters":{},"contextPath":"","contentLength":17,"queryString":""} is returned.
Note:
From your showing script, when you want to return your value of HTMLString, the modified script is as follows. But, in this case, the HTML is returned as text. Please be careful about this. If you want to see the rendered HTML, please use doGet and return return HtmlService.createHtmlOutput(HTMLString);.
function doPost(e) {
var HTMLString = "<style> h1,p {font-family: 'Helvitica', 'Arial'}</style>"
+ "<h1>Hello World!</h1>"
+ "<p>Welcome to the Web App";
HTMLOutput = HtmlService.createHtmlOutput(HTMLString).getContent();
return ContentService.createTextOutput(HTMLOutput);
}
Reference:
Taking advantage of Web Apps with Google Apps Script (Author: me)
Redeploying Web Apps without Changing URL of Web Apps for new IDE (Author: me)

Getting access to a XML from javascript without Node-Without JQuery

I am trying to developp modifications to a game. The thing is the game is already compiled and the developpers prefer not to decompile the game (for the time beeing). Because of the compilation probably, everytime I try to load JQuery or Node.js whatever version I get the error "that a key already exists in the dictionary". The thing is everything is fine without Node.js or JQuery.js.
What I am trying to achieve is add some features to the game that unfortunately aren't available through the Game's API function call itself. I want to be able to get access to data Inside .xml files used for items/weapons/devices/engines spécifications of items Inside the game. I've tried pretty much all I could find on Stackexchange with what I searched for which was Node and JQuery. Im sorry if you guys think this is a duplicate question. Because it isn't. I can't use Node.js neither can i use JQuery. What else could I try? can someone help me please.
I am a bit new to programing with only 1 year experience in c# and Javascript. Sorry if this feels really noObish to you guys.
What you need is ajax. Modern browsers provide a pretty functional XMLHttpRequest, so you don’t even need a framework anymore.
One important thing to know: you most likely won’t be able to download the xml file using ajax if it’s on a distant server, due to the same-origin policy. You need a reliable access to it. The most convenient solution is to have a copy of the file on a local server such as WAMP, XAMPP, and the like.
I’m not going to write yet another ajax tutorial. Insteal I’ll just provide you with a working minimal HTML page, and point you towards XMLHttpRequest documentation.
<button>Request</button>
<script>
'use strict';
document.querySelector('button').addEventListener('click',
function () {
let req = new XMLHttpRequest();
req.onload = function () {
if (this.responseXML) {
console.log(this.responseXML);
}
else {
console.log(this.responseText);
}
};
req.open('GET', xmlURL); // xmlURL should be the location of the .xml file
req.send();
});
</script>
When you click on the button, the script will request, and then display the server’s response, if any, in your browser console. To open the console, press F12 and select the console tab.
Be aware that the responseXML property will only be populated if the xml sent by the server is strictly well-formed. Xml parsing in JS is somewhat finicky, so you may want to rely on responseText as a fallback.

REST Api: how to pass the JSONresponse to Javascript?

I just set up my first web service (REST Api) and it works correctly (checked with postman plugin on Chrome). I have three arguments that are in the JSONresponse:
{ "a": 1,
"b": 5,
"c": 2
}
But now I want to pass these three arguments to javascript-code to use them there (for example printing them), but because I'm totally new to this, I have no idea how to do this.
I tried something like this:
var req = new XMLHttpRequest();
var url = "SomethingLike:/api/v1.0/name"
xmlhttp.onreadystatechange = function() {
var jsonResponse = JSON.parse(req.responseText);
myFunction(jsonResponse)
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(data) {
console.log(data)
}
But when I run it with Sublime Text I get this error:
Exception: ReferenceError: Can't find variable: XMLHttpRequest
From comments:
Exception: ReferenceError: Can't find variable: XMLHttpRequest
I'm running JavaScript in sublime
target platform is JavaScript embedded in a webpage
XMLHttpRequest is not native JavaScript, but is a standard object made available to the JS environment by web browsers.
I'm not entirely sure how you are getting Sublime to execute the JS, but it probably involves using Node.js which doesn't provide XMLHttpRequest.
If you want to develop JS that is intended to run in a webpage, then your first port of call should be to create a webpage and embed the JS in it, then load that page in a web browser. (Make sure you open the Developer Tools and look at the Console to see any error messages).
It can be useful to write JS code which runs in a browser and on Node.JS. I did it to write unit tests for a library a few years ago. That used XMLHttpRequest from NPM to make the object available to my code in Node.js.
However, once you start getting into the realm of DOM manipulation then it usually becomes more useful to test code in a browser environment such as is provided by PhantomJS or Selenium.

control JSON is undefined in Windows Phone WebBrowser (IE)

I am currently building an application on Windows Phone 7 using the WebBrowser control. The WebBrowser is navigated to a URL, but I also inject my own javascript into the control.
However the code which seems to work well on other platforms (WebView for Android and iOS) doesn't seem to work in the WebBrowser:
function parseToString(outObject)
{
var outJSON = null;
try{
if(outObject != null){
outJSON = JSON.stringify(outObject);
}
}
catch(err)
{
outJSON = err.message;
}
window.external.Notify(outJSON);
}
I found out that the "control JSON is undefined" is thrown in the try block.
Can someone recommend an alternative method for stringifying the JSON in the WebBrowser/IE? Furthermore, I cannot use external libraries as those javascripts are injected into the WebBrowser.
The WP7 browser does not supply a JSON object, so you will need to use a third party library in order to convert an object into a JSON string. I have used both of these in the past:
Douglas Crockford's JSON library.
The knockoutJS toJSON function.
See this related question:
Serializing to JSON in jQuery
I had the same problem, but then added the following to the top of the HTML document:
<!DOCTYPE html>
This unfortunately doesn't help if you don't have access to the HTML document and are only injecting JavaScript, but someone might find it useful.

How do you use the Facebook Graph API in a Google Chrome Extension?

I have been trying to access the information available when using the https://graph.facebok.com/id concept through JSON but have been unable to call or return any information based on different snippets of code I've found around. I'm not sure if I'm using the JSON function correctly or not.
For example,
var testlink = "https://graph.facebook.com/"+id+"/&callback=?";
$.getJSON(testlink,function(json){
var test;
$.each(json.data,function(i,fb){
test="<ul>"+json.name+"</ul>";
});
});
In this code, I am trying to return in the test variable the name. When I use this in a Google Chrome Extension, it just returns a blank page.
Alternatively, I've been also trying to use the Facebook Javascript SDK in my Google Chrome extension, but I am unsure what website I should be using when signing up for an API Key.
I believe that you need to establish either an OAuth session or provide your API key before you can talk to FB. It's been a while since I messed around with FB api but, I'm pretty sure you have to register before you can use the api.
Here's something that might be useful though, it's a javascript console for Facebook which would allow you to test out your code! http://developers.facebook.com/tools/console/
It's an issue with chrome, but I haven't figured out the exact problem. For example open the chrome inspector and type this into it:
$.getJSON("http://graph.facebook.com/prettyklicks/feed?limit=15&callback=?", function(json){console.log(json);});
Then do the same thing in Firefox. Chrome will return nothing, but FF will give you the JSON object. It's not the JSON call itself because if you call something else for instance:
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", function(data) {console.log(data);});
It will come through normally. So there is some miscommunication between chrome and fb. The really confusing part is that if you browse directly to the graph entry by just pasting it into your address bar it will come through normally that way too.

Categories

Resources