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;
      }
    );
  }
})
4
  • First of all, how are you getting past the "restricted URI denied" error? May 22, 2009 at 18:21
  • Ubiquity seems to take care of it, and is able to fetch the data.
    – Jason Coon
    May 22, 2009 at 18:29
  • Please post the code that includes the ubiquity call? May 22, 2009 at 18:41
  • I added the code to the question
    – Jason Coon
    May 22, 2009 at 19:15

2 Answers 2

2

I don't think this is even possible using the Ubiquity Preview. The problem is that when sending the request to Wolfram, the page finishes loading very quickly, but it continues to fetch additional information asynchronously as you already know.

If this was done through a browser window instead of the Ubiquity preview, you could actually load the page into a hidden iframe and check the contents before updating the results div. However, this option is not available in current version of Ubiquity.

Here's a screenshot of running your command and passing the text "USA GDP". Two divs get display information and 2 more start fetching additional data asynchronously as soon as the page is finished loading.
alt text http://exterbase.com/wolfram-result.png

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.