From a981895cb5e4d62fcf8c5ba70d27ce6ed5262c9d Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Mon, 8 Jan 2018 08:30:57 -0500 Subject: [PATCH] Add background script; implement custom build system --- .gitignore | 3 +- ext/manifest.json | 11 +++--- project.clj | 9 ++--- src/background/looped_in/background.cljs | 11 ++++++ src/build/looped_in/build.clj | 12 +++++++ src/content/looped_in/content.cljs | 44 ++++++++++++++++++++++++ src/looped_in/core.cljs | 22 ------------ 7 files changed, 78 insertions(+), 34 deletions(-) create mode 100644 src/background/looped_in/background.cljs create mode 100644 src/build/looped_in/build.clj create mode 100644 src/content/looped_in/content.cljs delete mode 100644 src/looped_in/core.cljs diff --git a/.gitignore b/.gitignore index 5a0febb..f0de844 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ pom.xml.asc .hgignore .hg/ out/ -ext/main.js \ No newline at end of file +ext/content.js +ext/background.js \ No newline at end of file diff --git a/ext/manifest.json b/ext/manifest.json index 135db2a..62483c7 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -11,15 +11,16 @@ { "matches": [""], "js": [ - "main.js" + "content.js" ] } ], + "background": { + "scripts": ["background.js"] + }, "browser_action": { - "default_icon": { - "16": "icons/icon.svg", - "32": "icons/icon.svg" - } + "default_icon": "icons/icon.svg", + "default_title": "Looped In" }, "content_security_policy": "script-src 'self'; object-src 'self'; connect-src 'self' http://hn.algolia.com" } diff --git a/project.clj b/project.clj index 90c9988..cfcccea 100644 --- a/project.clj +++ b/project.clj @@ -7,9 +7,6 @@ [org.clojure/clojurescript "1.9.946"] [org.clojure/core.async "0.3.465"] [cljs-ajax "0.7.3"]] - :plugins [[lein-cljsbuild "1.1.7"]] - :cljsbuild {:builds - [{:source-paths ["src"] - :compiler {:output-to "ext/main.js" - :output-dir "out" - :optimizations :whitespace}}]}) + :source-paths ["src/build"] + :main looped-in.build + :aliases {"build" ["run"]}) diff --git a/src/background/looped_in/background.cljs b/src/background/looped_in/background.cljs new file mode 100644 index 0000000..7173bb6 --- /dev/null +++ b/src/background/looped_in/background.cljs @@ -0,0 +1,11 @@ +(ns looped-in.background) + +(enable-console-print!) + +(-> js/browser + (.-runtime) + (.-onMessage) + (.addListener (fn [msg] + (-> js/browser + (.-browserAction) + (.setBadgeText (.-numComments msg)))))) diff --git a/src/build/looped_in/build.clj b/src/build/looped_in/build.clj new file mode 100644 index 0000000..b83d069 --- /dev/null +++ b/src/build/looped_in/build.clj @@ -0,0 +1,12 @@ +(ns looped-in.build + (:require [cljs.build.api :refer [build]])) + +(defn -main [& args] + (build "src/content" {:output-to "ext/content.js" + :output-dir "out" + :optimizations :whitespace + :pretty-print true}) + (build "src/background" {:output-to "ext/background.js" + :output-dir "out" + :optimizations :whitespace + :pretty-print true})) diff --git a/src/content/looped_in/content.cljs b/src/content/looped_in/content.cljs new file mode 100644 index 0000000..e1dec7b --- /dev/null +++ b/src/content/looped_in/content.cljs @@ -0,0 +1,44 @@ +(ns looped-in.content + (:require-macros [cljs.core.async.macros :refer [go]]) + (:require [ajax.core :refer [GET]] + [cljs.core.async :as async :refer [chan !]])) + +(enable-console-print!) + +(defn fetch-submission + "Fetches submissions from Hacker News by `url`" + [url] + (let [response-chan (chan)] + (GET "http://hn.algolia.com/api/v1/search" + {:params {"query" url + "hitsPerPage" 1000 + "restrictSearchableAttributes" "url"} + :handler (fn [res] (go (>! response-chan res))) + :error-handler (fn [err] (go (>! response-chan err)))}) + response-chan)) + +(defn filter-response + "Filters a response from hn.algolia.com to give just the relevant results" + ;; TODO implement a cache by URL and timestamp that caches the results of the + ;; request and this method for 5 minutes using localStorage + [url response] + (let [{:strs [hits]} response] + (filter #(= (get % "url") url) hits))) + +(defn total-num-comments + "Returns the total number of comments from some hits" + [hits] + (reduce (fn [acc {:strs [num_comments]}] (+ acc num_comments)) 0 hits)) + +(defn handle-hits + "Handles a filtered response" + [hits] + (js/console.log (clj->js hits)) + (let [num-comments (total-num-comments hits)] + (-> js/browser + (.-runtime) + (.sendMessage #js {:numComments num-comments})))) + +(let [current-url (-> js/window (.-location) (.-href)) + sub-chan (fetch-submission current-url)] + (go (handle-hits (filter-response current-url (!]])) - -(enable-console-print!) - -(defn fetch-submission - "Fetches submissions from Hacker News by `url`" - [url] - (let [response-chan (chan)] - (GET "http://hn.algolia.com/api/v1/search" - {:params {"query" url - "hitsPerPage" 1000 - "restrictSearchableAttributes" "url"} - :handler (fn [res] (go (>! response-chan res))) - :error-handler (fn [err] (go (>! response-chan err)))}) - response-chan)) - -(let [current-url (-> js/window (.-location) (.-href)) - sub-chan (fetch-submission current-url)] - (go (console.log (clj->js (