How To Add Cavern Status To Your Own Website

Domain Resource Management & Use (Subdomains, FTP, Email, CNames)

Moderators: OpenUru.org Moderators, Domain Managers

Leonardo
Member
Posts: 35
Joined: Sat Dec 13, 2008 5:57 pm

Re: How To Add Cavern Status To Your Own Website

Post by Leonardo »

ok. JSONP is basically a javascript object. This is a workaround to the same-origin-policy, because <script> are not subject to it.
A "JSONP server" should accept a GET argument that is usually named "jsoncallback".
The format of the returned text should look like a call to a function with a JSON object passed as argument, the name of the function that will be called is defined by the jsoncallback GET argument, if it is a question mark the name of the function should be empty. To stay on the safe side, please return a HTTP header with this content type: "application/x-javascript"
As an example see the Flickr JSONP service, which allows to fetch images from Flickr with javascript.
I have thought of two possible formats, I'll let you choose which you want to use:
Format 1:
Use this format if you want to "force" the requesting website to use your HTML formatting. I say "force" because with jQuery it is way too easy to manipulate HTML and exclude parts of the received text.
If I make this GET request: http://http://www.openuru.org/tools/cav ... rseOpenUru
and MOULa is open, I should get the following response:

Code: Select all

parseOpenUru({'widgetHTML':'<div id="cspanel"><span class="cshead">Cavern Status:</span><img src="http://www.openuru.org/sitestyle/img/led-green.gif" /> Open for exploration! <div id="cavstat_link"> - <a href="http://mystonline.com/play/"> Sign up to play at MystOnline.com!</a></div><div id="cavstat_row2"><span class="cshead">Developer Message:</span> Welcome to Myst Online: Uru Live!</div><div style="font-size: 8px; text-align: right;"><a href="http://www.openuru.org/">OpenUru.org</a></div></div>'})
Basically I put your HTML as a string in a JSON object.

Format 2:
In this format you give complete freedom to the requesting website to choose which parts they want and which HTML format they want to use. Here you only provide information and leave it up to the webdesigner to choose an HTML structure to put it in. This is extremely flexible to give people the freedom to format the widget in any way they want.

Code: Select all

parseOpenUru({'statusImg':'http://www.openuru.org/sitestyle/img/led-green.gif', 'statusText':'Open for exploration!','moulLink':'http://mystonline.com/play/','moulText':'Sign up to play at MystOnline.com!','devMsg':'Welcome to Myst Online: Uru Live!','providerLink':'http://www.openuru.org/','providerName':'OpenUru.org'})
Basically I have taken all the infos you have on the widget and put it one by one in a JSON object.
If you use this second format you can also rewrite the PHP code to use it, because PHP has the json_decode function that accepts a JSON formatted string and returns an associative array containing all the members of the JSON object. This way the PHP code will be completely HTML independent too.

Or you could even use a merge of the two formats (i.e. add the 'widgetHTML' member to the format 2 JSON object), so who wants to use their format for the widget they can do it, and the lazy webdesigner can just pick the whole HTML and put it in his webpage.

^^

EDIT: I'm sorry I didn't see the edit to you message MacFife. Yes your example is exactly what I need XD
User avatar
Mac_Fife
Member
Posts: 1239
Joined: Fri Dec 19, 2008 12:38 am
Location: Scotland
Contact:

Re: How To Add Cavern Status To Your Own Website

Post by Mac_Fife »

Thanks for that Leonardo :)

For jQuery support, I'd very much lean towards Format 2 and allow the user control over what they use and how they show it: That's pretty much what the PHP version allows you to do, and I'd like these methods to give the page designer freedom over their layout. If you don't want/need that level of control then the simple iFrames are easier to use.

OK, I've done some of the re-coding, but I'm finding some conflicting info, so a clarification would help: In your example and text you said that the argument is usually named jsoncallback and gave the example "?type=jsonp&jsoncallback=parseOpenUru". So if I allowed you to make a request like that, I'm assuming that "parseOpenUru" is a local function name that is essentially arbitrary, but needs to be the "padding" at the start of the response, so I'd begin by sending "parseOpenUru((...". Equally, if the GET argument was "?type=jsonp&jsoncallback=myCavernStatus", the response should start "myCavernStatus((...".

If my interpretation is right, then there's a (non-operational) sample that you can test for me using this URL:

Code: Select all

http://ccgi.mcintoshuk.plus.com/oucs/cavstat.php?type=jsonp&jsoncallback=anyCallBackFunctionNameYouLike
The above should spit out the following:

