Skip to content

Commit

Permalink
Target latest browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jun 11, 2019
1 parent 9e49300 commit f4b8545
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 183 deletions.
5 changes: 1 addition & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* text=auto
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
12 changes: 0 additions & 12 deletions .jshintrc

This file was deleted.

1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
79 changes: 39 additions & 40 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.multiDownload = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.multiDownload = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
'use strict';

function fallback(urls) {
var i = 0;
// TODO: Remove this sometime in the future
const fallback = urls => {
let i = 0;

(function createIframe() {
var frame = document.createElement('iframe');
const frame = document.createElement('iframe');
frame.style.display = 'none';
frame.src = urls[i++];
document.documentElement.appendChild(frame);
document.documentElement.append(frame);

// the download init has to be sequential otherwise IE only use the first
var interval = setInterval(function () {
if (frame.contentWindow.document.readyState === 'complete'
|| frame.contentWindow.document.readyState === 'interactive') {
clearInterval(interval);
// The download init has to be sequential otherwise IE only uses the first
const intervalId = setInterval(() => {
if (
frame.contentWindow.document.readyState === 'complete' ||
frame.contentWindow.document.readyState === 'interactive'
) {
clearInterval(intervalId);

// Safari needs a timeout
setTimeout(function () {
setTimeout(() => {
frame.parentNode.removeChild(frame);
}, 1000);

Expand All @@ -27,35 +30,28 @@ function fallback(urls) {
}
}, 100);
})();
}
};

function isFirefox() {
// sad panda :(
return /Firefox\//i.test(navigator.userAgent);
}
const isFirefox = () => /Firefox\//i.test(navigator.userAgent);

function sameDomain(url) {
var a = document.createElement('a');
a.href = url;
const isSafari = () => /^((?!chrome|android).)*safari/i.test(navigator.userAgent);

const sameDomain = url => {
const a = document.createElement('a');
a.href = url;
return location.hostname === a.hostname && location.protocol === a.protocol;
}
};

function download(url, name) {
var a = document.createElement('a');
const download = (url, name) => {
const a = document.createElement('a');
a.download = name;
a.href = url;
// firefox doesn't support `a.click()`...
a.dispatchEvent(new MouseEvent('click'));
}

module.exports = function (urlsOrOpts) {
if (urlsOrOpts.constructor === Array) {
urlsOrOpts = { urls: urlsOrOpts };
}

const { urls, renameFn } = urlsOrOpts;
// Firefox doesn't support `a.click()`
a.dispatchEvent(new MouseEvent('click'));
};

module.exports = (urls, options = {}) => {
if (!urls) {
throw new Error('`urls` required');
}
Expand All @@ -64,19 +60,22 @@ module.exports = function (urlsOrOpts) {
return fallback(urls);
}

var delay = 0;
let delay = 0;

urls.forEach(function (url, index) {
var name = typeof renameFn === 'function' ? renameFn({ url, index, urls }) : '';
for (const [index, url] of urls.entries()) {
const name = typeof options.rename === 'function' ? options.rename({url, index, urls}) : '';

// the download init has to be sequential for firefox if the urls are not on the same domain
if (isFirefox() && !sameDomain(url)) {
return setTimeout(download.bind(null, url, name), 100 * ++delay);
if (isFirefox() || isSafari()) {
setTimeout(() => {
download(url, name);
}, ++delay * 100);

continue;
}

download(url, name);
});
}
}
};

},{}]},{},[1])(1)
});
});
40 changes: 23 additions & 17 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,46 @@
<meta charset="utf-8">
<meta name="author" content="Sindre Sorhus">
<meta name="viewport" content="width=device-width">
<title>multi-download</title>
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<title>multi-download example</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<style>
#download-btn, #download-rename-btn {
#download-button,
#download-rename-button {
height: 50px;
position: absolute;
left: 50%;
transform: translate(-50%, -50%);
outline: none;
}
#download-btn {
top: calc(50% - 30px);

#download-button {
top: calc(50% - 50px);
}
#download-rename-btn {
top: calc(50% + 30px);

