Giter VIP home page Giter VIP logo

Comments (5)

GoogleCodeExporter avatar GoogleCodeExporter commented on June 16, 2024
[deleted comment]

from goosh.

GoogleCodeExporter avatar GoogleCodeExporter commented on June 16, 2024
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 [email protected] on 16 Apr 2009 at 4:42

from goosh.

GoogleCodeExporter avatar GoogleCodeExporter commented on June 16, 2024
<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 [email protected] on 25 Apr 2009 at 8:56

from goosh.

GoogleCodeExporter avatar GoogleCodeExporter commented on June 16, 2024
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 [email protected] on 12 Jun 2009 at 7:09

from goosh.

GoogleCodeExporter avatar GoogleCodeExporter commented on June 16, 2024
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 [email protected] on 12 Jun 2009 at 7:22

from goosh.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.