pixabay sourcer download text file with source

This commit is contained in:
2023-06-06 13:34:43 -06:00
parent a6981c4255
commit 795d54398f
4 changed files with 19 additions and 5 deletions

View File

@@ -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 {

View File

@@ -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)))

View File

@@ -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

View File

@@ -2,5 +2,5 @@ package pixabay_sourcer;
typedef Message = {
type:String,
data:Array<String>
data:Array<Dynamic>
};