From 669cc47e0e3585cd8163f177b51f92e06190e156 Mon Sep 17 00:00:00 2001 From: Mike Lang Date: Mon, 10 Oct 2022 06:17:44 +1100 Subject: [PATCH] Trigger download of the image file in a hacky way Apparently this is the Recommended Approach for doing this - you create an element, set its attributes, then temporarily add it to the page and click() it. The downloads.download() api we found earlier is exclusive to Chrome Apps. --- thrimbletrimmer/scripts/common.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/thrimbletrimmer/scripts/common.js b/thrimbletrimmer/scripts/common.js index 29c0c32..3c1dbf2 100644 --- a/thrimbletrimmer/scripts/common.js +++ b/thrimbletrimmer/scripts/common.js @@ -470,6 +470,16 @@ function downloadFrame() { const url = `/frame/${globalStreamName}/source.png?timestamp=${wubloaderTimeFromDateTime(dateTime)}`; // Avoid : as it causes problems on Windows const filename = `${dateTime.toFormat("yyyy-LL-dd'T'HH-mm-ss.SSS")}.png`; - // TODO REPLACE WITH CORRECT CALL - console.log(`Would download ${url} as ${filename}`); + triggerDownload(url, filename); +} + +function triggerDownload(url, filename) { + // URL must be same-origin. + const link = document.createElement('a'); + link.setAttribute('download', filename); + link.href = url; + link.setAttribute('target', '_blank'); + document.body.appendChild(link); + link.click(); + document.body.removeChild(link); }