jQuery + 普遍性:从 URL 返回文档对象

发布于 2024-07-21 16:29:02 字数 2022 浏览 3 评论 0原文

我正在尝试编写一个简单的 Ubiquity 命令,该命令将在 Wolfram Alpha,并在 Ubiquity 预览对象中显示结果。

我需要设置预览对象的 innerHTML 。 我目前正在这样做是为了获取 HTML,这只是一个开始:

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

我有 2 个问题:

  1. Wolfram 需要 5-10 秒才能生成页面上的所有 HTML,因此 get()命令返回不完整的 HTML
    如何等待页面完全加载?

  2. 结果在 Wolfram 页面上有 id="results" ,我想通过执行以下操作来获取结果:
    previewBlock.innerHTML = page.getElementById('results').innerHTML
    这怎么可能完成了我正在使用的 URL?

另一种选择可能是使用 Wolfram URL 作为源创建一个新元素并将其附加到 previewBlock ——我不知道该怎么做不过。 任何建议,将不胜感激。

更新
这是我正在使用的 Ubiquity 脚本 —— 它将从源 HTML 中获取图像并循环输出它们。 注意:CmdUtils.previewGet(pblock, opt) 调用 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;
      }
    );
  }
})

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她比我温柔 2024-07-28 16:29:02

我认为使用Ubiquity Preview 甚至不可能实现这一点。 问题是,当向 Wolfram 发送请求时,页面很快就会完成加载,但正如您所知,它会继续异步获取其他信息。

如果这是通过浏览器窗口而不是 Ubiquity 预览完成的,您实际上可以将页面加载到隐藏的 iframe 中,并在更新结果 div 之前检查内容。 但是,此选项在当前版本的 Ubiquity 中不可用。

这是运行命令并传递文本“USA GDP”的屏幕截图。 页面加载完成后,两个 div 获取显示信息,另外 2 个 div 开始异步获取其他数据。
替代文本 http://exterbase.com/wolfram-result.png

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

单身狗的梦 2024-07-28 16:29:02

我认为你的想法是可行的,但我认为通过 AJAX 调用 Wolfram|Alpha API 比尝试解析对 WA Web 前端的响应会更成功。 有关 WolframAlpha API 的文档,请参见此处:www.wolframalpha.com/WolframAlphaAPI.pdf

我认为改变与 API 交互的方法应该可以解决您的问题。 如果您想继续采用当前的方法,您可能需要检查内置维基百科命令的代码,以了解它们如何解析搜索结果,然后异步检索文章摘要以进行预览。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文