use proper browser bar button to trigger download

This commit is contained in:
2023-06-06 14:20:27 -06:00
parent ad13e21591
commit 05e658878b
5 changed files with 34 additions and 18 deletions

BIN
icons/page-16.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 203 B

BIN
icons/page-32.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 278 B

View File

@@ -26,6 +26,18 @@
"id": "{0d44525b-9803-4630-917d-585c3b538fcb}"
}
},
"page_action": {
"default_icon": {
"16": "icons/page-16.png",
"32": "icons/page-32.png"
},
"default_title": "Source this asset",
"show_matches": [
"https://pixabay.com/*/*/"
]
},
"permissions": [
"<all_urls>",
"storage",

View File

@@ -1,4 +1,9 @@
(function _main []
(API.browser.pageAction.onClicked.addListener
->[tab clickData]
(API.browser.tabs.sendMessage
tab.id (object type "requestSource" data [])))
(API.browser.runtime.onMessage.addListener ->[:Message message sender sendResponse]
(let [:Function sendResponse sendResponse]
(case message
@@ -14,6 +19,7 @@
txtUrl (URL.createObjectURL file)]
(awaitLet [txtDownloadId (API.browser.downloads.download (object url txtUrl filename txtFileName))]
(catch [e] (sendResponse e))
(API.browser.downloads.show txtDownloadId)))))
(API.browser.downloads.show txtDownloadId)
(sendResponse "done")))))
(never otherwise))
true)))

View File

@@ -11,7 +11,7 @@
(function syncedStorage []
(API.browser.storage.sync.get (object queries (new Map<String,Dynamic>))))
(function queryId [:String id :Map<String,Dynamic> cache]
(function :Void queryId [:String id :Map<String,Dynamic> cache]
(let [request (new XHR)
apiQuery "${API_URL}?key=${PIXABAY_PUBLIC_KEY}&id=$id"]
(request.open "GET" apiQuery)
@@ -28,24 +28,22 @@
)))
(request.send)))
(function handleInfo [:Dynamic info]
(function :Void 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
(API.browser.runtime.onMessage.addListener
->[:Message message sender sendResponse]
(case message
((object type "requestSource")
(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 "-"))]
(ifLet [cachedInfo (dictGet cachedResults id)]
(handleInfo cachedInfo)
(queryId id cachedResults)))
(document.body.prepend sourceLink))))
(queryId id cachedResults)))))
(otherwise)))