This script can be placed on a webpage. It will execute the Pipe from the clients machine and return it's content without using an additional webserver:
<script>
/* these calls are just the plumbing */
pipesrpc = {};
pipesrpc._timeoutlength = 30000; /* 30 seconds by default */
pipesrpc._running = [];
pipesrpc._timeout = function(id,url) {
var cbo = pipesrpc._running[id];
pipesrpc._running[id]=null;
if (!cbo.callbackErr) return;
cbo.callbackErr("Timeout",-1,cbo.self);
}
pipesrpc._buildurl = function(pipeid,params) {
var url = "http://pipes.yahoo.com/pipes/pipe.run?_id="+pipeid+"&_render=json";
if (params) {
for (var key in params) {
if (params[key]===null) continue;
url+="&"+encodeURIComponent(key)+"="+encodeURIComponent(params[key])
}
}
return url;
}
pipesrpc._callbackhandler = function(o) {
var cbo = pipesrpc._running[callbackIndex];
if (!cbo) return;
pipesrpc._running[callbackIndex]=null;
window.clearTimeout(cbo.timeout);
if (!o || !o.count) {
if (!cbo.callbackErr) return;
cbo.callbackErr("Bad response",-2,cbo.self);
return;
}
if (!cbo.callbackOk) return;
cbo.callbackOk(o,cbo.self);
}
pipesrpc._execute = function(url,callbackOk,callbackErr,timeoutlength) {
if (!timeoutlength) timeoutlength = pipesrpc._timeoutlength;
var id = pipesrpc._running.length;
url+="&_callback=pipesrpc._callbackhandler_"+id;
var s=document.createElement("script");
s.setAttribute("src",url);
var fn = ""+pipesrpc._callbackhandler;
fn = fn.replace(/callbackIndex/g,id);
eval("pipesrpc._callbackhandler_"+id+"="+fn);
pipesrpc._running.push({self:this,callbackOk:callbackOk,callbackErr:callbackErr,timeout:window.setTimeout(function() { pipesrpc._timeout(id,url); },timeoutlength)});
document.getElementsByTagName("head")[0].appendChild(s);
return id;
}
/* use these three calls to run and cancel Pipes calls */
pipesrpc.cancelrequest = function(id) {
var cbo = pipesrpc._running[id];
window.clearTimeout(cbo.timeout);
pipesrpc._running[id]=null;
}
pipesrpc.cancelallrequests = function() {
for (var i=0; i<pipesrpc._running.length; i++) {
pipesrpc._cancelrequest(i);
}
}
pipesrpc.run = function(pipeid,params,callbackOk,callbackErr,timeoutLength) {
return pipesrpc._execute(pipesrpc._buildurl(pipeid,params),callbackOk,callbackErr,timeoutLength);
}
</script>
When this script is put on your webpage it demonstrates how to run a Pipe and get the results back into a JavaScript function:
var requestId = pipesrpc.run("jL98xV_82xG2_bp1e_gC8A",null,
function(data) {
alert("Got " +data.count+" items from pipe "+data.value.title);
},
function(errorMessage,errorCode) {
alert("Problem running pipe: "+errorMessage);
}
);
This script demonstrates how to call a Pipe supplying parameters that correspond to the user inputs for a pipe:
var f_ok = function(data) {
alert("Got " +data.count+" items");
for (var i=0; i<data.count; i++) {
alert("Item "+i+" title is "+data.value.items[i].title);
}
}
pipesrpc.run("1mrlkB232xGjJDdwXqIxGw",{location:"palo alto, ca",mindist:2,what:"parks"},f_ok);
Finally, this example puts it all together by demonstrating a simple way to display a Pipe on a webpage.