From da0e62dff66667c7102ba8526d2ae65e929e40fa Mon Sep 17 00:00:00 2001 From: Deathamns Date: Mon, 20 Oct 2014 19:05:12 +0200 Subject: [PATCH] Use HTML5 download instead of extension API Benefits: - Cross browser solution (however only for relatively new browsers) - Doesn't need extra permission in Chrome If the browser doesn't suppor the download attribute, then a new tab will be opened with the exported data. Other changes: - Start the download only if the data is not empty (previously the download started anyway) - Reorder code in vapi-client.js for Safari, so unnecessary code doesn't run on extension pages --- src/js/vapi-common.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/js/vapi-common.js b/src/js/vapi-common.js index 5d22bb0..5cd1449 100644 --- a/src/js/vapi-common.js +++ b/src/js/vapi-common.js @@ -1,10 +1,35 @@ -// only for background and other extension pages +// could be used for background and other extension pages (function() { 'use strict'; window.vAPI = window.vAPI || {}; +vAPI.download = function(details) { + if (!details.url) { + return; + } + + var a = document.createElement('a'); + + if ('download' in a) { + a.href = details.url; + a.setAttribute('download', details.filename || ''); + a.dispatchEvent(new MouseEvent('click')); + } + else { + var messager = vAPI.messaging.channel('download'); + messager.send({ + what: 'gotoURL', + details: { + url: a.target.href, + index: -1 + } + }); + messager.close(); + } +}; + if (window.chrome) { var chrome = window.chrome;