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}" "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": [ "permissions": [
"<all_urls>", "<all_urls>",
"storage", "storage",

View File

@@ -1,4 +1,9 @@
(function _main [] (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] (API.browser.runtime.onMessage.addListener ->[:Message message sender sendResponse]
(let [:Function sendResponse sendResponse] (let [:Function sendResponse sendResponse]
(case message (case message
@@ -14,6 +19,7 @@
txtUrl (URL.createObjectURL file)] txtUrl (URL.createObjectURL file)]
(awaitLet [txtDownloadId (API.browser.downloads.download (object url txtUrl filename txtFileName))] (awaitLet [txtDownloadId (API.browser.downloads.download (object url txtUrl filename txtFileName))]
(catch [e] (sendResponse e)) (catch [e] (sendResponse e))
(API.browser.downloads.show txtDownloadId))))) (API.browser.downloads.show txtDownloadId)
(sendResponse "done")))))
(never otherwise)) (never otherwise))
true))) true)))

View File

@@ -11,7 +11,7 @@
(function syncedStorage [] (function syncedStorage []
(API.browser.storage.sync.get (object queries (new Map<String,Dynamic>)))) (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) (let [request (new XHR)
apiQuery "${API_URL}?key=${PIXABAY_PUBLIC_KEY}&id=$id"] apiQuery "${API_URL}?key=${PIXABAY_PUBLIC_KEY}&id=$id"]
(request.open "GET" apiQuery) (request.open "GET" apiQuery)
@@ -28,24 +28,22 @@
))) )))
(request.send))) (request.send)))
(function handleInfo [:Dynamic info] (function :Void handleInfo [:Dynamic info]
(awaitLet [response (API.browser.runtime.sendMessage (object type "downloadPixabay" data [(first info.hits)]))] (awaitLet [response (API.browser.runtime.sendMessage (object type "downloadPixabay" data [(first info.hits)]))]
(print response))) (print response)))
(window.addEventListener "load" (API.browser.runtime.onMessage.addListener
->:Void ->[:Message message sender sendResponse]
(awaitLet [storage (syncedStorage)] (case message
(let [:Map<String,Dynamic> cachedResults (dictGet storage "queries") ((object type "requestSource")
:String url window.location.href (awaitLet [storage (syncedStorage)]
parts (url.split "/") (let [:Map<String,Dynamic> cachedResults (dictGet storage "queries")
_ (parts.pop) :String url window.location.href
titleAndId (parts.pop) parts (url.split "/")
id (.pop (titleAndId.split "-")) _ (parts.pop)
sourceLink (document.createElement "a")] titleAndId (parts.pop)
(set sourceLink.innerHTML "Source this image") id (.pop (titleAndId.split "-"))]
(sourceLink.addEventListener "click"
->:Void
(ifLet [cachedInfo (dictGet cachedResults id)] (ifLet [cachedInfo (dictGet cachedResults id)]
(handleInfo cachedInfo) (handleInfo cachedInfo)
(queryId id cachedResults))) (queryId id cachedResults)))))
(document.body.prepend sourceLink)))) (otherwise)))