Userscript GM_download 没有在短时间内下载整套文件

Userscript GM_download does not download the whole set of files in a short period

此脚本旨在获取图像列表 URL 并尽可能快地下载所有图像。问题是它遗漏了一些,我不确定为什么。是不是和Chrome限制并行下载数有关?我试图通过使用 GM_downloadonload 函数来解决这个问题。

它从列表中下载了大约一半的文件。如果我将 sleep() 计时器从 200 增加到 2000 之类的东西,成功率会上升,但我不知道为什么我依赖 onload 函数正确说明下载完成的时间(因此 Chrome 可以接受新的下载)

// ==UserScript==
// @name          Image Save Test
// @namespace     ist
// @include       *testimages.juliaimages.org/
// @version       1.0.0
// @run-at        document-end
// @noframes      true
// @grant         GM_download
// ==/UserScript==

var currentDownloads = 0;
var queue = [  'https://testimages.juliaimages.org/thumbnails/autumn_leaves.png',
               'https://testimages.juliaimages.org/thumbnails/blobs.png',
               'https://testimages.juliaimages.org/thumbnails/cameraman.png',
               'https://testimages.juliaimages.org/thumbnails/fabio_color_512.png',
               'https://testimages.juliaimages.org/thumbnails/earth_apollo17.png',
               'https://testimages.juliaimages.org/thumbnails/fabio_gray_256.png',
               'https://testimages.juliaimages.org/thumbnails/hela-cells.png',
               'https://testimages.juliaimages.org/thumbnails/lake_gray.png',
               'https://testimages.juliaimages.org/thumbnails/house.png',
               'https://testimages.juliaimages.org/thumbnails/jetplane.png',
               'https://testimages.juliaimages.org/thumbnails/lake_color.png',
               'https://testimages.juliaimages.org/thumbnails/lena_gray_16bit.png',
               'https://testimages.juliaimages.org/thumbnails/lighthouse.png',
               'https://testimages.juliaimages.org/thumbnails/mandril_color.png',
               'https://testimages.juliaimages.org/thumbnails/mandril_gray.png',
               'https://testimages.juliaimages.org/thumbnails/mountainstream.png',
               'https://testimages.juliaimages.org/thumbnails/peppers_color.png',
               'https://testimages.juliaimages.org/thumbnails/moonsurface.png',
               'https://testimages.juliaimages.org/thumbnails/peppers_gray.png',
               'https://testimages.juliaimages.org/thumbnails/toucan.png',
               'https://testimages.juliaimages.org/thumbnails/pirate.png'];

(async function() {
   while (queue.length > 0) {
      if (currentDownloads > 5) {
         await sleep(200);
         continue;
      }
      
      var item = queue.shift();
      
      (function(_item) {
         GM_download({
            url: _item,
            name: _item.match(/([0-9A-Za-z _-]+)(?=\.png)/)[0],
            saveAs: false,
            onerror: function(error) {
               queue.unshift(_item);
               currentDownloads--;
            },
            onload: function() {
               currentDownloads--;
            }
         });
         
         currentDownloads++;
      })(item);
   }
})();

function sleep(ms) {
   return new Promise(resolve => setTimeout(resolve, ms));
}

此问题已在 TM BETA 4.12.6126 中修复。 derjanb 已将下载文件的数量限制为每 500 毫秒 3 个:

This issue should be fixed at TM BETA 4.12.6126. I'm now limiting the number of downloads to 3 per 500ms. This works good here and I think the overall speed is not that important. Especially since setting download mode to "Browser API" fixes the complete issue.


解决方法:在设置中,将配置模式设置为高级,然后滚动并在下载测试版下将下载模式设置为浏览器 API。 (source)