Javascript:未捕获错误:downloads.onDeterminingFilename的监听器太多

时间:2016-07-15 20:38:31

标签: javascript

以下是代码:

assets.forEach(function(v) {

    var canvas = document.createElement('canvas');
    canvas.id     = "canvas";
    canvas.width  = 200;
    canvas.height = 150;
    var ctx = canvas.getContext("2d");

    var img = new Image();
    img.src = 'file:///path/to/file/' + v.name;
    img.onload = function () {
        ctx.drawImage(img, 0, 0, img.width, img.height-256, 0, 0, 200, 150);
    };
    document.getElementById('download').addEventListener('click',
        function() {
            chrome.downloads.download({
                url: canvas.toDataURL()
            });

            chrome.downloads.onDeterminingFilename.addListener(
                function(downloadItem, cb) {
                    console.log(downloadItem);
                    cb({
                        filename: 'path/to/file/thumb-' + v.name.split('_')[0] + '.png',
                        conflictAction: 'overwrite'
                    });
                    return true;
                }
            );
        },
    false
    );
});

如果文件夹中只有1个文件,则此方法有效。如果更多,那么它仅适用于单个文件。如何弄清楚它是否适用于许多文件?

请注意,它会将新缩略图保存到初始图像所在的同一文件夹中。

1 个答案:

答案 0 :(得分:2)

您不能拥有多个<ul class="main-menu menu-bees" id="menu-main-menu" data-menu-width="1112"> <!-- rowTpl --> <li class="menu-item bees-start active" id="menu-item-0"> <a href="#"> MENU 1 <div class="canvas_wrapper"> <canvas class="menu_dashed" height="64" width="501"></canvas> </div> </a> <span class="button_open"></span> </li> <!-- rowTpl --> <li class="menu-item" id="menu-item-1"> <a href="#"> MENU 2 <div class="canvas_wrapper"> <canvas class="menu_dashed" height="64" width="501"></canvas> </div> </a> <span class="button_open"></span> </li> <!-- rowTpl --> <li class="menu-item" id="menu-item-2"> <a href="#"> MENU 3 <div class="canvas_wrapper"> <canvas class="menu_dashed" height="64" width="501"></canvas> </div> </a> <span class="button_open"></span> </li> </ul>听众:

  

onDeterminingFilename

     

在文件名确定过程中,扩展将有机会覆盖目标DownloadItem.filename。每个扩展名可能不会为此事件注册多个侦听器。

Source