views:

565

answers:

2

I'm trying to write a simple Ubiquity command that will run a query on Wolfram Alpha, and display the results in the Ubiquity preview object.

I need to set the innerHTML of the preview object. I am currently doing this to get the HTML, which is only a start:

//...
jQuery.get( 'http://www.wolframalpha.com/input/?i=' + input.text, 
    null, 
    function( page ) {
        previewBlock.innerHTML = page;
    }
);

I have 2 problems/questions:

  1. Wolfram takes 5-10 sec to generate all the HTML on the page, so the get() command returns incomplete HTML
    How can it wait for the page to load completely?

  2. The results have id="results" on the Wolfram page, I'd like to just get the results by doing something like this:
    previewBlock.innerHTML = page.getElementById('results').innerHTML
    How can this be done with the URL I'm using?

Another option might be to create a new element using the Wolfram URL as source and append it to previewBlock -- I'm not sure how to do that though. Any suggestions would be appreciated.

UPDATE
Here is the Ubiquity script I'm using -- It will fetch the images from the source HTML and output them in a loop. Note: CmdUtils.previewGet(pblock, opt) calls jQuery.get(opt)

CmdUtils.CreateCommand({
  name: "wolfram",
  takes: {"input": noun_arb_text},
  icon: "http://www.wolframalpha.com/favicon.ico",
  homepage: "http://www.wolframalpha.com",
  author: {name:"Jason Coon"},
  description: "Searches Wolfram Alpha and loads results in to Preview pane.",

  preview: function(pblock, input) {
    CmdUtils.previewGet(pblock,
      'http://www.wolframalpha.com/input/?i=' + input.text,
      null,
      function(data){
        var resultStart = data.indexOf("results",0);
        var beginPos = data.indexOf("<img src", resultStart);
        var endPos = 0;
        var html = ""
        while(beginPos != -1){
          endPos = data.indexOf(">", beginPos);
          html = html + "<br><br>" + data.substring(beginPos, endPos);
          beginPos = data.indexOf("<img src", endPos);
        }
        pblock.innerHTML = html;
      }
    );
  }
})
+2  A: 
Jose Basilio
+1  A: 

I think your idea is doable, but I think you'll have more success with an AJAX call to the Wolfram|Alpha API than by trying to parse a response to the W-A web front-end. For documentation on the WolframAlpha API, see here: www.wolframalpha.com/WolframAlphaAPI.pdf

I think changing your approach to interact with the API should solve your problems. If you want to continue pursuing your current approach, you might want to examine the code for the built-in Wikipedia command to see how they parse the search results and then asynchronously retrieve article summaries for the preview.

dtjohnso