Is the ReCaptcha "customization options dictionary" JSON? - javascript

This is something of an embarrassing question to ask, seeing as it is mainly about my lack of Javascript knowledge. Seems like it might be "so obvious" that nobody specifies it anymore. Gotta start somewhere! I'm a proficient coder in other languages, I'm just unfamiliar with JS.
On the ReCaptcha Client API docs page, there's an example of the proper format for the customization options dictionary.
I'm writing a 3rd party app which generates that dictionary based on user prefs. I don't know of / couldn't find a dictionary type in Javascript. My best guess is that the dictionary is JSON. I want to make sure that my output is valid and conformant to what they expect, particularly with regards to quotes (at all? single? double? etc.)
Is it JSON? Is it another type of dictionary? Can anybody point me at some kind of specs for how a valid instance of a JS dictionary looks? Googling for "Javascript Dictionary" has proved ineffective owing to the generic quality of the search terms.
Thanks!

It's a javascript object literal, which is almost the same as JSON (it's a superset).
Any valid JSON will work here, but as you can see from their example, Javascript has a few other options:
<script>
var RecaptchaOptions = {
theme : 'white',
tabindex : 2
};
</script>
The part between the braces is valid Javascript but not valid JSON because theme, tabindex and white should all be double-quoted. Below is valid JSON embedded in Javascript, which works perfectly:
<script>
var RecaptchaOptions = {
"theme" : "white",
"tabindex" : 2
};
</script>

No, it’s not JSON, it’s plain JavaScript. More exact: It is a simple object declared using the object initialiser syntax { … }.
That syntax is similar to JSON as JSON is a subset of JavaScript. But JSON requires the property names to be a quoted string, JavaScript not.

Related

Is JSON (still) a valid JavaScript code aka. moving objects from backend to frontend

A rather theoretical question:
Let's say we have some object in the backend (let's ignore what technology and language it is) and we want to move (or actually copy) it into JavaScript code on the frontend side during rendering the page.
The reason why I'm asking about JSON is that many backends have some kind of json-ify function that takes an object in this language and produces JSON string out of it. It's tempting to use that in this way:
<script>
var myObject = ( <%= backendObject.to_json() %> );
// ...
</script>
but I'm not sure if this will work for all cases. For one, I know that objects in JS are written like this:
{
x: 10,
y: 20
}
and in JSON like this:
{
"x": 10,
"y": 20
}
but I'm sure about this either. On the other side JSON acronym is expanded to "JavaScript Object Notation", so it should have some common ground with JS. However I don't know if it still has.
PS: I know it may be simpler to render a page with JS code that fetch()-es the object from the backend, which json-ifes it. But it will be two HTTP requests. Besides sometimes you have got the object right now when the first request is made.
According to the official documentation, it is a subset of a version of JavaScript (ECMA-262 3rd Edition). So I guess the answer is "yes".
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language Standard ECMA-262 3rd Edition - December 1999.
Edit: upon further reading it seems that there may be one technicality relating to new line characters U+2028 and U+2029 that JSON can have inside strings, but JavaScript can't. However, you can escape them as \u2028 and \u2029.

Go templating engine that also runs in the browser

I'm developing a web application with Go on the server, and the router will use PushState, so the server will also have to be able to render my templates. That means that I'll need a templating engine that works with Go and Javascript. The only one I've come across so far is Mustache, but it doesn't seem to be able to handle lowercase properties of structs, and there also doesn't seem to be a possibility to provide custom names like JSON:
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
So, is there a templating engine that is available in both Go and JavaScript, and that can handle lowercase struct properties?
As the comments above state, you can't expect any 3rd party library to be able to read lowercase properties on your struct, but it appears you are trying to use tags to represent alternative representations of your struct (as you can with the encoding/json library).
What you can do is use something like github.com/fatih/structs to convert your structs to maps and then range through to lowercase all of your keys (copying the values and removing the uppercase versions) and pass it into mustache.Render() as your context. If you want to use struct tags like the encoding/json library does, you'd have to use the reflect package and write a struct-to-map function that takes into account the tags on the struct (basic example given in the documentation here). There are some SO answers on how to write a struct-to-map function using reflection, which you can improve upon to add struct tag handling as you need.
To answer your question, I don't think this is something a current templating library does that also works with javascript, but it shouldn't be too hard to get working with mustache given the idea above.

How to encode/decode URL parameters in javascript?

Before anyone jumps in and says, "Oh!! that's a bad idea", I know it is.
I want to keep both the key and value in the query string to be not easily visible to the end user.
I have something like this google.com/?category=textile&user=user1
I need to make it unintelligible like this: google.com/?kasjdhfkashasdfsf32423
Is there any way to achieve this in javascript. I have already seen this
I have already seen this
and this.
but I don't think encoding will solve the problem. Also, this code is entirely in client side. I know that it is not secure but I just need this is a naive, weak defense.
Please help.
Edit
I apologize if my question was not clear earlier.
The URL google.com/?category=textile&user=user1 is being passed on from a different application.
The values passed in the query string directly controls what is being displayed to the user. As is, anyone with no technical knowledge can easily change the value and view the data corresponding to a different category or user. I need to make this unintelligible so that it is not obvious. If a user is a techie and figures out the encryption used, then it is fine. I need a stop-gap solution till we have a better architecture in place
You can use base64. Javascript has native functions to do that :
alert(btoa("category=textile&user=user1")); // ==> Y2F0ZWdvcnk9dGV4dGlsZSZ1c2VyPXVzZXIx
and to reverse it :
alert(atob("Y2F0ZWdvcnk9dGV4dGlsZSZ1c2VyPXVzZXIx")); // ==> category=textile&user=user1
Be careful to read the doc if you have unicode strings, it's a little different : https://developer.mozilla.org/en-US/docs/Web/API/Window.btoa
If you don't looking for serious strong crypto, you can use ROT13:
http://en.wikipedia.org/wiki/ROT13
This is enough for slightly obfuscate keys/values in the your URLs.

