如何在 Chrome 扩展中发出多个并行 Web html 请求?

发布于 2024-11-06 18:09:43 字数 1391 浏览 0 评论 0原文

我想在 Chrome 扩展程序中检索和解析多个 html 页面。

对每个请求使用 Web Workers 似乎是使它们并行执行的一种简单方法。这可能吗?我的尝试失败了,可能是因为这是一个已知的权限错误

作为一种解决方法,我想我可以让主扩展页面执行多个异步 XmlHttpRequest,然后让回调将结果页面发送到 Web Workers 进行并行解析。但这种方法提出了一个问题:Chrome 可以同时发出多少个异步并行请求?这个问题已在此处提出,但没有答案。

我正在编写的 Chrome 扩展是一个浏览器操作。

工人代码:


// Triggered by postMessage in the page
onmessage = function (evt) {
    var message = evt.data;
    postMessage(message.count + " started");
    update(message.count, message.url);
};

// parse the results page
function parseResponse(count, url, resp) {
    var msg = count.toString() + " received response ";
    postMessage(msg);
}

// read the Buganizer URL and parse the result page
var update = function(count, url) {
  var xhr = new XMLHttpRequest();
      xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
          parseResponse(count, url, xhr.responseText);
        }
      }
  xhr.onerror = function(error) {
      var msg = "!>: " + count + ": error ";
      postMessage(msg);
  }
  var url = "http://www.hotgrog.com"; // for testing (manifest has permissions for this url)
  xhr.open("GET", url, true);
  xhr.send();
  postMessage(url);
}

I'd like to retrieve and parse multiple html pages within a Chrome extension.

Using Web Workers for each request seemed like a simple way to make them execute in parallel. Is that possible? My attempt failed, perhaps because it's a known permissions bug.

As a workaround, I guess I could have the main extension page do multiple asynchronous XmlHttpRequests, then have the callback send the result page to Web Workers for parallel parsing. But that method raises the question of how many asynchronous parallel requests can Chrome make at once? That question has been asked here, without answer.

The Chrome Extension I'm writing is a Browser Action.

Code for the worker:


// Triggered by postMessage in the page
onmessage = function (evt) {
    var message = evt.data;
    postMessage(message.count + " started");
    update(message.count, message.url);
};

// parse the results page
function parseResponse(count, url, resp) {
    var msg = count.toString() + " received response ";
    postMessage(msg);
}

// read the Buganizer URL and parse the result page
var update = function(count, url) {
  var xhr = new XMLHttpRequest();
      xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
          parseResponse(count, url, xhr.responseText);
        }
      }
  xhr.onerror = function(error) {
      var msg = "!>: " + count + ": error ";
      postMessage(msg);
  }
  var url = "http://www.hotgrog.com"; // for testing (manifest has permissions for this url)
  xhr.open("GET", url, true);
  xhr.send();
  postMessage(url);
}

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

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

发布评论

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

评论(1

箜明 2024-11-13 18:09:43

您是否考虑过尝试异步加载器,例如 RequireJS卷曲

看看作者关于为什么我们应该使用他的产品的解释。

Have you looked into trying asynchronous-loaders such as RequireJS or Curl?

Take a look at the authors explanation as to WHY we should use his product.

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