Add background script; implement custom build system

This commit is contained in:
Jeremy Dormitzer 2018-01-08 08:30:57 -05:00
parent e27287bf17
commit a981895cb5
No known key found for this signature in database
GPG Key ID: 04F17C0F5A32C320
7 changed files with 78 additions and 34 deletions

3
.gitignore vendored
View File

@ -10,4 +10,5 @@ pom.xml.asc
.hgignore
.hg/
out/
ext/main.js
ext/content.js
ext/background.js

View File

@ -11,15 +11,16 @@
{
"matches": ["<all_urls>"],
"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"
}

View File

@ -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"]})

View File

@ -0,0 +1,11 @@
(ns looped-in.background)
(enable-console-print!)
(-> js/browser
(.-runtime)
(.-onMessage)
(.addListener (fn [msg]
(-> js/browser
(.-browserAction)
(.setBadgeText (.-numComments msg))))))

View File

@ -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}))

View File

@ -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 (<! sub-chan)))))

View File

@ -1,22 +0,0 @@
(ns looped-in.core
(: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))
(let [current-url (-> js/window (.-location) (.-href))
sub-chan (fetch-submission current-url)]
(go (console.log (clj->js (<! sub-chan)))))