restructure for multiple supported source sites

This commit is contained in:
2023-06-06 13:46:40 -06:00
parent 25811da4b5
commit 27662277f7
12 changed files with 32 additions and 26 deletions

View File

@@ -0,0 +1,16 @@
package hollywoo_sourcer;
import kiss.Prelude;
import kiss_firefox.API;
import haxe.Constraints;
import hollywoo_sourcer.Message;
import haxe.io.Path;
import js.html.File;
import js.html.URL;
@:build(kiss.Kiss.build())
class Background {
static function main() {
_main();
}
}

View File

@@ -0,0 +1,19 @@
(function _main []
(API.browser.runtime.onMessage.addListener ->[:Message message sender sendResponse]
(let [:Function sendResponse sendResponse]
(case message
((object type "downloadPixabay" 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

@@ -0,0 +1,6 @@
package hollywoo_sourcer;
typedef Message = {
type:String,
data:Array<Dynamic>
};

View File

@@ -0,0 +1,7 @@
package hollywoo_sourcer;
class PixabayMain {
static function main() {
PixabayMain_.main();
}
}

View File

@@ -0,0 +1,51 @@
(import kiss_firefox.API)
(import js.lib.Promise)
(importAs js.html.XMLHttpRequest XHR)
(import hollywoo_sourcer.Message)
(var :Dynamic document js.Lib.global.document)
(var :Dynamic window js.Lib.global.window)
(var PIXABAY_PUBLIC_KEY "24159230-ce68ffe06827131f3f991f4f6")
(var API_URL "https://pixabay.com/api/")
(function syncedStorage []
(API.browser.storage.sync.get (object queries (new Map<String,Dynamic>))))
(function queryId [:String id :Map<String,Dynamic> cache]
(let [request (new XHR)
apiQuery "${API_URL}?key=${PIXABAY_PUBLIC_KEY}&id=$id"]
(request.open "GET" apiQuery)
(request.addEventListener "readystatechange"
->:Void
(cond
((and (= request.readyState 4) (= request.status 200))
(let [data (haxe.Json.parse request.responseText)]
(dictSet cache id data)
(API.browser.storage.sync.set (object queries cache))
(handleInfo data)))
((= request.readyState 4)
// TODO report failure
)))
(request.send)))
(function handleInfo [:Dynamic info]
(awaitLet [response (API.browser.runtime.sendMessage (object type "downloadPixabay" data [(first info.hits)]))]
(print response)))
(window.addEventListener "load"
->:Void
(awaitLet [storage (syncedStorage)]
(let [:Map<String,Dynamic> cachedResults (dictGet storage "queries")
:String url window.location.href
parts (url.split "/")
_ (parts.pop)
titleAndId (parts.pop)
id (.pop (titleAndId.split "-"))
sourceLink (document.createElement "a")]
(set sourceLink.innerHTML "Source this image")
(sourceLink.addEventListener "click"
->:Void
(ifLet [cachedInfo (dictGet cachedResults id)]
(handleInfo cachedInfo)
(queryId id cachedResults)))
(document.body.prepend sourceLink))))