diff --git a/build.hxml b/build.hxml new file mode 100644 index 0000000..10eac0b --- /dev/null +++ b/build.hxml @@ -0,0 +1,12 @@ +-lib kiss +-lib kiss-firefox +-cp externs +-cp src +-dce full +--each +--main pixabay_sourcer.Main +--js bin/main.js +--next +--main pixabay_sourcer.Background +--js bin/background.js +-cmd cp node_modules/webextension-polyfill/dist/browser-polyfill.js* bin/ && zip -r template.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/haxe_libraries/haxe-strings.hxml b/haxe_libraries/haxe-strings.hxml new file mode 100644 index 0000000..e5c6cbd --- /dev/null +++ b/haxe_libraries/haxe-strings.hxml @@ -0,0 +1,5 @@ +# @install: lix --silent download "haxelib:/haxe-strings#7.0.2" into haxe-strings/7.0.2/haxelib +-cp ${HAXE_LIBCACHE}/haxe-strings/7.0.2/haxelib/src/ +-D haxe-strings=7.0.2 +--macro hx.strings.internal.Macros.addDefines() +--macro hx.strings.internal.Macros.configureNullSafety() diff --git a/haxe_libraries/hscript.hxml b/haxe_libraries/hscript.hxml new file mode 100644 index 0000000..106e1f8 --- /dev/null +++ b/haxe_libraries/hscript.hxml @@ -0,0 +1,5 @@ +# @install: lix --silent download "haxelib:/hscript#2.5.0" into hscript/2.5.0/haxelib +# @run: haxelib run-dir hscript "${HAXE_LIBCACHE}/hscript/2.5.0/haxelib" +-cp ${HAXE_LIBCACHE}/hscript/2.5.0/haxelib/ +-D hscript=2.5.0 +--macro keep('IntIterator') \ No newline at end of file diff --git a/haxe_libraries/kiss-firefox.hxml b/haxe_libraries/kiss-firefox.hxml new file mode 100644 index 0000000..d76f6a5 --- /dev/null +++ b/haxe_libraries/kiss-firefox.hxml @@ -0,0 +1,4 @@ +# @install: lix --silent download "gh://github.com/kiss-lang/kiss-firefox#a81ba4bfd2a5d5ec454e0d49c6bfcc8a332c2bae" into kiss-firefox/0.0.0/github/a81ba4bfd2a5d5ec454e0d49c6bfcc8a332c2bae +-lib kiss +-cp ${HAXE_LIBCACHE}/kiss-firefox/0.0.0/github/a81ba4bfd2a5d5ec454e0d49c6bfcc8a332c2bae/src/ +-D kiss-firefox=0.0.0 \ No newline at end of file diff --git a/haxe_libraries/kiss.hxml b/haxe_libraries/kiss.hxml new file mode 100644 index 0000000..a05db95 --- /dev/null +++ b/haxe_libraries/kiss.hxml @@ -0,0 +1,12 @@ +# @install: lix --silent download "gh://github.com/kiss-lang/kiss#a6c0cf88c6a02aa7845dca7c5c5025b915b78fbc" into kiss/0.0.1/github/a6c0cf88c6a02aa7845dca7c5c5025b915b78fbc +# @run: haxelib run-dir kiss "${HAXE_LIBCACHE}/kiss/0.0.1/github/a6c0cf88c6a02aa7845dca7c5c5025b915b78fbc" +-lib haxe-strings +-lib hscript +-lib tink_json +-lib tink_macro +-lib tink_syntaxhub +-lib uuid +-cp ${HAXE_LIBCACHE}/kiss/0.0.1/github/a6c0cf88c6a02aa7845dca7c5c5025b915b78fbc/src/ +-D kiss=0.0.1 +-w -WUnusedPattern +--macro kiss.KissFrontend.use() \ No newline at end of file diff --git a/haxe_libraries/tink_core.hxml b/haxe_libraries/tink_core.hxml new file mode 100644 index 0000000..37a0d96 --- /dev/null +++ b/haxe_libraries/tink_core.hxml @@ -0,0 +1,3 @@ +# @install: lix --silent download "haxelib:/tink_core#2.1.0" into tink_core/2.1.0/haxelib +-cp ${HAXE_LIBCACHE}/tink_core/2.1.0/haxelib/src +-D tink_core=2.1.0 \ No newline at end of file diff --git a/haxe_libraries/tink_json.hxml b/haxe_libraries/tink_json.hxml new file mode 100644 index 0000000..b167de7 --- /dev/null +++ b/haxe_libraries/tink_json.hxml @@ -0,0 +1,4 @@ +# @install: lix --silent download "haxelib:/tink_json#0.11.0" into tink_json/0.11.0/haxelib +-lib tink_typecrawler +-cp ${HAXE_LIBCACHE}/tink_json/0.11.0/haxelib/src +-D tink_json=0.11.0 \ No newline at end of file diff --git a/haxe_libraries/tink_macro.hxml b/haxe_libraries/tink_macro.hxml new file mode 100644 index 0000000..a9e19b1 --- /dev/null +++ b/haxe_libraries/tink_macro.hxml @@ -0,0 +1,4 @@ +# @install: lix --silent download "haxelib:/tink_macro#1.0.1" into tink_macro/1.0.1/haxelib +-lib tink_core +-cp ${HAXE_LIBCACHE}/tink_macro/1.0.1/haxelib/src +-D tink_macro=1.0.1 \ No newline at end of file diff --git a/haxe_libraries/tink_priority.hxml b/haxe_libraries/tink_priority.hxml new file mode 100644 index 0000000..98cfa80 --- /dev/null +++ b/haxe_libraries/tink_priority.hxml @@ -0,0 +1,3 @@ +-D tink_priority=0.1.3 +# @install: lix --silent download "gh://github.com/haxetink/tink_priority#ea736d31dc788aae703a2aa415c25d5b80d0e7d1" into tink_priority/0.1.3/github/ea736d31dc788aae703a2aa415c25d5b80d0e7d1 +-cp ${HAXE_LIBCACHE}/tink_priority/0.1.3/github/ea736d31dc788aae703a2aa415c25d5b80d0e7d1/src diff --git a/haxe_libraries/tink_syntaxhub.hxml b/haxe_libraries/tink_syntaxhub.hxml new file mode 100644 index 0000000..e3c7d2b --- /dev/null +++ b/haxe_libraries/tink_syntaxhub.hxml @@ -0,0 +1,6 @@ +# @install: lix --silent download "gh://github.com/haxetink/tink_syntaxhub#b6ea4966bbdee4d176ac8dd5d2d8ae3b362b2f86" into tink_syntaxhub/0.6.0/github/b6ea4966bbdee4d176ac8dd5d2d8ae3b362b2f86 +-lib tink_macro +-lib tink_priority +-cp ${HAXE_LIBCACHE}/tink_syntaxhub/0.6.0/github/b6ea4966bbdee4d176ac8dd5d2d8ae3b362b2f86/src +-D tink_syntaxhub=0.6.0 +--macro tink.SyntaxHub.use() \ No newline at end of file diff --git a/haxe_libraries/tink_typecrawler.hxml b/haxe_libraries/tink_typecrawler.hxml new file mode 100644 index 0000000..373aee9 --- /dev/null +++ b/haxe_libraries/tink_typecrawler.hxml @@ -0,0 +1,4 @@ +# @install: lix --silent download "haxelib:/tink_typecrawler#0.7.0" into tink_typecrawler/0.7.0/haxelib +-lib tink_macro +-cp ${HAXE_LIBCACHE}/tink_typecrawler/0.7.0/haxelib/src +-D tink_typecrawler=0.7.0 \ No newline at end of file diff --git a/haxe_libraries/uuid.hxml b/haxe_libraries/uuid.hxml new file mode 100644 index 0000000..ff1269b --- /dev/null +++ b/haxe_libraries/uuid.hxml @@ -0,0 +1,3 @@ +# @install: lix --silent download "haxelib:/uuid#2.4.1" into uuid/2.4.1/haxelib +-cp ${HAXE_LIBCACHE}/uuid/2.4.1/haxelib/src +-D uuid=2.4.1 \ No newline at end of file diff --git a/icons/icon-48.png b/icons/icon-48.png new file mode 100644 index 0000000..90687de Binary files /dev/null and b/icons/icon-48.png differ diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..27e04cb --- /dev/null +++ b/manifest.json @@ -0,0 +1,34 @@ +{ + + "manifest_version": 2, + "name": "pixabay-sourcer", + "version": "0.0", + + "description": "", + + "icons": { + "48": "icons/icon-48.png" + }, + "content_scripts": [ + { + "matches": ["https://pixabay.com/photos/*/"], + "js": ["bin/browser-polyfill.js", "bin/main.js"] + } + ], + "background": { + "scripts": [ + "bin/browser-polyfill.js", + "bin/background.js" + ] + }, + "browser_specific_settings": { + "gecko": { + "id": "{0d44525b-9803-4630-917d-585c3b538fcb}" + } + }, + "permissions": [ + "", + "storage", + "downloads" + ] +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..48acc89 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,82 @@ +{ + "name": "template", + "version": "0.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "template", + "version": "0.0.0", + "hasInstallScript": true, + "dependencies": { + "@types/webextension-polyfill": "^0.10.0", + "webextension-polyfill": "^0.10.0" + }, + "devDependencies": { + "dts2hx": "^0.19.0" + } + }, + "node_modules/@types/webextension-polyfill": { + "version": "0.10.0", + "resolved": "https://registry.npmjs.org/@types/webextension-polyfill/-/webextension-polyfill-0.10.0.tgz", + "integrity": "sha512-If4EcaHzYTqcbNMp/FdReVdRmLL/Te42ivnJII551bYjhX19bWem5m14FERCqdJA732OloGuxCRvLBvcMGsn4A==" + }, + "node_modules/dts2hx": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/dts2hx/-/dts2hx-0.19.2.tgz", + "integrity": "sha512-1+X3kOB941APt/O6b5jX9+XMefRPqvFk4fvGDHPLIbuYdIAjNwcX3XOuZJ9fYtUD7Odn4ACPfK2ql2HWaFrLrg==", + "dev": true, + "dependencies": { + "typescript": "3.7.4" + }, + "bin": { + "dts2hx": "cli.js" + } + }, + "node_modules/typescript": { + "version": "3.7.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.4.tgz", + "integrity": "sha512-A25xv5XCtarLwXpcDNZzCGvW2D1S3/bACratYBx2sax8PefsFhlYmkQicKHvpYflFS8if4zne5zT5kpJ7pzuvw==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=4.2.0" + } + }, + "node_modules/webextension-polyfill": { + "version": "0.10.0", + "resolved": "https://registry.npmjs.org/webextension-polyfill/-/webextension-polyfill-0.10.0.tgz", + "integrity": "sha512-c5s35LgVa5tFaHhrZDnr3FpQpjj1BB+RXhLTYUxGqBVN460HkbM8TBtEqdXWbpTKfzwCcjAZVF7zXCYSKtcp9g==" + } + }, + "dependencies": { + "@types/webextension-polyfill": { + "version": "0.10.0", + "resolved": "https://registry.npmjs.org/@types/webextension-polyfill/-/webextension-polyfill-0.10.0.tgz", + "integrity": "sha512-If4EcaHzYTqcbNMp/FdReVdRmLL/Te42ivnJII551bYjhX19bWem5m14FERCqdJA732OloGuxCRvLBvcMGsn4A==" + }, + "dts2hx": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/dts2hx/-/dts2hx-0.19.2.tgz", + "integrity": "sha512-1+X3kOB941APt/O6b5jX9+XMefRPqvFk4fvGDHPLIbuYdIAjNwcX3XOuZJ9fYtUD7Odn4ACPfK2ql2HWaFrLrg==", + "dev": true, + "requires": { + "typescript": "3.7.4" + } + }, + "typescript": { + "version": "3.7.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.4.tgz", + "integrity": "sha512-A25xv5XCtarLwXpcDNZzCGvW2D1S3/bACratYBx2sax8PefsFhlYmkQicKHvpYflFS8if4zne5zT5kpJ7pzuvw==", + "dev": true + }, + "webextension-polyfill": { + "version": "0.10.0", + "resolved": "https://registry.npmjs.org/webextension-polyfill/-/webextension-polyfill-0.10.0.tgz", + "integrity": "sha512-c5s35LgVa5tFaHhrZDnr3FpQpjj1BB+RXhLTYUxGqBVN460HkbM8TBtEqdXWbpTKfzwCcjAZVF7zXCYSKtcp9g==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..5e9161d --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "template", + "version": "0.0.0", + "description": "A Firefox extension made with Kisslang", + "main": "index.js", + "scripts": { + "postinstall": "dts2hx --all --noLibWrap" + }, + "dependencies": { + "@types/webextension-polyfill": "^0.10.0", + "webextension-polyfill": "^0.10.0" + }, + "devDependencies": { + "dts2hx": "^0.19.0" + } +} diff --git a/src/pixabay_sourcer/Background.hx b/src/pixabay_sourcer/Background.hx new file mode 100644 index 0000000..b56a997 --- /dev/null +++ b/src/pixabay_sourcer/Background.hx @@ -0,0 +1,13 @@ +package pixabay_sourcer; + +import kiss.Prelude; +import kiss_firefox.API; +import haxe.Constraints; +import pixabay_sourcer.Message; + +@:build(kiss.Kiss.build()) +class Background { + static function main() { + _main(); + } +} \ No newline at end of file diff --git a/src/pixabay_sourcer/Background.kiss b/src/pixabay_sourcer/Background.kiss new file mode 100644 index 0000000..f30818f --- /dev/null +++ b/src/pixabay_sourcer/Background.kiss @@ -0,0 +1,9 @@ +(function _main [] + (API.browser.runtime.onMessage.addListener ->[:Message message sender sendResponse] + (let [:Function sendResponse sendResponse] + (case message + ((object type "download" data [url]) + // TODO awaitLet the DownloadItem and send a nativemessage with the path and credit info + (API.browser.downloads.download (objectWith [saveAs true] url))) + (never otherwise)) + true))) diff --git a/src/pixabay_sourcer/Main.hx b/src/pixabay_sourcer/Main.hx new file mode 100644 index 0000000..0831944 --- /dev/null +++ b/src/pixabay_sourcer/Main.hx @@ -0,0 +1,7 @@ +package pixabay_sourcer; + +class Main { + static function main() { + Main_.main(); + } +} diff --git a/src/pixabay_sourcer/Main_.kiss b/src/pixabay_sourcer/Main_.kiss new file mode 100644 index 0000000..4a0e851 --- /dev/null +++ b/src/pixabay_sourcer/Main_.kiss @@ -0,0 +1,50 @@ +(import kiss_firefox.API) +(import js.lib.Promise) +(importAs js.html.XMLHttpRequest XHR) +(import pixabay_sourcer.Message) +(var :Dynamic document js.Lib.global.document) +(var :Dynamic window js.Lib.global.window) + +(var PIXABAY_PUBLIC_KEY "24159230-ce68ffe06827131f3f991f4f6") +(var API_URL "https://pixabay.com/api/") + +(function syncedStorage [] + (API.browser.storage.sync.get (object queries (new Map)))) + +(function queryId [:String id :Map cache] + (let [request (new XHR) + apiQuery "${API_URL}?key=${PIXABAY_PUBLIC_KEY}&id=$id"] + (request.open "GET" apiQuery) + (request.addEventListener "readystatechange" + ->:Void + (cond + ((and (= request.readyState 4) (= request.status 200)) + (let [data (haxe.Json.parse request.responseText)] + (dictSet cache id data) + (API.browser.storage.sync.set (object queries cache)) + (handleInfo data))) + ((= request.readyState 4) + // TODO report failure + ))) + (request.send))) + +(function handleInfo [:Dynamic info] + (API.browser.runtime.sendMessage (object type "download" data [.largeImageURL (first info.hits)]))) + +(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 + (ifLet [cachedInfo (dictGet cachedResults id)] + (handleInfo cachedInfo) + (queryId id cachedResults))) + (document.body.prepend sourceLink)))) \ No newline at end of file diff --git a/src/pixabay_sourcer/Message.hx b/src/pixabay_sourcer/Message.hx new file mode 100644 index 0000000..18833d6 --- /dev/null +++ b/src/pixabay_sourcer/Message.hx @@ -0,0 +1,6 @@ +package pixabay_sourcer; + +typedef Message = { + type:String, + data:Array +}; \ No newline at end of file diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..d6444af --- /dev/null +++ b/test.sh @@ -0,0 +1,7 @@ +#! /bin/bash + +if [ ! -d node_modules ]; then + npm install +fi + +haxe -D test build.hxml \ No newline at end of file