/
automation.clj
187 lines (143 loc) · 9.19 KB
/
automation.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
(ns chromex.ext.automation
"The chrome.automation API allows developers to access the
automation (accessibility) tree for the browser. The tree resembles the DOM
tree, but only exposes the semantic structure of a page. It can be
used to programmatically interact with a page by examining names, roles, and
states, listening for events, and performing actions on nodes.
* available since Chrome 77
* https://developer.chrome.com/extensions/automation"
(:refer-clojure :only [defmacro defn apply declare meta let partial])
(:require [chromex.wrapgen :refer [gen-wrap-helper]]
[chromex.callgen :refer [gen-call-helper gen-tap-all-events-call]]))
(declare api-table)
(declare gen-call)
; -- functions --------------------------------------------------------------------------------------------------------------
(defmacro get-tree
"Get the automation tree for the tab with the given tabId, or the current tab if no tabID is given, enabling automation if
necessary. Returns a tree with a placeholder root node; listen for the 'loadComplete' event to get a notification that the
tree has fully loaded (the previous root node reference will stop working at or before this point).
|tab-id| - https://developer.chrome.com/extensions/automation#property-getTree-tabId.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [root-node] where:
|root-node| - https://developer.chrome.com/extensions/automation#property-callback-rootNode.
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/extensions/automation#method-getTree."
([tab-id] (gen-call :function ::get-tree &form tab-id))
([] `(get-tree :omit)))
(defmacro get-desktop
"Get the automation tree for the whole desktop which consists of all on screen views. Note this API is currently only
supported on Chrome OS.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [root-node] where:
|root-node| - https://developer.chrome.com/extensions/automation#property-callback-rootNode.
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/extensions/automation#method-getDesktop."
([] (gen-call :function ::get-desktop &form)))
(defmacro get-focus
"Get the automation node that currently has focus, globally. Will return null if none of the nodes in any loaded trees have
focus.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [focused-node] where:
|focused-node| - https://developer.chrome.com/extensions/automation#property-callback-focusedNode.
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/extensions/automation#method-getFocus."
([] (gen-call :function ::get-focus &form)))
(defmacro add-tree-change-observer
"Add a tree change observer. Tree change observers are static/global, they listen to changes across all trees. Pass a filter
to determine what specific tree changes to listen to, and note that listnening to all tree changes can be expensive.
|filter| - https://developer.chrome.com/extensions/automation#property-addTreeChangeObserver-filter.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [tree-change] where:
|tree-change| - https://developer.chrome.com/extensions/automation#property-observer-treeChange.
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/extensions/automation#method-addTreeChangeObserver."
([filter] (gen-call :function ::add-tree-change-observer &form filter)))
(defmacro remove-tree-change-observer
"Remove a tree change observer.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [tree-change] where:
|tree-change| - https://developer.chrome.com/extensions/automation#property-observer-treeChange.
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/extensions/automation#method-removeTreeChangeObserver."
([] (gen-call :function ::remove-tree-change-observer &form)))
(defmacro set-document-selection
"Sets the selection in a tree. This creates a selection in a single tree (anchorObject and focusObject must have the same
root). Everything in the tree between the two node/offset pairs gets included in the selection. The anchor is where the
user started the selection, while the focus is the point at which the selection gets extended e.g. when dragging with a
mouse or using the keyboard. For nodes with the role staticText, the offset gives the character offset within the value
where the selection starts or ends, respectively.
|params| - https://developer.chrome.com/extensions/automation#property-setDocumentSelection-params.
https://developer.chrome.com/extensions/automation#method-setDocumentSelection."
([params] (gen-call :function ::set-document-selection &form params)))
(defmacro language-annotation-for-string-attribute
"Returns the detected languages for the provided string attribute as an array of LanguageSpan objects. There are several
guarantees about the format of the LanguageSpan array: 1. Is either empty or contains LanguageSpans that cover all indices
in the associated string attribute value. 2. Is sorted by increasing startIndex (those with smaller startIndex appear
first). 3. LanguageSpans are non-overlapping and contain exactly one language.
|attribute| - https://developer.chrome.com/extensions/automation#property-languageAnnotationForStringAttribute-attribute.
https://developer.chrome.com/extensions/automation#method-languageAnnotationForStringAttribute."
([attribute] (gen-call :function ::language-annotation-for-string-attribute &form attribute)))
; -- convenience ------------------------------------------------------------------------------------------------------------
(defmacro tap-all-events
"Taps all valid non-deprecated events in chromex.ext.automation namespace."
[chan]
(gen-tap-all-events-call api-table (meta &form) chan))
; ---------------------------------------------------------------------------------------------------------------------------
; -- API TABLE --------------------------------------------------------------------------------------------------------------
; ---------------------------------------------------------------------------------------------------------------------------
(def api-table
{:namespace "chrome.automation",
:since "77",
:functions
[{:id ::get-tree,
:name "getTree",
:callback? true,
:params
[{:name "tab-id", :optional? true, :type "integer"}
{:name "callback",
:optional? true,
:type :callback,
:callback {:params [{:name "root-node", :type "automation.AutomationNode"}]}}]}
{:id ::get-desktop,
:name "getDesktop",
:callback? true,
:params
[{:name "callback",
:type :callback,
:callback {:params [{:name "root-node", :type "automation.AutomationNode"}]}}]}
{:id ::get-focus,
:name "getFocus",
:callback? true,
:params
[{:name "callback",
:type :callback,
:callback {:params [{:name "focused-node", :type "automation.AutomationNode"}]}}]}
{:id ::add-tree-change-observer,
:name "addTreeChangeObserver",
:callback? true,
:params
[{:name "filter", :type "unknown-type"}
{:name "observer", :type :callback, :callback {:params [{:name "tree-change", :type "automation.TreeChange"}]}}]}
{:id ::remove-tree-change-observer,
:name "removeTreeChangeObserver",
:callback? true,
:params
[{:name "observer", :type :callback, :callback {:params [{:name "tree-change", :type "automation.TreeChange"}]}}]}
{:id ::set-document-selection,
:name "setDocumentSelection",
:params [{:name "params", :type "automation.SetDocumentSelectionParams"}]}
{:id ::language-annotation-for-string-attribute,
:name "languageAnnotationForStringAttribute",
:return-type "[array-of-objects]",
:params [{:name "attribute", :type "string"}]}]})
; -- helpers ----------------------------------------------------------------------------------------------------------------
; code generation for native API wrapper
(defmacro gen-wrap [kind item-id config & args]
(apply gen-wrap-helper api-table kind item-id config args))
; code generation for API call-site
(def gen-call (partial gen-call-helper api-table))