Flash Twitter API with JSON

I have read a lot about parsing JSON with Actionscript. Originally it was said to use this library. http://code.google.com/p/as3corelib/ but it seems Flash Player 11 has native support for it now.
My problem is that I cannot find examples or help that takes you from beginning to end of the process. Everything I have read seems to start in the middle. I have no real experience with JSON so this is a problem. I don't even know how to point ActionScript to the JSON file it needs to read.
I have a project with a tight deadline that requires me to read twitter through JSON. I need to get the three most recent tweets, along with the user who posted it, their twitter name and the time those tweets were posted.
The back end to this is already set up I believe by the development team here, therefor my JSON files or XML just needs to be pointed to and then I need to display the values in the interface text boxes I have already designed and created.
Any help will be greatly appreciated...I do know that there are a lot of threads on here I just do not understand them as they all have some understanding of it to begin with.
You need to:
Load the data, whatever it is.
Parse the data from a particular format.
For this you would normally:
Use URLLoader class to load any data. (Just go to the language reference and look into example of how to use this class).
Use whatever parser to parse the particular format that you need. http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/JSON.html this is the reference to JSON API, it also shows usage examples. I'm not aware of these API being in production version of the player, still there might be quite a bit of FP 10.X players out there, so I'd have a fallback JSON parser, but I would recommend using this library: http://www.blooddy.by/en/crypto/ over as3corelib because it is faster. The built-in API are no different from those you would find in browser, so if you look up JSON JavaScript entries, the use should be in general similar to Flash.
After you parse JSON format, you will end up with a number of objects of the following types: Object, Array, Boolean, Number, String. It has also literals to mean null and undefined. Basically, you will be working with native to Flash data structures, you only should take extra care because they will be dynamically constructed, meaning you may not make assumption about existence of parts of the data - you must always check the availability.
wvxvw's answer is good, but I think skips over a to be desired explanation of what JSON itself is. JSON is plain text, javascript object notation, when you read the text on screen it looks something like this
http://www.json.org/example.html
you can see a side by side JSON and XML (both plain text formats) essentially JSON is a bunch of name value pairs.
When you use JSON.parse("your JSON string goes here") it will do the conversions to AS3 "dynamic objects" which are just plain objects (whose properties can be assigned without previously being defined, hence dynamic). But to make a long story short, take the example you see in the link above, copy and paste the JSON as a string variable in AS3, use
var str:String = '{"glossary": {"title": "example glossary","GlossDiv": {"title": "S","GlossList": {"GlossEntry": {"ID": "SGML","SortAs": "SGML","GlossTerm": "Standard Generalized Markup Language","Acronym": "SGML","Abbrev": "ISO 8879:1986","GlossDef": {"para": "A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso": ["GML", "XML"]},"GlossSee": "markup"}}}}}';
var test:Object = JSON.parse(str);
method on the string, store it in a variable and use the debugger to see what the resulting object is. As far as I know there's really nothing else to JSON it's simply this format for storing data (you can't use E4X on it since it's not XML based and because of that it's slightly more concise than XML, no closing tags, but in my opionion slightly less readable... but is valid javascript). For a nice break-down of the performance gains/losses between AMF, JSON and XML check out this page: http://www.jamesward.com/census2/ Though many times you don't have a choice with regard to the delivery message format or protocol being used if you're not building the service, it's good to understand what the performance costs of them are.

localizing strings in javascript

We need to be able to localize strings in javascript - thinking for things like the app_offline.htm file etc.
jquery globalize is hectic and seems like total overkill. Is there a simple jquery plugin or anything really that will allow us to localize js strings?
At the risk of over simplifying:
var globals = {
en-US: {
color:'color',
cell:'cell phone'
},
en-GB: {
color: 'colour',
cell: 'mobile phone'
}
};
To use:
text = globals[lang].color;
where lang = 'en-US' etc
You can either generate that structure on the server and use resource files etc there, or just keep this object literal in a global.js or similar.
The Globalize.js library, previously known as jquery-global or jQuery Globalize, is relatively small, but if you only need string localization (and not date and number localization as well), then it does not offer much more than a general setup: locale (culture) object containing the property messages. It is initialized to an empty object, and you are supposed to add properties to it, corresponding to your strings to be localized. And it has the simple method Globalize.localize() that selects a localized string for a key
To implement simple string localization, you do not necessarily need any library or plugin. You could just code some simple approach like that of Globalize.js; the general code is fairly simple, much less work than defining the actual localizations for each string. On the other hand, if you have localization needs, you might just as well use Globalize.js, preparing for other kinds of localization in the future.

Categories

Resources