diff --git a/build.hxml b/build.hxml index 45f94c6..cd9ac41 100644 --- a/build.hxml +++ b/build.hxml @@ -7,6 +7,9 @@ --main hollywoo_sourcer.PixabayMain --js bin/pixabay.js --next +--main hollywoo_sourcer.WikipediaMain +--js bin/wikipedia.js +--next --main hollywoo_sourcer.Background --js bin/background.js -cmd cp node_modules/webextension-polyfill/dist/browser-polyfill.js* bin/ && zip -r hollywoo-sourcer.zip . -x *.git* -x *.hxml -x *.zip -x src/\* -x node_modules/\* -x libs/\* -x test.sh \ No newline at end of file diff --git a/manifest.json b/manifest.json index 04085a9..c639d62 100644 --- a/manifest.json +++ b/manifest.json @@ -13,6 +13,13 @@ { "matches": ["https://pixabay.com/*/*/"], "js": ["bin/browser-polyfill.js", "bin/pixabay.js"] + }, + { + "matches": [ + "https://*.wikipedia.org/wiki/File:*", + "https://*.wikipedia.org/wiki/*" + ], + "js": ["bin/browser-polyfill.js", "bin/wikipedia.js"] } ], "background": { @@ -34,7 +41,9 @@ }, "default_title": "Source this asset", "show_matches": [ - "https://pixabay.com/*/*/" + "https://pixabay.com/*/*/", + "https://*.wikipedia.org/wiki/File:*", + "https://*.wikipedia.org/wiki/*" ] }, diff --git a/src/hollywoo_sourcer/Background.kiss b/src/hollywoo_sourcer/Background.kiss index 51a7e7e..ccfb3e7 100644 --- a/src/hollywoo_sourcer/Background.kiss +++ b/src/hollywoo_sourcer/Background.kiss @@ -21,5 +21,19 @@ (catch [e] (sendResponse e)) (API.browser.downloads.show txtDownloadId) (sendResponse "done"))))) + ((object type "downloadWikipedia" data [filename src fromUrl]) + // awaitLet the DownloadItem and make an object url with the source info + (awaitLet [dlItemId (API.browser.downloads.download (object saveAs true url src)) + dlItems (API.browser.downloads.search (object id dlItemId))] + (let [dlItem (first dlItems) + fileName dlItem.filename + txtFileName "$(Path.withoutDirectory (Path.withoutExtension fileName)).tsv" + txtContent "wikipedia.org\t\t${fromUrl}" + 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/WikipediaMain.hx b/src/hollywoo_sourcer/WikipediaMain.hx new file mode 100644 index 0000000..50c820f --- /dev/null +++ b/src/hollywoo_sourcer/WikipediaMain.hx @@ -0,0 +1,7 @@ +package hollywoo_sourcer; + +class WikipediaMain { + static function main() { + WikipediaMain_.main(); + } +} diff --git a/src/hollywoo_sourcer/WikipediaMain_.kiss b/src/hollywoo_sourcer/WikipediaMain_.kiss new file mode 100644 index 0000000..14231dd --- /dev/null +++ b/src/hollywoo_sourcer/WikipediaMain_.kiss @@ -0,0 +1,23 @@ +(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) + +(API.browser.runtime.onMessage.addListener + ->[:Message message sender sendResponse] + (case message + ((object type "requestSource") + (let [:String url window.location.href + parts (url.split ":") + filename (parts.pop) + imgTags (document.getElementsByTagName "img")] + (doFor idx (range imgTags.length) + (let [imgTag (imgTags.item idx) + src (imgTag.getAttribute "src")] + (when (StringTools.contains src filename) + (awaitLet [response (API.browser.runtime.sendMessage (object type "downloadWikipedia" data [filename "https:${src}" url]))] + (print response)) + (break)))))) + (otherwise))) \ No newline at end of file