From 05e658878b65b9606926597dc738c5d64928d029 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Tue, 6 Jun 2023 14:20:27 -0600 Subject: [PATCH] use proper browser bar button to trigger download --- icons/page-16.png | Bin 0 -> 203 bytes icons/page-32.png | Bin 0 -> 278 bytes manifest.json | 12 ++++++++++ src/hollywoo_sourcer/Background.kiss | 8 ++++++- src/hollywoo_sourcer/PixabayMain_.kiss | 32 ++++++++++++------------- 5 files changed, 34 insertions(+), 18 deletions(-) create mode 100644 icons/page-16.png create mode 100644 icons/page-32.png diff --git a/icons/page-16.png b/icons/page-16.png new file mode 100644 index 0000000000000000000000000000000000000000..8abaacc90f44c7ac9c90bd3158155983024868e3 GIT binary patch literal 203 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!73?$#)eFPHVEbxddW?hCsaS_K9 qM)89u6G9m-=J4^HNl4GpWMI%fDDBuF_VEnR2nJ7AKbLh*2~7aXf^(}va;kvh!8w&1_8kq$ ztLi-aa2?PDfs!DFVdQ&MBb@01Go{LjV8( literal 0 HcmV?d00001 diff --git a/manifest.json b/manifest.json index 3582ec0..04085a9 100644 --- a/manifest.json +++ b/manifest.json @@ -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": [ "", "storage", diff --git a/src/hollywoo_sourcer/Background.kiss b/src/hollywoo_sourcer/Background.kiss index 13cafbf..a705af3 100644 --- a/src/hollywoo_sourcer/Background.kiss +++ b/src/hollywoo_sourcer/Background.kiss @@ -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))) diff --git a/src/hollywoo_sourcer/PixabayMain_.kiss b/src/hollywoo_sourcer/PixabayMain_.kiss index c902d8d..d256e2e 100644 --- a/src/hollywoo_sourcer/PixabayMain_.kiss +++ b/src/hollywoo_sourcer/PixabayMain_.kiss @@ -11,7 +11,7 @@ (function syncedStorage [] (API.browser.storage.sync.get (object queries (new Map)))) -(function queryId [:String id :Map cache] +(function :Void queryId [:String id :Map 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 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 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)))) \ No newline at end of file + (queryId id cachedResults))))) + (otherwise))) \ No newline at end of file