ed-ro0t / goosh

Automatically exported from code.google.com/p/goosh
0 stars 0 forks source link

currency/units convention #2

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I miss currency/units convention... I will be nice to have it in goosh

Original issue reported on code.google.com by mplic...@gmail.com on 13 Jan 2009 at 12:09

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Currently the calc command appears to only evaluate commands as straight 
javascript
on the client's machine.

Using google calc would allow for units and imaginary numbers...

But unfortunately there's no api provided by google to do this. The easiest way 
I can
imagine is to grab a whole google search page, then take out the right table 
(I'd
look for the table with the calc picture in it myself).

This guy's done a similar thing in perl
http://johnbokma.com/perl/google-calculator.html ha

Original comment by huw.camp...@gmail.com on 16 Apr 2009 at 4:42

GoogleCodeExporter commented 8 years ago
<table class=std><tr><td><img src=/images/calc_img.gif width=40 height=30
alt=""><td> <td nowrap ><h2 class=r style="font-size:138%">16.18 * 15 =
242.7</h2><tr><td> <td> <td><a
href="/intl/fr/help/features.html#calculator">Plus d'infos sur la fonction
calculatrice.</a></table>

There you go.

Starts on line 7 column 50.

Hope to help.

Original comment by evias.se...@gmail.com on 25 Apr 2009 at 8:56

GoogleCodeExporter commented 8 years ago
I haven't tested this, but if you substitute this for calc.js I think it should 
work....

goosh.module.calculate = function(){
  this.name = "calculate";
  this.aliases = new Array("calculate","calc");

  this.help = "evaluate a mathematical expression";
  this.parameters = "[mathematical expression]";
  this.search = function(params) {
    var myurl = 'http://www.google.com/search?q=' + escape(params.replace(/\+/g,'\+')).replace(/\+/g, 
'%2B');
    var onloadHandler = function() { this.load(xmlRequest); };
    var xmlRequest = new XMLHttpRequest();
    xmlRequest.onload = onloadHandler;
    xmlRequest.open("GET", myurl);
    xmlRequest.setRequestHeader("Cache-Control", "no-cache");
    xmlRequest.setRequestHeader('User-Agent',navigator.userAgent);
    xmlRequest.send(null);
  }
  this.load = function(xmlRequest) {
    if (xmlRequest.status == 200) {
        var response = xmlRequest.responseText;
        if(response.match(/calc_img.gif/)) {

            var result = response.match(/calc_img.gif.*?(.*?)<\/b>/)[1];
            goosh.gui.outln(result);
        } else {
            goosh.gui.error("could not calculate that.");
        }
    }
    else {
        goosh.gui.error("could not calculate that.");
    }
  }
  this.call = function(args){

    var out ="";

    var exp = args.join(" ");
    this.search(exp);
    return false; 
  }

}
goosh.modules.register("calculate");

Original comment by dave.gut...@gmail.com on 12 Jun 2009 at 7:09

GoogleCodeExporter commented 8 years ago
The only part that is probably helpful to you guys is the regular expression 
(should be fairly flexible): /calc_img.gif.*?(.*?)<\/b>/

I'm sure you are all much more comfortable with AJAX and javascript than I am.

A much more robust way of doing it would be to turn the XMLHttpRequest 
responseText into a new DOM 
object, but that's a bit tricky to do in a browser compatible way, so I'll 
leave that to you guys.

For that method, something like this should work...

//load responseText into response_dom somehow...

var images = response_dom.getElementsByTagName('img');
var node;
for(var i = 0; i < images.length; i++) {
      if(x[i].src.match(/calc_img.gif/)) {
//            The calc image is contained in a table cell in the same row as 
the answer, which can be found in the 
last cell.  That is, it looks like  
"<tr><td><img/></td>(<td></td>)*<td>result</td></tr>".  The HTML is a 
bit ugly... last child of the parent of the parent of the image should work for 
now though.
                node = x[i].parentNode.parentNode.lastChild;
      }
}
if(node) {
     goosh.gui.outln(node.textContent);
} else {
   //error
}

Original comment by dave.gut...@gmail.com on 12 Jun 2009 at 7:22