Skip to content

Commit

Permalink
allow user to specify custom url params for DevTools frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin committed Aug 25, 2016
1 parent 6b689cb commit 907df32
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/automation/dirac/automation.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@
:kind :error-log
:delay (or delay 100)}))

(defn get-frontend-url-params [devtools-id]
(automate-dirac-frontend! devtools-id {:action :get-frontend-url-params}))

(defn scrape [devtools-id scraper-name & args]
(automate-dirac-frontend! devtools-id {:action :scrape
:scraper scraper-name
Expand Down
14 changes: 8 additions & 6 deletions src/background/dirac/background/helpers.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
items (map (fn [[k v]] (str (encode k) "=" (encode v))) params)]
(string/join "&" items)))

(defn make-relative-url [path params]
{:pre [(map? params)]}
(let [non-empty-params (into {} (filter #(some? (second %)) params))]
(str path "?" (build-query non-empty-params))))
(defn make-relative-url [path params & [user-params-str]]
{:pre [(map? params)
(or (string? user-params-str) (nil? user-params-str))]}
(let [non-empty-params (into {} (filter #(some? (second %)) params))
url-params-segments (remove empty? [(build-query non-empty-params) user-params-str])]
(str path "?" (string/join "&" url-params-segments))))

; -- dirac frontend url -----------------------------------------------------------------------------------------------------

Expand All @@ -52,7 +54,7 @@
; chrome-extension://mjdnckdilfjoenmikegbbenflgjcmbid/devtools/front_end/inspector.html?devtools_id=1&dirac_flags=11111&ws=localhost:9222/devtools/page/76BE0A6D-412C-4592-BC3C-ED3ECB5DFF8C
(defn make-dirac-frontend-url [devtools-id options]
{:pre [devtools-id]}
(let [{:keys [backend-url flags reset-settings automate extra-url-params backend-api backend-css]} options]
(let [{:keys [backend-url flags reset-settings automate extra-url-params backend-api backend-css user-url-params]} options]
(assert backend-url)
(assert flags)
(let [html-file-path (get-dirac-main-html-file-path)
Expand All @@ -66,7 +68,7 @@
backend-api (assoc "backend_api" backend-api)
backend-css (assoc "backend_css" backend-css)
extra-url-params (merge extra-url-params))]
(runtime/get-url (make-relative-url html-file-path all-params)))))
(runtime/get-url (make-relative-url html-file-path all-params user-url-params)))))

(defn extract-devtools-id-from-url [url]
(utils/parse-int (get-query-param (str url) "devtools_id")))
Expand Down
9 changes: 8 additions & 1 deletion src/background/dirac/background/tools.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,18 @@
(assoc options :backend-css backend-css)))
options))

(defn provide-user-url-params [options]
(or
(if-let [user-url-params (options/get-option :user-frontend-url-params)]
(assoc options :user-url-params user-url-params))
options))

(defn prepare-options [initial-options]
(-> initial-options
(automate-if-marion-present)
(provide-backend-api-if-available)
(provide-backend-css-if-available)))
(provide-backend-css-if-available)
(provide-user-url-params)))

(defn connect-and-navigate-dirac-devtools! [frontend-tab-id backend-tab-id options]
(let [devtools-id (devtools/register! frontend-tab-id backend-tab-id)
Expand Down
4 changes: 4 additions & 0 deletions src/implant/dirac/implant/automation.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@
trigger-fn #(ocall (oget js/window "dirac") fn-name)]
(trigger-fn-and-wait! trigger-fn delay)))

(defn get-frontend-url-params []
(helpers/get-url-params))

; -- main dispatch ----------------------------------------------------------------------------------------------------------

(defn dispatch-command! [command]
Expand All @@ -144,6 +147,7 @@
:get-suggest-box-representation (get-suggest-box-representation)
:get-prompt-representation (get-prompt-representation)
:trigger-internal-error (trigger-internal-error! (:delay command) (:kind command))
:get-frontend-url-params (get-frontend-url-params)
:scrape (apply scrape (:scraper command) (:args command))
(warn "received unknown automation command:" (pr-str command))))

Expand Down
3 changes: 3 additions & 0 deletions src/implant/dirac/implant/helpers.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
(:require [chromex.support :refer-macros [oget ocall oapply]]
[chromex.logging :refer-macros [log warn error group group-end]]))

(defn get-url-params []
(oget js/window "location" "search"))

(defn get-query-param [name]
(if-let [runtime (oget js/window "Runtime")]
(ocall runtime "queryParam" name)
Expand Down
3 changes: 2 additions & 1 deletion src/options/dirac/options/ui.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
(f/checkbox "Enable clean URLs" data [:options :clean-urls])
(f/checkbox "Beautify function names" data [:options :beautify-function-names])
(f/checkbox "Use backend-supported API" data [:options :use-backend-supported-api])
(f/checkbox "Use backend-supported CSS" data [:options :use-backend-supported-css])])
(f/checkbox "Use backend-supported CSS" data [:options :use-backend-supported-css])]
(f/text "Extra frontend URL params:" data [:options :user-frontend-url-params]))
(f/form-buttons
(f/button "Reset to Defaults" reset-to-defaults!)
(f/button "Save and Exit" save-state-and-exit!))))))
Expand Down
3 changes: 2 additions & 1 deletion src/shared/dirac/options/model.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
:clean-urls true
:beautify-function-names true
:use-backend-supported-api true
:use-backend-supported-css true})
:use-backend-supported-css true
:user-frontend-url-params nil})

(defonce cached-options (atom nil))
(defonce chrome-event-channel (make-chrome-event-channel (chan)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
(:require [cljs.core.async :refer [timeout]]
[cljs.test :refer-macros [is testing]]
[dirac.settings :refer-macros [seconds minutes]]
[dirac.automation :refer-macros [<!* go-task with-scenario with-devtools] :as a]
[dirac.utils :as utils]))
[dirac.automation :refer-macros [<!* go-task with-scenario with-devtools with-options] :as a]
[clojure.string :as string]))

(go-task
(with-scenario "normal"))
(with-scenario "normal"
(testing "user-specified url params should get propagated to DevTools frontends"
(let [user-params "x=1&y=2"]
(with-options {:user-frontend-url-params user-params}
(with-devtools
(is (string/includes? (<!* a/get-frontend-url-params) user-params))))))))
17 changes: 16 additions & 1 deletion test/browser/transcripts/expected/suite01-misc.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
ns Testing dirac.tests.tasks.suite01.misc
automate open-tab-with-scenario! ["normal"]
automate store-options!
automate set-options! [{:user-frontend-url-params "x=1&y=2"}]
automate open-devtools!
extension handling command: open-dirac-devtools
extension register devtools #1
devtools #1 implant initialized
devtools #1 reset devtools settings
devtools #1 devtools ready
devtools #1 setCurrentPanel: elements
devtools #1 namespacesCache is cool now
automate #1 get-frontend-url-params
automate #1 close-devtools!
extension handling command: close-dirac-devtools
extension unregister devtools #1
automate restore-options!
automate close-tab-with-scenario! ["scenario-tab#1"]
summary Automated 2 actions with 0 check-points containing 0 assertions.
summary Automated 8 actions with 0 check-points containing 1 assertions.
0 failures, 0 errors.

0 comments on commit 907df32

Please sign in to comment.