#download-rename-button {
top: calc(50% + 50px);
}
</style>
</head>
<body >
<button id="download-btn" class="btn btn-primary btn-lg" data-files="fixture/unicorn.jpg.zip fixture/rainbow.jpg.zip fixture/unicorn2.jpg.zip">Download multiple files</button>
<button id="download-rename-btn" class="btn btn-primary btn-lg" data-files="fixture/unicorn.jpg.zip fixture/rainbow.jpg.zip fixture/unicorn2.jpg.zip">Download multiple files and rename them</button>
<button id="download-button" class="btn btn-primary btn-lg" data-files="fixture/unicorn.jpg.zip fixture/rainbow.jpg.zip fixture/unicorn2.jpg.zip">Download multiple files</button>
<button id="download-rename-button" class="btn btn-primary btn-lg" data-files="fixture/unicorn.jpg.zip fixture/rainbow.jpg.zip fixture/unicorn2.jpg.zip">Download multiple files and rename them</button>
<script src="browser.js"></script>
<script>
document.querySelector('#download-btn').addEventListener('click', function (e) {
var files = e.target.dataset.files.split(' ');
document.querySelector('#download-button').addEventListener('click', event => {
const files = event.target.dataset.files.split(' ');
multiDownload(files);
});

document.querySelector('#download-rename-btn').addEventListener('click', function (e) {
var files = e.target.dataset.files.split(' ');
multiDownload({ urls: files, renameFn: function ({url, index, urls}) {
var extension = url.substring(url.lastIndexOf('.') + 1);
return 'file' + index + '.' + extension;
}});
document.querySelector('#download-rename-button').addEventListener('click', event => {
const files = event.target.dataset.files.split(' ');

multiDownload(files, {
rename: ({url, index, urls}) => {
const extension = url.slice(url.lastIndexOf('.') + 1);
return `file${index}.${extension}`;
}
});
});
</script>
</body>
Expand Down
77 changes: 36 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
'use strict';

function fallback(urls) {
var i = 0;
// TODO: Remove this sometime in the future
const fallback = urls => {
let i = 0;

(function createIframe() {
var frame = document.createElement('iframe');
const frame = document.createElement('iframe');
frame.style.display = 'none';
frame.src = urls[i++];
document.documentElement.appendChild(frame);
document.documentElement.append(frame);

// the download init has to be sequential otherwise IE only use the first
var interval = setInterval(function () {
if (frame.contentWindow.document.readyState === 'complete'
|| frame.contentWindow.document.readyState === 'interactive') {
clearInterval(interval);
// The download init has to be sequential otherwise IE only uses the first
const intervalId = setInterval(() => {
if (
frame.contentWindow.document.readyState === 'complete' ||
frame.contentWindow.document.readyState === 'interactive'
) {
clearInterval(intervalId);

// Safari needs a timeout
setTimeout(function () {
setTimeout(() => {
frame.parentNode.removeChild(frame);
}, 1000);

Expand All @@ -26,39 +29,28 @@ function fallback(urls) {
}
}, 100);
})();
}
};

function isFirefox() {
// sad panda :(
return /Firefox\//i.test(navigator.userAgent);
}
const isFirefox = () => /Firefox\//i.test(navigator.userAgent);

function isSafari() {
return /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
}
const isSafari = () => /^((?!chrome|android).)*safari/i.test(navigator.userAgent);

function sameDomain(url) {
var a = document.createElement('a');
const sameDomain = url => {
const a = document.createElement('a');
a.href = url;

return location.hostname === a.hostname && location.protocol === a.protocol;
}
};

function download(url, name) {
var a = document.createElement('a');
const download = (url, name) => {
const a = document.createElement('a');
a.download = name;
a.href = url;
// firefox doesn't support `a.click()`...
a.dispatchEvent(new MouseEvent('click'));
}

module.exports = function (urlsOrOpts) {
if (urlsOrOpts.constructor === Array) {
urlsOrOpts = { urls: urlsOrOpts };
}

const { urls, renameFn } = urlsOrOpts;
// Firefox doesn't support `a.click()`
a.dispatchEvent(new MouseEvent('click'));
};

module.exports = (urls, options = {}) => {
if (!urls) {
throw new Error('`urls` required');
}
Expand All @@ -67,16 +59,19 @@ module.exports = function (urlsOrOpts) {
return fallback(urls);
}

var delay = 0;
let delay = 0;

for (const [index, url] of urls.entries()) {
const name = typeof options.rename === 'function' ? options.rename({url, index, urls}) : '';

urls.forEach(function (url, index) {
var name = typeof renameFn === 'function' ? renameFn({ url, index, urls }) : '';
if (isFirefox() || isSafari()) {
setTimeout(() => {
download(url, name);
}, ++delay * 100);

// the download init has to be sequential for firefox if the urls are not on the same domain
if ((isFirefox() && !sameDomain(url)) || isSafari()) {
return setTimeout(download.bind(null, url, name), 100 * ++delay);
continue;
}

download(url, name);
});
}
}
};
20 changes: 4 additions & 16 deletions license
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading

0 comments on commit f4b8545

Please sign in to comment.