mirror of
https://github.com/penpot/penpot.git
synced 2025-01-23 23:18:48 -05:00
✨ Properly configure page default timeouts (exporter).
This commit is contained in:
parent
fb2d1e7953
commit
3fdcea78e4
3 changed files with 53 additions and 38 deletions
|
@ -6,14 +6,17 @@
|
|||
|
||||
(ns app.browser
|
||||
(:require
|
||||
["puppeteer-cluster" :as ppc]
|
||||
[app.common.data :as d]
|
||||
[app.config :as cf]
|
||||
[lambdaisland.glogi :as log]
|
||||
[promesa.core :as p]
|
||||
["puppeteer-cluster" :as ppc]))
|
||||
[promesa.core :as p]))
|
||||
|
||||
;; --- BROWSER API
|
||||
|
||||
(def USER-AGENT
|
||||
(def default-timeout 30000)
|
||||
(def default-viewport {:width 1920 :height 1080 :scale 1})
|
||||
(def default-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"))
|
||||
|
||||
|
@ -23,15 +26,25 @@
|
|||
(let [page (unchecked-get props "page")]
|
||||
(f page)))))
|
||||
|
||||
(defn emulate!
|
||||
[page {:keys [viewport user-agent scale]
|
||||
:or {user-agent USER-AGENT
|
||||
scale 1}}]
|
||||
(let [[width height] viewport]
|
||||
(.emulate ^js page #js {:viewport #js {:width width
|
||||
:height height
|
||||
:deviceScaleFactor scale}
|
||||
:userAgent user-agent})))
|
||||
(defn set-cookie!
|
||||
[page {:keys [key value domain]}]
|
||||
(.setCookie ^js page #js {:name key
|
||||
:value value
|
||||
:domain domain}))
|
||||
|
||||
(defn configure-page!
|
||||
[page {:keys [timeout cookie user-agent viewport]}]
|
||||
(let [timeout (or timeout default-timeout)
|
||||
user-agent (or user-agent default-user-agent)
|
||||
viewport (d/merge default-viewport viewport)]
|
||||
(p/do!
|
||||
(.setViewport ^js page #js {:width (:width viewport)
|
||||
:height (:height viewport)
|
||||
:deviceScaleFactor (:scale viewport)})
|
||||
(.setUserAgent ^js page user-agent)
|
||||
(.setDefaultTimeout ^js page timeout)
|
||||
(when cookie
|
||||
(set-cookie! page cookie)))))
|
||||
|
||||
(defn navigate!
|
||||
([page url] (navigate! page url nil))
|
||||
|
@ -43,10 +56,9 @@
|
|||
[page ms]
|
||||
(.waitForTimeout ^js page ms))
|
||||
|
||||
|
||||
(defn wait-for
|
||||
([page selector] (wait-for page selector nil))
|
||||
([page selector {:keys [visible] :or {visible false}}]
|
||||
([page selector {:keys [visible timeout] :or {visible false timeout 10000}}]
|
||||
(.waitForSelector ^js page selector #js {:visible visible})))
|
||||
|
||||
(defn screenshot
|
||||
|
@ -71,11 +83,6 @@
|
|||
[frame selector]
|
||||
(.$$ ^js frame selector))
|
||||
|
||||
(defn set-cookie!
|
||||
[page {:keys [key value domain]}]
|
||||
(.setCookie ^js page #js {:name key
|
||||
:value value
|
||||
:domain domain}))
|
||||
|
||||
;; --- BROWSER STATE
|
||||
|
||||
|
|
|
@ -40,16 +40,20 @@
|
|||
|
||||
(screenshot [page uri cookie]
|
||||
(log/info :uri uri)
|
||||
(p/do!
|
||||
(bw/emulate! page {:viewport [1920 1080]
|
||||
:scale scale})
|
||||
(bw/set-cookie! page cookie)
|
||||
(bw/navigate! page uri)
|
||||
(bw/eval! page (js* "() => document.body.style.background = 'transparent'"))
|
||||
(p/let [dom (bw/select page "#screenshot")]
|
||||
(case type
|
||||
:png (bw/screenshot dom {:omit-background? true :type type})
|
||||
:jpeg (bw/screenshot dom {:omit-background? false :type type})))))]
|
||||
(let [viewport {:width 1920
|
||||
:height 1080
|
||||
:scale scale}
|
||||
options {:viewport viewport
|
||||
:cookie cookie}]
|
||||
(p/do!
|
||||
(bw/configure-page! page options)
|
||||
(bw/navigate! page uri)
|
||||
(bw/eval! page (js* "() => document.body.style.background = 'transparent'"))
|
||||
(bw/wait-for page "#screenshot")
|
||||
(p/let [dom (bw/select page "#screenshot")]
|
||||
(case type
|
||||
:png (bw/screenshot dom {:omit-background? true :type type})
|
||||
:jpeg (bw/screenshot dom {:omit-background? false :type type}))))))]
|
||||
|
||||
(bw/exec! browser handle)))
|
||||
|
||||
|
|
|
@ -253,15 +253,19 @@
|
|||
result))
|
||||
|
||||
(render-in-page [page {:keys [uri cookie] :as rctx}]
|
||||
(p/do!
|
||||
(bw/emulate! page {:viewport [1920 1080]
|
||||
:scale 4})
|
||||
(bw/set-cookie! page cookie)
|
||||
(bw/navigate! page uri)
|
||||
;; (bw/wait-for page "#screenshot foreignObject" {:visible true})
|
||||
(bw/sleep page 2000)
|
||||
;; (bw/eval! page (js* "() => document.body.style.background = 'transparent'"))
|
||||
page))
|
||||
(let [viewport {:width 1920
|
||||
:height 1080
|
||||
:scale 4}
|
||||
options {:viewport viewport
|
||||
:timeout 15000
|
||||
:cookie cookie}]
|
||||
(p/do!
|
||||
(bw/configure-page! page options)
|
||||
(bw/navigate! page uri)
|
||||
(bw/wait-for page "#screenshot")
|
||||
(bw/sleep page 2000)
|
||||
;; (bw/eval! page (js* "() => document.body.style.background = 'transparent'"))
|
||||
page)))
|
||||
|
||||
(handle [rctx page]
|
||||
(p/let [page (render-in-page page rctx)]
|
||||
|
|
Loading…
Add table
Reference in a new issue