javascript - 使用 chrome.downloads 从通过 chrome.webRequest 找到的 URL 下载文件?

标签 javascript google-chrome google-chrome-extension

我的最终目标是从网站下载文件。不幸的是,当您单击按钮时,包含 URL 的页面会通过 JavaScript 获取下载 URL。因此,我不能只从 HTML 中提取 URL。

现在,我正在使用 chrome 应用程序和 chrome.webRequest.onCompleted.addListener 在单击按钮时获取 URL,然后将该 URL 传递给 chrome.downloads .download 下载文件。

问题是这会造成无限循环,因为使用 URL 运行 chrome.download 只会再次触发 chrome.webRequest。有人可以帮我解决这个先有鸡还是先有蛋的问题吗?这是代码:

list .json

{
  "name": "Chrome test",
  "version": "0.1",
  "description": "A download test",
  "manifest_version": 2,
  "permissions": [
    "webRequest", "*://*.theSite.com/",
    "downloads"
  ],
  "background": {
    "scripts": ["backgroundjs.js"],
    "persistent": true
  }
}

这是后台javascript:

backgroundjs.js

chrome.webRequest.onCompleted.addListener(function(details){
  var fileurl = details.url;
  chrome.downloads.download({
    url: fileurl
  })

},
{urls: ["*://*.theSite.com/KnownGeneralPathOfFile/*"]});

最佳答案

有多种方法可以完成您想要做的事情。立即想到的几种不同方法是:

  1. 保留一个您已经启动下载的 URL 列表,只是不启动另一个与您的 URL 列表中的 URL 相匹配的 URL 的下载。
  2. 根据传递给 webRequest 监听器的 details 对象的属性区分您的操作。对于下载,似乎是 details.tabId===-1details.type==='other'。因此,当满足这两个条件时,您可以不开始下载。

保留要从下载中排除的 URL 列表(再次):
鉴于只有一个 webRequest 为您正在下载的每个 URL 发起,您可以选择在发现匹配一次后将该 URL 留在列表中,或者在第一次匹配后将其删除.如果在第一次匹配后将其删除,则只会阻止当前下载生成的 webRequest 再次下载。如果您选择将 URL 保留在列表中,那么相同的 URL 将不会被下载两次。就个人而言,我希望您的用户通常更愿意在每个 session 中只下载每个 URL 一次。但是,该选择取决于您,或者可以是您允许用户选择的首选项。

以下是一个扩展,它实现了保留一个 URL 列表以排除下载。就像这里一样,每个 URL 只保留到第一次找到为止。这将防止下载中的 webRequest 导致启动另一次下载。如果您只想下载每个 URL 一次,请注释掉或删除 splice 行。

background.js:

var excludedWebRequestDownloadUrls=[];
chrome.webRequest.onCompleted.addListener(function(details){
    let fileUrl = details.url;
    let foundIndex = excludedWebRequestDownloadUrls.indexOf(fileUrl);
    if(foundIndex>-1){
        //Remove the found item (only one webRequest is executed for the download).
        //  May want to not remove the URL from the list of excluded URLs, as you probably
        //  only want to download the same URL once, not multiple times.
        excludedWebRequestDownloadUrls.splice(foundIndex,1);
        //Don't try to download the found URL again.
        return;
    }
    //Remember the URL which we are going to download.
    excludedWebRequestDownloadUrls.push(fileUrl);
    chrome.downloads.download({
        url: fileUrl
    });
},{urls: ["*://*.stackoverflow.com/questions/*"]});

list .json:

{
  "name": "Chrome test",
  "version": "0.1",
  "description": "A download test",
  "manifest_version": 2,
  "permissions": [
    "webRequest", "*://*.stackoverflow.com/",
    "downloads"
  ],
  "background": {
    "scripts": ["background.js"],
    "persistent": true
  }
}

以上代码在 Chrome 和 Firefox 中都经过了测试。

关于javascript - 使用 chrome.downloads 从通过 chrome.webRequest 找到的 URL 下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40075649/

相关文章:

javascript - .splice() 不断删除数组的最后一项

css - Safari 额外的左输入填充

google-chrome - 使用 Google Chrome webRequest API 进行简单转发

javascript - XMLHttpRequest 触发中止事件 - 为什么?

javascript - knockout .js : Binding to a function on the click-event using the Repository-Pattern

javascript - 模糊事件 : get the element clicked from inside the blur event

javascript - 使用 onclick 功能按钮更改选择选项下拉列表

javascript - 清除 JavaScript/jQuery 事件队列

xhtml - chrome根据要渲染的文件扩展名不同渲染js【包含testcase】

javascript - 如何将从客户端浏览器抓取的数据保存到用户文件中?使用 Electron ?