diff --git a/examples/direction.js b/examples/direction.js index 24df0c3b..7f42999a 100644 --- a/examples/direction.js +++ b/examples/direction.js @@ -15,185 +15,20 @@ if (phantom.state.length === 0) { } else { if (phantom.loadStatus === 'fail') { console.log('Unable to access network'); - phantom.exit(1); + } else { + var steps = phantom.content.match(/(.*)<\/html_instructions>/ig); + if (steps == null) { + console.log('No data available for ' + phantom.state); + } else { + steps.forEach(function (ins) { + ins = ins.replace(/\</ig, '<').replace(/\>/ig, '>'); + ins = ins.replace(/\
/g, ''); + console.log(ins); + }); + console.log(''); + console.log(phantom.content.match(/.*<\/copyrights>/ig).join('').replace(/<.*?>/g, '')); + } } - - direction = x2j(phantom.content); - if (typeof direction == 'undefined') { - console.log('No data available for ' + phantom.state); - phantom.exit(1); - } - - route = direction.directionsresponse.route; - route.leg.step.forEach(function (step) { - var ins = step.html_instructions; - ins = ins.replace(/\</ig, '<').replace(/\>/ig, '>'); - ins = ins.replace(/\
/g, ''); - console.log(ins + ' (' + step.distance.text + ')'); - }); - - console.log(''); - console.log(route.copyrights); phantom.exit(); } - -/* -xml2json v 1.1 -copyright 2005-2007 Thomas Frank - -This program is free software under the terms of the -GNU General Public License version 2 as published by the Free -Software Foundation. It is distributed without any warranty. -*/ -function x2j(xml) { - - xml2json={ - parser:function(xmlcode,ignoretags,debug){ - if(!ignoretags){ignoretags=""}; - xmlcode=xmlcode.replace(/\s*\/>/g,'/>'); - xmlcode=xmlcode.replace(/<\?[^>]*>/g,"").replace(/<\![^>]*>/g,""); - if (!ignoretags.sort){ignoretags=ignoretags.split(",")}; - var x=this.no_fast_endings(xmlcode); - x=this.attris_to_tags(x); - x=escape(x); - x=x.split("%3C").join("<").split("%3E").join(">").split("%3D").join("=").split("%22").join("\""); - for (var i=0;i","g"),"*$**"+ignoretags[i]+"**$*"); - x=x.replace(new RegExp("","g"),"*$***"+ignoretags[i]+"**$*") - }; - x=''+x+''; - this.xmlobject={}; - var y=this.xml_to_object(x).jsontagwrapper; - if(debug){y=this.show_json_structure(y,debug)}; - return y - }, - xml_to_object:function(xmlcode){ - var x=xmlcode.replace(/<\//g,"§"); - x=x.split("<"); - var y=[]; - var level=0; - var opentags=[]; - for (var i=1;i")[0]; - opentags.push(tagname); - level++ - y.push(level+"<"+x[i].split("§")[0]); - while(x[i].indexOf("§"+opentags[opentags.length-1]+">")>=0){level--;opentags.pop()} - }; - var oldniva=-1; - var objname="this.xmlobject"; - for (var i=0;i")[0]; - tagnamn=tagnamn.toLowerCase(); - var rest=y[i].split(">")[1]; - if(niva<=oldniva){ - var tabort=oldniva-niva+1; - for (var j=0;j") - } - } - else {rest="{}"}; - if(rest.charAt(0)=="'"){rest='unescape('+rest+')'}; - if (already && !eval(objname+".sort")){preeval+=objname+"=["+objname+"];\n"}; - var before="=";after=""; - if (already){before=".push(";after=")"}; - var toeval=preeval+objname+before+rest+after; - eval(toeval); - if(eval(objname+".sort")){objname+="["+eval(objname+".length-1")+"]"}; - oldniva=niva - }; - return this.xmlobject - }, - show_json_structure:function(obj,debug,l){ - var x=''; - if (obj.sort){x+="[\n"} else {x+="{\n"}; - for (var i in obj){ - if (!obj.sort){x+=i+":"}; - if (typeof obj[i] == "object"){ - x+=this.show_json_structure(obj[i],false,1) - } - else { - if(typeof obj[i]=="function"){ - var v=obj[i]+""; - //v=v.replace(/\t/g,""); - x+=v - } - else if(typeof obj[i]!="string"){x+=obj[i]+",\n"} - else {x+="'"+obj[i].replace(/\'/g,"\\'").replace(/\n/g,"\\n").replace(/\t/g,"\\t").replace(/\r/g,"\\r")+"',\n"} - } - }; - if (obj.sort){x+="],\n"} else {x+="},\n"}; - if (!l){ - x=x.substring(0,x.lastIndexOf(",")); - x=x.replace(new RegExp(",\n}","g"),"\n}"); - x=x.replace(new RegExp(",\n]","g"),"\n]"); - var y=x.split("\n");x=""; - var lvl=0; - for (var i=0;i=0 || y[i].indexOf("]")>=0){lvl--}; - tabs="";for(var j=0;j=0 || y[i].indexOf("[")>=0){lvl++} - }; - if(debug=="html"){ - x=x.replace(//g,">"); - x=x.replace(/\n/g,"
").replace(/\t/g,"    ") - }; - if (debug=="compact"){x=x.replace(/\n/g,"").replace(/\t/g,"")} - }; - return x - }, - no_fast_endings:function(x){ - x=x.split("/>"); - for (var i=1;i"+x[i] - } ; - x=x.join(""); - return x - }, - attris_to_tags: function(x){ - var d=' ="\''.split(""); - x=x.split(">"); - for (var i=0;i"); - x=x.replace(/ ([^=]*)=([^ |>]*)/g,"><$1>$2"/g,">").replace(/"