2021-02-15 12:15:16 +01:00
|
|
|
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
;;
|
|
|
|
;; This Source Code Form is "Incompatible With Secondary Licenses", as
|
|
|
|
;; defined by the Mozilla Public License, v. 2.0.
|
|
|
|
;;
|
|
|
|
;; Copyright (c) 2020-2021 UXBOX Labs SL
|
|
|
|
|
2020-06-29 16:07:48 +02:00
|
|
|
(ns app.browser
|
|
|
|
(:require
|
|
|
|
[lambdaisland.glogi :as log]
|
|
|
|
[promesa.core :as p]
|
|
|
|
["puppeteer-cluster" :as ppc]))
|
|
|
|
|
|
|
|
(def USER-AGENT
|
|
|
|
(str "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
|
|
|
"(KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"))
|
|
|
|
|
|
|
|
(defn exec!
|
|
|
|
[browser f]
|
|
|
|
(.execute ^js browser (fn [props]
|
|
|
|
(let [page (unchecked-get props "page")]
|
|
|
|
(f page)))))
|
|
|
|
|
|
|
|
(defn emulate!
|
2020-07-02 14:48:17 +02:00
|
|
|
[page {:keys [viewport user-agent scale]
|
|
|
|
:or {user-agent USER-AGENT
|
|
|
|
scale 1}}]
|
2020-06-29 16:07:48 +02:00
|
|
|
(let [[width height] viewport]
|
2020-07-30 15:23:11 +02:00
|
|
|
(.emulate ^js page #js {:viewport #js {:width width
|
|
|
|
:height height
|
|
|
|
:deviceScaleFactor scale}
|
|
|
|
:userAgent user-agent})))
|
2020-06-29 16:07:48 +02:00
|
|
|
|
|
|
|
(defn navigate!
|
|
|
|
([page url] (navigate! page url nil))
|
|
|
|
([page url {:keys [wait-until]
|
|
|
|
:or {wait-until "networkidle2"}}]
|
|
|
|
(.goto ^js page url #js {:waitUntil wait-until})))
|
|
|
|
|
|
|
|
(defn sleep
|
|
|
|
[page ms]
|
|
|
|
(.waitFor ^js page ms))
|
|
|
|
|
|
|
|
(defn screenshot
|
2020-07-02 14:48:17 +02:00
|
|
|
([frame] (screenshot frame nil))
|
2020-07-30 15:23:11 +02:00
|
|
|
([frame {:keys [full-page? omit-background? type]
|
2020-07-02 14:48:17 +02:00
|
|
|
:or {full-page? false
|
2020-07-30 15:23:11 +02:00
|
|
|
type "png"
|
2020-07-02 14:48:17 +02:00
|
|
|
omit-background? false}}]
|
|
|
|
(.screenshot ^js frame #js {:fullPage full-page?
|
2020-07-30 15:23:11 +02:00
|
|
|
:type (name type)
|
2020-07-02 14:48:17 +02:00
|
|
|
:omitBackground omit-background?})))
|
|
|
|
|
|
|
|
(defn eval!
|
|
|
|
[frame f]
|
|
|
|
(.evaluate ^js frame f))
|
|
|
|
|
|
|
|
(defn select
|
|
|
|
[frame selector]
|
|
|
|
(.$ ^js frame selector))
|
2020-06-29 16:07:48 +02:00
|
|
|
|
2020-07-30 15:23:11 +02:00
|
|
|
(defn select-all
|
|
|
|
[frame selector]
|
|
|
|
(.$$ ^js frame selector))
|
|
|
|
|
2020-06-29 16:07:48 +02:00
|
|
|
(defn set-cookie!
|
|
|
|
[page {:keys [key value domain]}]
|
|
|
|
(.setCookie ^js page #js {:name key
|
|
|
|
:value value
|
|
|
|
:domain domain}))
|
|
|
|
|
|
|
|
(defn start!
|
|
|
|
([] (start! nil))
|
|
|
|
([{:keys [concurrency concurrency-strategy]
|
2020-07-02 14:48:17 +02:00
|
|
|
:or {concurrency 10
|
|
|
|
concurrency-strategy :incognito}}]
|
2020-06-29 16:07:48 +02:00
|
|
|
(let [ccst (case concurrency-strategy
|
|
|
|
:browser (.-CONCURRENCY_BROWSER ^js ppc/Cluster)
|
|
|
|
:incognito (.-CONCURRENCY_CONTEXT ^js ppc/Cluster)
|
|
|
|
:page (.-CONCURRENCY_PAGE ^js ppc/Cluster))
|
|
|
|
opts #js {:concurrency ccst
|
2020-07-02 10:40:58 +02:00
|
|
|
:maxConcurrency concurrency
|
2020-07-02 14:48:17 +02:00
|
|
|
:puppeteerOptions #js {:args #js ["--no-sandbox"]}}]
|
2020-06-29 16:07:48 +02:00
|
|
|
(.launch ^js ppc/Cluster opts))))
|
|
|
|
|
|
|
|
(defn stop!
|
|
|
|
[instance]
|
|
|
|
(p/do!
|
|
|
|
(.idle ^js instance)
|
|
|
|
(.close ^js instance)
|
|
|
|
(log/info :msg "shutdown headless browser")
|
|
|
|
nil))
|