Code: Select all

anyCallBackFunctionNameYouLike(("LED": "http://www.openuru.org/sitestyle/img/led-green.gif", "State": "Open for exploration!", "Link": "Sign up to play at MystOnline.com!", "LinkURL": "http://mystonline.com/play/", "Message": "Welcome to Myst Online: Uru Live! Bug Chucker now available on Android Marketplace!", "Origin": "www.openuru.org", "OriginURL": "http://www.openuru.org/")) 
I don't know how important white space is in the output, or if newlines will "break" it, so for now I've kept it simple, if messy to read, so for ease of reference, here is a formatted version so you can see the array members I'm using:

Code: Select all

myCavStat((
  "LED": "http://www.openuru.org/sitestyle/img/led-green.gif",
  "State": "Open for exploration!",
  "Link": "Sign up to play at MystOnline.com!",
  "LinkURL": "http://mystonline.com/play/",
  "Message": "Welcome to Myst Online: Uru Live! Bug Chucker now available on Android Marketplace!",
  "Origin": "www.openuru.org",
  "OriginURL": "http://www.openuru.org/"
))\n 
Mac_Fife
OpenUru.org wiki wrangler
Leonardo
Member
Posts: 35
Joined: Sat Dec 13, 2008 5:57 pm

Re: How To Add Cavern Status To Your Own Website

Post by Leonardo »

Thank you MacFife!
Yes you have interpreted everything correctly. I'm not sure about the spacing either, I'd keep it without additional newlines, just to be sure.
That format should be correct to get javascript to fetch content with a cross domain request :)
User avatar
Mac_Fife
Member
Posts: 1239
Joined: Fri Dec 19, 2008 12:38 am
Location: Scotland
Contact:

Re: How To Add Cavern Status To Your Own Website

Post by Mac_Fife »

Great! I'll get the update into the repo, and hopefully JW will push it to the server soon. I've also changed the "Play" link in the "regular" versions of the widget to open in a new window instead of inside the iFrame.
Mac_Fife
OpenUru.org wiki wrangler
Christian Walther
Member
Posts: 317
Joined: Sat Dec 13, 2008 10:54 am

Re: How To Add Cavern Status To Your Own Website

Post by Christian Walther »

I’m no JavaScript guru, but I think there should be curly braces, not parentheses, around the JSON object. At least my Safari JavaScript console complains about the parentheses.

Code: Select all

myCavStat({"LED": …})
not

Code: Select all

myCavStat(("LED": …))
User avatar
Mac_Fife
Member
Posts: 1239
Joined: Fri Dec 19, 2008 12:38 am
Location: Scotland
Contact:

Re: How To Add Cavern Status To Your Own Website

Post by Mac_Fife »

Yes, I think you're right Christian - the text I was reading made it difficult to differentiate between {...} and (...). I'll get that fixed.
Mac_Fife
OpenUru.org wiki wrangler
Leonardo
Member
Posts: 35
Joined: Sat Dec 13, 2008 5:57 pm

Re: How To Add Cavern Status To Your Own Website

Post by Leonardo »

ugh, I'm sorry I didn't notice. That's what happens when I work till late night :lol:
User avatar
JWPlatt
Member
Posts: 1137
Joined: Sun Dec 07, 2008 7:32 pm
Location: Everywhere, all at once

Re: How To Add Cavern Status To Your Own Website

Post by JWPlatt »

I pushed the update out last night. Which file needs the myCavStat correction?
Perfect speed is being there.
User avatar
Mac_Fife
Member
Posts: 1239
Joined: Fri Dec 19, 2008 12:38 am
Location: Scotland
Contact:

Re: How To Add Cavern Status To Your Own Website

Post by Mac_Fife »

I've just committed an update into the repo, so it's just a case of pushing that to the server. I also tightened up the validation of the request URL to avoid a predictable error. I'd have done this earlier today, but I couldn't remember the repo URL when I was using my laptop :roll: .

Edit: I've now added a section to the wiki page to document the JSONP type option. I'll leave further documentation on practical usage examples with jQuery to people who know about that sort of thing!
Mac_Fife
OpenUru.org wiki wrangler
User avatar
JWPlatt
Member
Posts: 1137
Joined: Sun Dec 07, 2008 7:32 pm
Location: Everywhere, all at once

Re: How To Add Cavern Status To Your Own Website

Post by JWPlatt »

Applied, thanks! :)
Perfect speed is being there.
Post Reply

Return to “Domain (OpenUru.org)”