diff --git a/build.hxml b/build.hxml index 901d438..e038c0c 100644 --- a/build.hxml +++ b/build.hxml @@ -5,6 +5,10 @@ -dce full --each +--main hollywoo_sourcer.OpenClipArtMain +--js bin/openclipart.js +--next + --main hollywoo_sourcer.PixabayMain --js bin/pixabay.js --next diff --git a/manifest.json b/manifest.json index 348b0cb..c4764d6 100644 --- a/manifest.json +++ b/manifest.json @@ -10,6 +10,10 @@ "48": "icons/icon-48.png" }, "content_scripts": [ + { + "matches": ["https://openclipart.org/detail/*/*"], + "js": ["bin/browser-polyfill.js", "bin/openclipart.js"] + }, { "matches": ["https://pixabay.com/*/*/"], "js": ["bin/browser-polyfill.js", "bin/pixabay.js"] @@ -57,7 +61,8 @@ "https://*.wikipedia.org/wiki/File:*", "https://*.wikipedia.org/wiki/*", "https://freesound.org/people/*/sounds/*/", - "https://opengameart.org/content/*" + "https://opengameart.org/content/*", + "https://openclipart.org/detail/*/*" ] }, diff --git a/src/hollywoo_sourcer/Background.kiss b/src/hollywoo_sourcer/Background.kiss index 3643672..1533911 100644 --- a/src/hollywoo_sourcer/Background.kiss +++ b/src/hollywoo_sourcer/Background.kiss @@ -4,7 +4,7 @@ (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] (case message ((object type "downloadPixabay" data [imageInfo]) @@ -65,5 +65,19 @@ (catch [e] (sendResponse e)) (API.browser.downloads.show txtDownloadId) (sendResponse "done")))))) + ((object type "downloadOpenClipArt" data [sourceUrl filename creator fileUrl]) + // awaitLet the DownloadItem and make an object url with the source info + (awaitLet [dlItemId (API.browser.downloads.download (object saveAs true url fileUrl filename "${filename}.png")) + dlItems (API.browser.downloads.search (object id dlItemId))] + (let [dlItem (first dlItems) + fileName dlItem.filename + txtFileName "$(Path.withoutDirectory (Path.withoutExtension fileName)).tsv" + txtContent "openclipart.org\t${creator}\t${sourceUrl}" + 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) + (sendResponse "done"))))) (never otherwise)) true))) diff --git a/src/hollywoo_sourcer/OpenClipArtMain.hx b/src/hollywoo_sourcer/OpenClipArtMain.hx new file mode 100644 index 0000000..938481a --- /dev/null +++ b/src/hollywoo_sourcer/OpenClipArtMain.hx @@ -0,0 +1,7 @@ +package hollywoo_sourcer; + +class OpenClipArtMain { + static function main() { + OpenClipArtMain_.main(); + } +} diff --git a/src/hollywoo_sourcer/OpenClipArtMain_.kiss b/src/hollywoo_sourcer/OpenClipArtMain_.kiss new file mode 100644 index 0000000..590f863 --- /dev/null +++ b/src/hollywoo_sourcer/OpenClipArtMain_.kiss @@ -0,0 +1,21 @@ +(import kiss_firefox.API) +(import js.lib.Promise) +(importAs js.html.XMLHttpRequest XHR) +(import js.html.HTMLCollection) +(import hollywoo_sourcer.Message) +(var :Dynamic document js.Lib.global.document) +(var :Dynamic window js.Lib.global.window) + +(API.browser.runtime.onMessage.addListener + ->[:Message message sender sendResponse] + (case message + ((object type "requestSource") + (let [url window.location.href + title .innerHTML (.item (document.getElementsByTagName "h2") 0) + links (document.getElementsByTagName "a") + links (for idx (range links.length) (links.item idx)) + artist .innerHTML (first (filter links ->[:Dynamic link] (StringTools.contains link.href "/artist/"))) + fileUrl .href (first (filter links ->[:Dynamic link] (= link.innerHTML "Large")))] + (awaitLet [response (API.browser.runtime.sendMessage (object type "downloadOpenClipArt" data [url title artist fileUrl]))] + (print response)))) + (otherwise))) \ No newline at end of file