From 795d54398f9fd6e427fbe188616030a76dc9f9ed Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Tue, 6 Jun 2023 13:34:43 -0600 Subject: [PATCH] pixabay sourcer download text file with source --- src/pixabay_sourcer/Background.hx | 3 +++ src/pixabay_sourcer/Background.kiss | 16 +++++++++++++--- src/pixabay_sourcer/Main_.kiss | 3 ++- src/pixabay_sourcer/Message.hx | 2 +- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/pixabay_sourcer/Background.hx b/src/pixabay_sourcer/Background.hx index b56a997..9d3d660 100644 --- a/src/pixabay_sourcer/Background.hx +++ b/src/pixabay_sourcer/Background.hx @@ -4,6 +4,9 @@ import kiss.Prelude; import kiss_firefox.API; import haxe.Constraints; import pixabay_sourcer.Message; +import haxe.io.Path; +import js.html.File; +import js.html.URL; @:build(kiss.Kiss.build()) class Background { diff --git a/src/pixabay_sourcer/Background.kiss b/src/pixabay_sourcer/Background.kiss index f30818f..fa8ea01 100644 --- a/src/pixabay_sourcer/Background.kiss +++ b/src/pixabay_sourcer/Background.kiss @@ -2,8 +2,18 @@ (API.browser.runtime.onMessage.addListener ->[:Message message sender sendResponse] (let [:Function sendResponse sendResponse] (case message - ((object type "download" data [url]) - // TODO awaitLet the DownloadItem and send a nativemessage with the path and credit info - (API.browser.downloads.download (objectWith [saveAs true] url))) + ((object type "download" data [imageInfo]) + // awaitLet the DownloadItem and make an object url with the source info + (awaitLet [dlItemId (API.browser.downloads.download (object saveAs true url imageInfo.largeImageURL)) + dlItems (API.browser.downloads.search (object id dlItemId))] + (let [dlItem (first dlItems) + fileName dlItem.filename + txtFileName "$(Path.withoutDirectory (Path.withoutExtension fileName)).txt" + txtContent "${imageInfo.pageURL}\t${imageInfo.user}\t${imageInfo.user_id}" + file (new File [txtContent] txtFileName) + txtUrl (URL.createObjectURL file)] + (awaitLet [txtDownloadId (API.browser.downloads.download (object url txtUrl filename txtFileName))] + (catch [e] (sendResponse e)) + (API.browser.downloads.show txtDownloadId))))) (never otherwise)) true))) diff --git a/src/pixabay_sourcer/Main_.kiss b/src/pixabay_sourcer/Main_.kiss index 4a0e851..d4442a3 100644 --- a/src/pixabay_sourcer/Main_.kiss +++ b/src/pixabay_sourcer/Main_.kiss @@ -29,7 +29,8 @@ (request.send))) (function handleInfo [:Dynamic info] - (API.browser.runtime.sendMessage (object type "download" data [.largeImageURL (first info.hits)]))) + (awaitLet [response (API.browser.runtime.sendMessage (object type "download" data [(first info.hits)]))] + (print response))) (window.addEventListener "load" ->:Void diff --git a/src/pixabay_sourcer/Message.hx b/src/pixabay_sourcer/Message.hx index 18833d6..09559db 100644 --- a/src/pixabay_sourcer/Message.hx +++ b/src/pixabay_sourcer/Message.hx @@ -2,5 +2,5 @@ package pixabay_sourcer; typedef Message = { type:String, - data:Array + data:Array }; \ No newline at end of file