Here are a couple of examples of the library in use:
- Use the Google Feeds API to retrieve the top slashdot post:
http://hg.mozilla.org/users/alex_croczilla.com/oni/raw-file/tip/ajax/samples/google_feeds.html
<script>
var feed = "http://rss.slashdot.org/Slashdot/slashdot";
SLift(function(txt) {
document.getElementById("feed").innerHTML = txt; }
)(ObjRef(Google.Feeds.Load(feed), "entries", "1", "content")).run();
</script>
<div id="feed"/>
- Use the Google Feeds & Language APIs to retrieve the top slashdot
post and translate it to Spanish & Japanese:
http://hg.mozilla.org/users/alex_croczilla.com/oni/raw-file/tip/ajax/samples/google_translated_feeds.html
<script>
var feed = "http://rss.slashdot.org/Slashdot/slashdot";
Let({ post : ObjRef(Google.Feeds.Load(feed), "entries", "1",
"content") },
SLift(function(orig, spanish, japanese) {
document.getElementById("feed").innerHTML =
"<h2>Original:</h2>" + orig + "<hr>" +
"<h2>Spanish:</h2>" + spanish + "<hr>" +
"<h2>Japanese:</h2>" + japanese + "<hr>";
})(Get("post"),
ObjRef(Google.Language.Translate(Get("post"), "en", "es"),
"translation"),
ObjRef(Google.Language.Translate(Get("post"), "en", "ja"),
"translation"))).run();
}
</script>
<div id="feed"/>
At the moment the library (which is checked in here: http://hg.mozilla.org/users/alex_croczilla.com/oni/file/tip/ajax/oni-ajax.js)
contains some generic datastructure manipulation code that I'm
planning to put into oni.js at some point:
- Scheme/Lisp-inspired predicates and functions for List manipulation
(IsNull, Cons, Car, Cdr, Member, Remove, ListRef, ListTail, etc)
- 'JSON' object construction/referencing (Obj, ObjRef)
Then there are a few DOM-specific functions:
- DOM.WaitForEvent(event, element) : blocks until event fires
- DOM.ElementById(id) : == document.getElementById
- DOM.Alert(message) : == window.alert
Next there is a function for dynamically loading scripts:
- LoadScript(url) : blocks until the script from url is loaded
And finally, there are some wrappers for Google AJAX APIs (see http://code.google.com/apis/ajax/documentation/)
- Google.EnsureAPI() : Ensures that the Google API is loaded. This is
called implicitly inside the library most of the time. The only reason
you would want to call this is to ensure that datastructures such as
google.loader.ClientLocation are initialized.
- Google.Load(moduleName, moduleVersion) : The Oni version of
google.load(), mostly called implicitly inside the library.
- Google.Feeds.Load(url, [optional] numEntries) : retrieve rss feed at
url. Returns a result object as described at http://code.google.com/apis/ajaxfeeds/documentation/reference.html
.
- Google.Language.Translate(text, srclang, destlang, [optional]
type) : The Oni version of google.language.translate (see http://code.google.com/apis/ajaxlanguage/documentation/)
, with some twists. E.g. the oni version chunks up text, so it can be
used to translate arbitrarily long texts (well, bounded by the js
engine, and not the 2000 character URL limit anyway).
- Google.Language.Detect(text) : Tries to detect the language of text.
- Google.Language.GetBranding(elem, [optional] options) : Appends
Google attribution branding to elem.
Future plans:
- Add wrappers for more Google APIs
- Add support for cross-domain XMLHttpRequests, probably using flensed
flXHR (http://flxhr.flensed.com).
- Start looking at wrappers for other APIs. Suggestions welcome.