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:
- 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? - 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;
}
);
}
})