Friday, August 20, 2004

Cool Web Service functions in Mozilla...

This Mozilla WSDL Example page shows how cool Mozilla can be... There is an on page example in which I can type some text, press a button, and it uses the BabelFish web service on xmethods to translate the text... Neat trick you say... This is cooler than you think because it's using Mozilla's built in web service functionality to make the request - There is no server side interface to xmethods... The browser is acting as a web service client to the babel fish service...

So that's nice and all - but the truly cool thing: There are links at the bottom of the page that I can drag onto my Mozilla toolbar (with my bookmarks...) ... I can then simply select some text on a page, and press the "bookmark" which translates the text I've highlighted! What's going on... Well the link that I dragged to my toolbar is some javascript:

javascript:
var proxy= null;
var wsdl_uri = "http://www.xmethods.net/sd/2001/BabelFishService.wsdl";
function Translate (aValue) {
if (!proxy) {
var listener = {
onLoad: function (aProxy) {
proxy = aProxy;
proxy.setListener(listener);
requestTranslation(aValue);
},
onError: function (aError) { },
BabelFishCallback : function (aTranslatedValue) { alert(aTranslatedValue); }
};
createProxy(listener);
} else {
requestTranslation(aValue);
}
}
function createProxy(aCreationListener) {
try {
var factory = new WebServiceProxyFactory();
factory.createProxyAsync(wsdl_uri, "BabelFishPort", "", true, aCreationListener);
} catch (ex) { alert(ex); }
}
function requestTranslation (value) {
if (proxy) {
proxy.BabelFish("en_fr", value);
} else {
alert("Error: Proxy set up not complete!");
}
}

if(window.getSelection()!="")Translate(window.getSelection());
else alert("please select a text before");void(0);


This is interesting on about 3 different levels:
1) I can execute javascript in Mozilla by clicking a bookmark in my toolbar...
2) This bit of code is truly interesting:

try {
var factory = new WebServiceProxyFactory();
factory.createProxyAsync(wsdl_uri, "BabelFishPort", "", true, aCreationListener);
} catch (ex) { alert(ex); }

I can now write javascript to more or less call any webservice I like (haven't tried it with complex data structures - but who knows...)
3) Why aren't there 1000 Mozilla webservice bookmarks out there for me to drop on my toolbar...

I'll be playing around with some of this today to see if I can get my own web service running using javascript, Perl, SOAP::Lite, IMDB::Film (!!) - so that as I'm looking at my video stores website, I can just highlight movie titles and get the IMDB movie rating... Will post an update later once I've got something working...

1 Comments:

Blogger Chris said...

Thanks! For some reason I had missed regularly cleaning this article. To fight comment spam, only registered bloggers can submit comments.

May 22, 2005 9:41 PM  

Post a Comment

<< Home