@yaks/extension 0.0.0-stage → 0.2.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +275 -2
- package/_dist/act.d.ts +12 -0
- package/_dist/act.d.ts.map +1 -0
- package/_dist/answer.d.ts +42 -0
- package/_dist/answer.d.ts.map +1 -0
- package/_dist/build.d.ts +3 -0
- package/_dist/build.d.ts.map +1 -0
- package/_dist/connect.d.ts +22 -0
- package/_dist/connect.d.ts.map +1 -0
- package/_dist/consent.d.ts +16 -0
- package/_dist/consent.d.ts.map +1 -0
- package/_dist/manifest.d.ts +22 -0
- package/_dist/manifest.d.ts.map +1 -0
- package/_dist/mod.d.ts +12 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/read.d.ts +11 -0
- package/_dist/read.d.ts.map +1 -0
- package/_dist/screen.d.ts +22 -0
- package/_dist/screen.d.ts.map +1 -0
- package/_dist/signin.d.ts +27 -0
- package/_dist/signin.d.ts.map +1 -0
- package/_dist/tools.d.ts +23 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/touch.d.ts +32 -0
- package/_dist/touch.d.ts.map +1 -0
- package/_dist/wire.d.ts +33 -0
- package/_dist/wire.d.ts.map +1 -0
- package/act.js +41 -0
- package/act.js.map +1 -0
- package/act.ts +67 -0
- package/answer.js +157 -0
- package/answer.js.map +1 -0
- package/answer.ts +269 -0
- package/background.ts +190 -0
- package/build.js +67 -0
- package/build.js.map +1 -0
- package/build.ts +71 -0
- package/chrome.ts +76 -0
- package/connect.js +59 -0
- package/connect.js.map +1 -0
- package/connect.ts +85 -0
- package/consent.js +50 -0
- package/consent.js.map +1 -0
- package/consent.ts +67 -0
- package/deno.json +1 -0
- package/give.ts +124 -0
- package/icon.js +60 -0
- package/icon.js.map +1 -0
- package/icon.ts +53 -0
- package/manifest.js +50 -0
- package/manifest.js.map +1 -0
- package/manifest.ts +53 -0
- package/mod.js +15 -0
- package/mod.js.map +1 -0
- package/mod.ts +15 -0
- package/package.json +36 -4
- package/popup.css +117 -0
- package/popup.html +5 -0
- package/popup.ts +89 -0
- package/read.js +61 -0
- package/read.js.map +1 -0
- package/read.ts +80 -0
- package/screen.js +41 -0
- package/screen.js.map +1 -0
- package/screen.ts +125 -0
- package/signin.js +77 -0
- package/signin.js.map +1 -0
- package/signin.ts +94 -0
- package/tools.js +183 -0
- package/tools.js.map +1 -0
- package/tools.ts +192 -0
- package/touch.js +216 -0
- package/touch.js.map +1 -0
- package/touch.ts +259 -0
- package/wire.js +66 -0
- package/wire.js.map +1 -0
- package/wire.ts +91 -0
package/touch.js
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// Touching the page a tab shows: listing what can be pressed or typed into,
|
|
2
|
+
// each named by a short **ref** (`e1`, `e2`, …), and clicking or typing into
|
|
3
|
+
// one by its ref.
|
|
4
|
+
//
|
|
5
|
+
// `touch` runs inside the page, like ./read.ts `read`: the browser glue hands
|
|
6
|
+
// it to `chrome.scripting.executeScript({func: touch, args})`, so it closes
|
|
7
|
+
// over nothing in this module. The elements a listing named are held in
|
|
8
|
+
// `held`, which in the browser is the extension's own isolated world for the
|
|
9
|
+
// page: its globals outlive one call and the page's scripts can't see them,
|
|
10
|
+
// so a page can't move a ref onto another element. A new page is a new world,
|
|
11
|
+
// so its refs start again. Its result crosses back by structured clone.
|
|
12
|
+
/** One thing on a page a person can press or type into, as the agent is told
|
|
13
|
+
* of it. Every string but `ref` and `role` is the page's own words. */ /** A touch on a document, holding refs in `held`. */ export let touch = (asked, doc = document, held = globalThis)=>{
|
|
14
|
+
let KEY = '__touched';
|
|
15
|
+
let LIMIT = 400;
|
|
16
|
+
let PICK = 'a[href],button,input,select,textarea,summary,[contenteditable],' + '[role=button],[role=link],[role=checkbox],[role=radio],[role=switch],' + '[role=tab],[role=menuitem],[role=option],[role=textbox],' + '[role=searchbox],[role=combobox]';
|
|
17
|
+
let HIDDEN = /(^|;)\s*(display\s*:\s*none|visibility\s*:\s*hidden)/i;
|
|
18
|
+
let PRESSED = /^(submit|button|reset|image)$/;
|
|
19
|
+
let words = (s)=>(s ?? '').replace(/\s+/g, ' ').trim();
|
|
20
|
+
let cut = (s, n = 200)=>s.length > n ? s.slice(0, n) + '…' : s;
|
|
21
|
+
let hidden = (el)=>{
|
|
22
|
+
for(let at = el; at; at = at.parentElement){
|
|
23
|
+
if (at.hasAttribute('hidden') || at.getAttribute('aria-hidden') == 'true' || HIDDEN.test(at.getAttribute('style') ?? '')) return true;
|
|
24
|
+
}
|
|
25
|
+
return 'checkVisibility' in el && !el.checkVisibility({
|
|
26
|
+
visibilityProperty: true
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
let tag = (el)=>el.tagName.toLowerCase();
|
|
30
|
+
let type = (el)=>(el.getAttribute('type') ?? '').toLowerCase();
|
|
31
|
+
let editable = (el)=>[
|
|
32
|
+
'',
|
|
33
|
+
'true',
|
|
34
|
+
'plaintext-only'
|
|
35
|
+
].includes(el.getAttribute('contenteditable') ?? 'false');
|
|
36
|
+
let roleOf = (el)=>{
|
|
37
|
+
let given = el.getAttribute('role');
|
|
38
|
+
if (given) return given;
|
|
39
|
+
let t = tag(el);
|
|
40
|
+
if (t == 'a') return 'link';
|
|
41
|
+
if (t == 'button' || t == 'summary') return 'button';
|
|
42
|
+
if (t == 'select') return 'select';
|
|
43
|
+
if (t == 'textarea' || editable(el)) return 'textbox';
|
|
44
|
+
if (t != 'input') return t;
|
|
45
|
+
let ty = type(el);
|
|
46
|
+
return PRESSED.test(ty) ? 'button' : ty == 'checkbox' || ty == 'radio' ? ty : ty == 'search' ? 'searchbox' : 'textbox';
|
|
47
|
+
};
|
|
48
|
+
let labels = (el)=>{
|
|
49
|
+
let id = el.getAttribute('id');
|
|
50
|
+
let by = id ? Array.from(doc.querySelectorAll('label')).filter((l)=>l.getAttribute('for') == id) : [];
|
|
51
|
+
let wrap = el.closest('label');
|
|
52
|
+
return [
|
|
53
|
+
...by,
|
|
54
|
+
...wrap ? [
|
|
55
|
+
wrap
|
|
56
|
+
] : []
|
|
57
|
+
].map((l)=>words(l.textContent)).join(' ');
|
|
58
|
+
};
|
|
59
|
+
let nameOf = (el)=>{
|
|
60
|
+
let by = (el.getAttribute('aria-labelledby') ?? '').split(/\s+/).map((id)=>id && doc.getElementById(id)?.textContent).filter(Boolean);
|
|
61
|
+
let field = [
|
|
62
|
+
'input',
|
|
63
|
+
'select',
|
|
64
|
+
'textarea'
|
|
65
|
+
].includes(tag(el));
|
|
66
|
+
let pressed = tag(el) == 'input' && PRESSED.test(type(el));
|
|
67
|
+
return words(el.getAttribute('aria-label') || by.join(' ') || (field && !pressed ? labels(el) : '') || (pressed ? el.getAttribute('value') || el.getAttribute('alt') : '') || (field ? '' : el.textContent) || el.querySelector('img[alt]')?.getAttribute('alt') || el.getAttribute('placeholder') || el.getAttribute('title') || el.getAttribute('name'));
|
|
68
|
+
};
|
|
69
|
+
let formOf = (el)=>el.form ?? el.closest('form');
|
|
70
|
+
let describe = (el, ref)=>{
|
|
71
|
+
let role = roleOf(el);
|
|
72
|
+
let t = tag(el), ty = type(el);
|
|
73
|
+
let c = {
|
|
74
|
+
ref,
|
|
75
|
+
role,
|
|
76
|
+
name: cut(nameOf(el), 120)
|
|
77
|
+
};
|
|
78
|
+
let form = formOf(el);
|
|
79
|
+
if (form) c.form = true;
|
|
80
|
+
if (form && (t == 'button' && (ty == '' || ty == 'submit') || t == 'input' && (ty == 'submit' || ty == 'image'))) c.submits = true;
|
|
81
|
+
if (role == 'searchbox' || el.closest('[role=search],search')) {
|
|
82
|
+
c.search = true;
|
|
83
|
+
}
|
|
84
|
+
if (t == 'a') c.href = el.href || undefined;
|
|
85
|
+
if (role == 'checkbox' || role == 'radio' || role == 'switch') {
|
|
86
|
+
c.checked = el.checked ?? el.getAttribute('aria-checked') == 'true';
|
|
87
|
+
} else if (t == 'select') {
|
|
88
|
+
let s = el;
|
|
89
|
+
let opts = Array.from(s.querySelectorAll('option'));
|
|
90
|
+
c.options = opts.slice(0, 30).map((o)=>cut(words(o.textContent), 60));
|
|
91
|
+
let chosen = opts.find((o)=>o.selected) ?? opts[0];
|
|
92
|
+
if (chosen) c.value = words(chosen.textContent);
|
|
93
|
+
} else if (t == 'textarea' || t == 'input' && !PRESSED.test(ty)) {
|
|
94
|
+
if (ty != 'password') c.value = cut(el.value ?? '');
|
|
95
|
+
} else if (editable(el)) c.value = cut(words(el.textContent));
|
|
96
|
+
if (el.hasAttribute('disabled') || el.getAttribute('aria-disabled') == 'true') {
|
|
97
|
+
c.disabled = true;
|
|
98
|
+
}
|
|
99
|
+
return c;
|
|
100
|
+
};
|
|
101
|
+
let kept = held[KEY] ?? [];
|
|
102
|
+
let at = (ref = '')=>{
|
|
103
|
+
let el = /^e\d+$/.test(ref) ? kept[Number(ref.slice(1)) - 1] : undefined;
|
|
104
|
+
return el?.isConnected ? el : undefined;
|
|
105
|
+
};
|
|
106
|
+
if (asked.op == 'list') {
|
|
107
|
+
let all = Array.from(doc.querySelectorAll(PICK)).filter((el)=>!(tag(el) == 'input' && type(el) == 'hidden') && !hidden(el));
|
|
108
|
+
held[KEY] = all.slice(0, LIMIT);
|
|
109
|
+
let listed = held[KEY].map((el, i)=>describe(el, `e${i + 1}`));
|
|
110
|
+
return all.length > LIMIT ? {
|
|
111
|
+
controls: listed,
|
|
112
|
+
error: `The page has ${all.length} controls; these are its first ${LIMIT}.`
|
|
113
|
+
} : {
|
|
114
|
+
controls: listed
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
let el = at(asked.ref);
|
|
118
|
+
if (!el) return {
|
|
119
|
+
controls: []
|
|
120
|
+
};
|
|
121
|
+
let was = describe(el, asked.ref);
|
|
122
|
+
if (asked.op == 'find') return {
|
|
123
|
+
controls: [
|
|
124
|
+
was
|
|
125
|
+
]
|
|
126
|
+
};
|
|
127
|
+
if (was.name != asked.name) {
|
|
128
|
+
return {
|
|
129
|
+
controls: [
|
|
130
|
+
was
|
|
131
|
+
],
|
|
132
|
+
error: 'it changed while the person was asked'
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
if (was.disabled) return {
|
|
136
|
+
controls: [
|
|
137
|
+
was
|
|
138
|
+
],
|
|
139
|
+
error: 'it is disabled'
|
|
140
|
+
};
|
|
141
|
+
let h = el;
|
|
142
|
+
h.scrollIntoView?.({
|
|
143
|
+
block: 'center'
|
|
144
|
+
});
|
|
145
|
+
h.focus?.();
|
|
146
|
+
if (asked.op == 'click') {
|
|
147
|
+
h.click();
|
|
148
|
+
return {
|
|
149
|
+
controls: [
|
|
150
|
+
was
|
|
151
|
+
]
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
let text = asked.text ?? '';
|
|
155
|
+
let win = doc.defaultView;
|
|
156
|
+
let fire = (name)=>el.dispatchEvent(new (win?.Event ?? Event)(name, {
|
|
157
|
+
bubbles: true
|
|
158
|
+
}));
|
|
159
|
+
if (tag(el) == 'select') {
|
|
160
|
+
let s = el;
|
|
161
|
+
let opt = Array.from(s.querySelectorAll('option')).find((o)=>words(o.textContent) == words(text) || o.value == text);
|
|
162
|
+
if (!opt) return {
|
|
163
|
+
controls: [
|
|
164
|
+
was
|
|
165
|
+
],
|
|
166
|
+
error: `it has no option “${text}”`
|
|
167
|
+
};
|
|
168
|
+
for (let o of Array.from(s.querySelectorAll('option'))){
|
|
169
|
+
o.selected = o == opt;
|
|
170
|
+
}
|
|
171
|
+
fire('input');
|
|
172
|
+
fire('change');
|
|
173
|
+
} else if (editable(el)) {
|
|
174
|
+
let selection = doc.getSelection?.();
|
|
175
|
+
selection?.selectAllChildren(el);
|
|
176
|
+
if (!doc.execCommand?.('insertText', false, text)) el.textContent = text;
|
|
177
|
+
fire('input');
|
|
178
|
+
} else if ('value' in el) {
|
|
179
|
+
// A page's framework watches the value through its own setter, so the
|
|
180
|
+
// value is set through the element's prototype, as typing would.
|
|
181
|
+
let proto = Object.getPrototypeOf(el);
|
|
182
|
+
let set = Object.getOwnPropertyDescriptor(proto, 'value')?.set;
|
|
183
|
+
set ? set.call(el, text) : el.value = text;
|
|
184
|
+
fire('input');
|
|
185
|
+
fire('change');
|
|
186
|
+
} else return {
|
|
187
|
+
controls: [
|
|
188
|
+
was
|
|
189
|
+
],
|
|
190
|
+
error: 'it is not a field'
|
|
191
|
+
};
|
|
192
|
+
if (asked.enter) {
|
|
193
|
+
// A document without keyboard events (one parsed, not shown) gets plain
|
|
194
|
+
// ones, which the page's handlers hear all the same.
|
|
195
|
+
let Key = win?.KeyboardEvent ?? win?.Event ?? Event;
|
|
196
|
+
let key = (name)=>el.dispatchEvent(new Key(name, {
|
|
197
|
+
key: 'Enter',
|
|
198
|
+
code: 'Enter',
|
|
199
|
+
bubbles: true,
|
|
200
|
+
cancelable: true
|
|
201
|
+
}));
|
|
202
|
+
// A synthetic key presses nothing, so the form it would submit is
|
|
203
|
+
// submitted here, unless the page took the key for itself.
|
|
204
|
+
let free = key('keydown');
|
|
205
|
+
key('keypress');
|
|
206
|
+
key('keyup');
|
|
207
|
+
let form = formOf(el);
|
|
208
|
+
if (free && form) form.requestSubmit();
|
|
209
|
+
}
|
|
210
|
+
return {
|
|
211
|
+
controls: [
|
|
212
|
+
was
|
|
213
|
+
]
|
|
214
|
+
};
|
|
215
|
+
};
|
|
216
|
+
//# sourceMappingURL=touch.js.map
|
package/touch.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"touch.js","sources":["./touch.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,6EAA6E;AAC7E,kBAAkB;AAClB,EAAE;AACF,8EAA8E;AAC9E,4EAA4E;AAC5E,wEAAwE;AACxE,6EAA6E;AAC7E,4EAA4E;AAC5E,8EAA8E;AAC9E,wEAAwE;AAExE;qEACqE,GAuCrE,mDAAmD,GACnD,OAAO,IAAI,QAAQ,CACjB,OACA,MAAgB,QAAQ,EACxB,OAAgC,UAAqC;EAErE,IAAI,MAAM;EACV,IAAI,QAAQ;EACZ,IA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SAAS;QACT,YAAY;MACd;IAEJ,kEAAkE;IAClE,2DAA2D;IAC3D,IAAI,OAAO,IAAI;IACf,IAAI;IACJ,IAAI;IACJ,IAAI,OAAO,OAAO;IAClB,IAAI,QAAQ,MAAM,KAAK,aAAa;EACtC;EACA,OAAO;IAAE,UAAU;MAAC;KAAI;EAAC;AAC3B,EAAC"}
|
package/touch.ts
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
// Touching the page a tab shows: listing what can be pressed or typed into,
|
|
2
|
+
// each named by a short **ref** (`e1`, `e2`, …), and clicking or typing into
|
|
3
|
+
// one by its ref.
|
|
4
|
+
//
|
|
5
|
+
// `touch` runs inside the page, like ./read.ts `read`: the browser glue hands
|
|
6
|
+
// it to `chrome.scripting.executeScript({func: touch, args})`, so it closes
|
|
7
|
+
// over nothing in this module. The elements a listing named are held in
|
|
8
|
+
// `held`, which in the browser is the extension's own isolated world for the
|
|
9
|
+
// page: its globals outlive one call and the page's scripts can't see them,
|
|
10
|
+
// so a page can't move a ref onto another element. A new page is a new world,
|
|
11
|
+
// so its refs start again. Its result crosses back by structured clone.
|
|
12
|
+
|
|
13
|
+
/** One thing on a page a person can press or type into, as the agent is told
|
|
14
|
+
* of it. Every string but `ref` and `role` is the page's own words. */
|
|
15
|
+
export type Control = {
|
|
16
|
+
ref: string
|
|
17
|
+
/** What it is: `link`, `button`, `textbox`, `searchbox`, `checkbox`,
|
|
18
|
+
* `radio`, `select`, or the role the page gave it. */
|
|
19
|
+
role: string
|
|
20
|
+
/** What it says or is labelled, as a person reads it. */
|
|
21
|
+
name: string
|
|
22
|
+
/** Pressing it submits a form: a submit button. */
|
|
23
|
+
submits?: boolean
|
|
24
|
+
/** It sits in a form, so Enter in it may submit that form. */
|
|
25
|
+
form?: boolean
|
|
26
|
+
/** It is a search: its own role or type says so, or a search landmark
|
|
27
|
+
* holds it. */
|
|
28
|
+
search?: boolean
|
|
29
|
+
href?: string
|
|
30
|
+
/** A field's value; a password's is never read. */
|
|
31
|
+
value?: string
|
|
32
|
+
checked?: boolean
|
|
33
|
+
disabled?: boolean
|
|
34
|
+
/** A select's options, as their words. */
|
|
35
|
+
options?: string[]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** What a touch gives back: the controls it found or acted on, or why it
|
|
39
|
+
* could not. */
|
|
40
|
+
export type Touched = { controls: Control[]; error?: string }
|
|
41
|
+
|
|
42
|
+
/** What a touch does: `list` names every control afresh; `find` describes the
|
|
43
|
+
* one a ref names; `click` and `type` act on it, once its name is still the
|
|
44
|
+
* name given, so a page that changed under a wait is not acted on. */
|
|
45
|
+
export type Touch = {
|
|
46
|
+
op: 'list' | 'find' | 'click' | 'type'
|
|
47
|
+
ref?: string
|
|
48
|
+
name?: string
|
|
49
|
+
text?: string
|
|
50
|
+
enter?: boolean
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** A touch on a document, holding refs in `held`. */
|
|
54
|
+
export let touch = (
|
|
55
|
+
asked: Touch,
|
|
56
|
+
doc: Document = document,
|
|
57
|
+
held: Record<string, unknown> = globalThis as Record<string, unknown>,
|
|
58
|
+
): Touched => {
|
|
59
|
+
let KEY = '__touched'
|
|
60
|
+
let LIMIT = 400
|
|
61
|
+
let PICK = 'a[href],button,input,select,textarea,summary,[contenteditable],' +
|
|
62
|
+
'[role=button],[role=link],[role=checkbox],[role=radio],[role=switch],' +
|
|
63
|
+
'[role=tab],[role=menuitem],[role=option],[role=textbox],' +
|
|
64
|
+
'[role=searchbox],[role=combobox]'
|
|
65
|
+
let HIDDEN = /(^|;)\s*(display\s*:\s*none|visibility\s*:\s*hidden)/i
|
|
66
|
+
let PRESSED = /^(submit|button|reset|image)$/
|
|
67
|
+
let words = (s: string | null | undefined) =>
|
|
68
|
+
(s ?? '').replace(/\s+/g, ' ').trim()
|
|
69
|
+
let cut = (s: string, n = 200) => s.length > n ? s.slice(0, n) + '…' : s
|
|
70
|
+
let hidden = (el: Element): boolean => {
|
|
71
|
+
for (let at: Element | null = el; at; at = at.parentElement) {
|
|
72
|
+
if (
|
|
73
|
+
at.hasAttribute('hidden') || at.getAttribute('aria-hidden') == 'true' ||
|
|
74
|
+
HIDDEN.test(at.getAttribute('style') ?? '')
|
|
75
|
+
) return true
|
|
76
|
+
}
|
|
77
|
+
return 'checkVisibility' in el &&
|
|
78
|
+
!el.checkVisibility({ visibilityProperty: true })
|
|
79
|
+
}
|
|
80
|
+
let tag = (el: Element) => el.tagName.toLowerCase()
|
|
81
|
+
let type = (el: Element) => (el.getAttribute('type') ?? '').toLowerCase()
|
|
82
|
+
let editable = (el: Element) =>
|
|
83
|
+
['', 'true', 'plaintext-only'].includes(
|
|
84
|
+
el.getAttribute('contenteditable') ?? 'false',
|
|
85
|
+
)
|
|
86
|
+
let roleOf = (el: Element): string => {
|
|
87
|
+
let given = el.getAttribute('role')
|
|
88
|
+
if (given) return given
|
|
89
|
+
let t = tag(el)
|
|
90
|
+
if (t == 'a') return 'link'
|
|
91
|
+
if (t == 'button' || t == 'summary') return 'button'
|
|
92
|
+
if (t == 'select') return 'select'
|
|
93
|
+
if (t == 'textarea' || editable(el)) return 'textbox'
|
|
94
|
+
if (t != 'input') return t
|
|
95
|
+
let ty = type(el)
|
|
96
|
+
return PRESSED.test(ty)
|
|
97
|
+
? 'button'
|
|
98
|
+
: ty == 'checkbox' || ty == 'radio'
|
|
99
|
+
? ty
|
|
100
|
+
: ty == 'search'
|
|
101
|
+
? 'searchbox'
|
|
102
|
+
: 'textbox'
|
|
103
|
+
}
|
|
104
|
+
let labels = (el: Element) => {
|
|
105
|
+
let id = el.getAttribute('id')
|
|
106
|
+
let by = id
|
|
107
|
+
? Array.from(doc.querySelectorAll('label'))
|
|
108
|
+
.filter((l) => l.getAttribute('for') == id)
|
|
109
|
+
: []
|
|
110
|
+
let wrap = el.closest('label')
|
|
111
|
+
return [...by, ...wrap ? [wrap] : []].map((l) => words(l.textContent))
|
|
112
|
+
.join(' ')
|
|
113
|
+
}
|
|
114
|
+
let nameOf = (el: Element): string => {
|
|
115
|
+
let by = (el.getAttribute('aria-labelledby') ?? '').split(/\s+/)
|
|
116
|
+
.map((id) => id && doc.getElementById(id)?.textContent).filter(Boolean)
|
|
117
|
+
let field = ['input', 'select', 'textarea'].includes(tag(el))
|
|
118
|
+
let pressed = tag(el) == 'input' && PRESSED.test(type(el))
|
|
119
|
+
return words(
|
|
120
|
+
el.getAttribute('aria-label') ||
|
|
121
|
+
by.join(' ') ||
|
|
122
|
+
(field && !pressed ? labels(el) : '') ||
|
|
123
|
+
(pressed ? el.getAttribute('value') || el.getAttribute('alt') : '') ||
|
|
124
|
+
(field ? '' : el.textContent) ||
|
|
125
|
+
el.querySelector('img[alt]')?.getAttribute('alt') ||
|
|
126
|
+
el.getAttribute('placeholder') ||
|
|
127
|
+
el.getAttribute('title') ||
|
|
128
|
+
el.getAttribute('name'),
|
|
129
|
+
)
|
|
130
|
+
}
|
|
131
|
+
let formOf = (el: Element) =>
|
|
132
|
+
(el as HTMLInputElement).form ?? el.closest('form')
|
|
133
|
+
let describe = (el: Element, ref: string): Control => {
|
|
134
|
+
let role = roleOf(el)
|
|
135
|
+
let t = tag(el), ty = type(el)
|
|
136
|
+
let c: Control = { ref, role, name: cut(nameOf(el), 120) }
|
|
137
|
+
let form = formOf(el)
|
|
138
|
+
if (form) c.form = true
|
|
139
|
+
if (
|
|
140
|
+
form && (
|
|
141
|
+
(t == 'button' && (ty == '' || ty == 'submit')) ||
|
|
142
|
+
(t == 'input' && (ty == 'submit' || ty == 'image'))
|
|
143
|
+
)
|
|
144
|
+
) c.submits = true
|
|
145
|
+
if (role == 'searchbox' || el.closest('[role=search],search')) {
|
|
146
|
+
c.search = true
|
|
147
|
+
}
|
|
148
|
+
if (t == 'a') c.href = (el as HTMLAnchorElement).href || undefined
|
|
149
|
+
if (role == 'checkbox' || role == 'radio' || role == 'switch') {
|
|
150
|
+
c.checked = (el as HTMLInputElement).checked ??
|
|
151
|
+
el.getAttribute('aria-checked') == 'true'
|
|
152
|
+
} else if (t == 'select') {
|
|
153
|
+
let s = el as HTMLSelectElement
|
|
154
|
+
let opts = Array.from(s.querySelectorAll('option'))
|
|
155
|
+
c.options = opts.slice(0, 30).map((o) => cut(words(o.textContent), 60))
|
|
156
|
+
let chosen = opts.find((o) => o.selected) ?? opts[0]
|
|
157
|
+
if (chosen) c.value = words(chosen.textContent)
|
|
158
|
+
} else if (t == 'textarea' || (t == 'input' && !PRESSED.test(ty))) {
|
|
159
|
+
if (ty != 'password') c.value = cut((el as HTMLInputElement).value ?? '')
|
|
160
|
+
} else if (editable(el)) c.value = cut(words(el.textContent))
|
|
161
|
+
if (
|
|
162
|
+
el.hasAttribute('disabled') || el.getAttribute('aria-disabled') == 'true'
|
|
163
|
+
) {
|
|
164
|
+
c.disabled = true
|
|
165
|
+
}
|
|
166
|
+
return c
|
|
167
|
+
}
|
|
168
|
+
let kept = (held[KEY] ?? []) as Element[]
|
|
169
|
+
let at = (ref = '') => {
|
|
170
|
+
let el = /^e\d+$/.test(ref) ? kept[Number(ref.slice(1)) - 1] : undefined
|
|
171
|
+
return el?.isConnected ? el : undefined
|
|
172
|
+
}
|
|
173
|
+
if (asked.op == 'list') {
|
|
174
|
+
let all = Array.from(doc.querySelectorAll(PICK)).filter((el) =>
|
|
175
|
+
!(tag(el) == 'input' && type(el) == 'hidden') && !hidden(el)
|
|
176
|
+
)
|
|
177
|
+
held[KEY] = all.slice(0, LIMIT)
|
|
178
|
+
let listed = (held[KEY] as Element[]).map((el, i) =>
|
|
179
|
+
describe(el, `e${i + 1}`)
|
|
180
|
+
)
|
|
181
|
+
return all.length > LIMIT
|
|
182
|
+
? {
|
|
183
|
+
controls: listed,
|
|
184
|
+
error:
|
|
185
|
+
`The page has ${all.length} controls; these are its first ${LIMIT}.`,
|
|
186
|
+
}
|
|
187
|
+
: { controls: listed }
|
|
188
|
+
}
|
|
189
|
+
let el = at(asked.ref)
|
|
190
|
+
if (!el) return { controls: [] }
|
|
191
|
+
let was = describe(el, asked.ref!)
|
|
192
|
+
if (asked.op == 'find') return { controls: [was] }
|
|
193
|
+
if (was.name != asked.name) {
|
|
194
|
+
return {
|
|
195
|
+
controls: [was],
|
|
196
|
+
error: 'it changed while the person was asked',
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if (was.disabled) return { controls: [was], error: 'it is disabled' }
|
|
200
|
+
let h = el as HTMLElement
|
|
201
|
+
h.scrollIntoView?.({ block: 'center' })
|
|
202
|
+
h.focus?.()
|
|
203
|
+
if (asked.op == 'click') {
|
|
204
|
+
h.click()
|
|
205
|
+
return { controls: [was] }
|
|
206
|
+
}
|
|
207
|
+
let text = asked.text ?? ''
|
|
208
|
+
let win = doc.defaultView
|
|
209
|
+
let fire = (name: string) =>
|
|
210
|
+
el!.dispatchEvent(new (win?.Event ?? Event)(name, { bubbles: true }))
|
|
211
|
+
if (tag(el) == 'select') {
|
|
212
|
+
let s = el as HTMLSelectElement
|
|
213
|
+
let opt = Array.from(s.querySelectorAll('option')).find((o) =>
|
|
214
|
+
words(o.textContent) == words(text) || o.value == text
|
|
215
|
+
)
|
|
216
|
+
if (!opt) return { controls: [was], error: `it has no option “${text}”` }
|
|
217
|
+
for (let o of Array.from(s.querySelectorAll('option'))) {
|
|
218
|
+
o.selected = o == opt
|
|
219
|
+
}
|
|
220
|
+
fire('input')
|
|
221
|
+
fire('change')
|
|
222
|
+
} else if (editable(el)) {
|
|
223
|
+
let selection = doc.getSelection?.()
|
|
224
|
+
selection?.selectAllChildren(el)
|
|
225
|
+
if (!doc.execCommand?.('insertText', false, text)) el.textContent = text
|
|
226
|
+
fire('input')
|
|
227
|
+
} else if ('value' in el) {
|
|
228
|
+
// A page's framework watches the value through its own setter, so the
|
|
229
|
+
// value is set through the element's prototype, as typing would.
|
|
230
|
+
let proto = Object.getPrototypeOf(el)
|
|
231
|
+
let set = Object.getOwnPropertyDescriptor(proto, 'value')?.set
|
|
232
|
+
set ? set.call(el, text) : ((el as HTMLInputElement).value = text)
|
|
233
|
+
fire('input')
|
|
234
|
+
fire('change')
|
|
235
|
+
} else return { controls: [was], error: 'it is not a field' }
|
|
236
|
+
if (asked.enter) {
|
|
237
|
+
// A document without keyboard events (one parsed, not shown) gets plain
|
|
238
|
+
// ones, which the page's handlers hear all the same.
|
|
239
|
+
let Key =
|
|
240
|
+
(win?.KeyboardEvent ?? win?.Event ?? Event) as typeof KeyboardEvent
|
|
241
|
+
let key = (name: string) =>
|
|
242
|
+
el!.dispatchEvent(
|
|
243
|
+
new Key(name, {
|
|
244
|
+
key: 'Enter',
|
|
245
|
+
code: 'Enter',
|
|
246
|
+
bubbles: true,
|
|
247
|
+
cancelable: true,
|
|
248
|
+
}),
|
|
249
|
+
)
|
|
250
|
+
// A synthetic key presses nothing, so the form it would submit is
|
|
251
|
+
// submitted here, unless the page took the key for itself.
|
|
252
|
+
let free = key('keydown')
|
|
253
|
+
key('keypress')
|
|
254
|
+
key('keyup')
|
|
255
|
+
let form = formOf(el)
|
|
256
|
+
if (free && form) form.requestSubmit()
|
|
257
|
+
}
|
|
258
|
+
return { controls: [was] }
|
|
259
|
+
}
|
package/wire.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// The frames between a host's agent and a browser that lends it a tab, said
|
|
2
|
+
// once for every host: a call goes to the browser, an answer comes back with
|
|
3
|
+
// the call's id. The socket they travel on is the host's; how a browser opens
|
|
4
|
+
// it with its bearer token is said here too (`protocols`, `bearerOf`), since
|
|
5
|
+
// both ends must agree on it.
|
|
6
|
+
import { fromBase64url, toBase64url } from "@yaks/fp";
|
|
7
|
+
/** A call frame. */ export let call = (id, tool, args = {})=>JSON.stringify({
|
|
8
|
+
call: {
|
|
9
|
+
id,
|
|
10
|
+
tool,
|
|
11
|
+
args
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
/** An answer frame. */ export let answer = (a)=>JSON.stringify({
|
|
15
|
+
answer: a
|
|
16
|
+
});
|
|
17
|
+
let parsed = (data)=>{
|
|
18
|
+
if (typeof data != 'string') return null;
|
|
19
|
+
try {
|
|
20
|
+
let v = JSON.parse(data);
|
|
21
|
+
return v && typeof v == 'object' ? v : null;
|
|
22
|
+
} catch {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
/** The call a frame holds, or null for anything else. */ export let callOf = (data)=>{
|
|
27
|
+
let c = parsed(data)?.call;
|
|
28
|
+
return c && typeof c.id == 'string' && typeof c.tool == 'string' ? {
|
|
29
|
+
id: c.id,
|
|
30
|
+
tool: c.tool,
|
|
31
|
+
args: c.args ?? {}
|
|
32
|
+
} : null;
|
|
33
|
+
};
|
|
34
|
+
/** The answer a frame holds, or null for anything else. */ export let answerOf = (data)=>{
|
|
35
|
+
let a = parsed(data)?.answer;
|
|
36
|
+
return a && typeof a.id == 'string' ? a : null;
|
|
37
|
+
};
|
|
38
|
+
/** A frame the browser sends every so often to keep its socket open. A host
|
|
39
|
+
* answers nothing and ignores it, as it ignores every frame that is not an
|
|
40
|
+
* answer. */ export let PING = '{"ping":true}';
|
|
41
|
+
// A browser's WebSocket can set no header, so the bearer token rides in the
|
|
42
|
+
// subprotocol list beside the plain `yaks` the host answers with. base64url
|
|
43
|
+
// without padding, because a subprotocol is an HTTP token and `=` is not one.
|
|
44
|
+
/** The subprotocol the socket speaks, and the one the host's 101 names. */ export let PROTOCOL = 'yaks';
|
|
45
|
+
let BEARER = 'bearer.';
|
|
46
|
+
/// protocols('abc') -> ['yaks', 'bearer.YWJj']
|
|
47
|
+
/** The subprotocols a browser opens its socket with, for a bearer token. */ export let protocols = (token)=>[
|
|
48
|
+
PROTOCOL,
|
|
49
|
+
BEARER + toBase64url(new TextEncoder().encode(token))
|
|
50
|
+
];
|
|
51
|
+
/// bearerOf('yaks, bearer.YWJj') -> 'abc'
|
|
52
|
+
/// bearerOf('yaks') -> null
|
|
53
|
+
/// bearerOf(null) -> null
|
|
54
|
+
/** The bearer token a socket's `sec-websocket-protocol` header carries, or
|
|
55
|
+
* null when it carries none. */ export let bearerOf = (header)=>{
|
|
56
|
+
let named = header?.split(',').map((p)=>p.trim()).find((p)=>p.startsWith(BEARER));
|
|
57
|
+
if (!named) return null;
|
|
58
|
+
try {
|
|
59
|
+
return new TextDecoder('utf-8', {
|
|
60
|
+
fatal: true
|
|
61
|
+
}).decode(fromBase64url(named.slice(BEARER.length))) || null;
|
|
62
|
+
} catch {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
//# sourceMappingURL=wire.js.map
|
package/wire.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wire.js","sources":["./wire.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,6EAA6E;AAC7E,8EAA8E;AAC9E,6EAA6E;AAC7E,8BAA8B;AAE9B,SAAS,aAAa,EAAE,WAAW,wBAA6B;AAiBhE,kBAAkB,GAClB,OAAO,IAAI,OAAO,CAAC,IAAY,MAAc,OAAa,CAAC,CAAC,GAC1D,KAAK,SAAS,CAAC;IAAE,MAAM;MAAE;MAAI;MAAM;IAAK;EAAE,GAAiB;AAE7D,qBAAqB,GACrB,OAAO,IAAI,SAAS,CAAC,IACnB,KAAK,SAAS,CAAC;IAAE,QAAQ;EAAE,GAAmB;AAEhD,IAAI,SAAS,CAAC;EACZ,IAAI,OAAO,QAAQ,UAAU,OAAO;EACpC,IAAI;IACF,IAAI,IAAI,KAAK,KAAK,CAAC;IACnB,OAAO,KAAK,OAAO,KAAK,WAAW,IAAI;EACzC,EAAE,OAAM;IACN,OAAO;EACT;AACF;AAEA,uDAAuD,GACvD,OAAO,IAAI,SAAS,CAAC;EACnB,IAAI,IAAI,OAAO,OAAO;EACtB,OAAO,KAAK,OAAO,EAAE,EAAE,IAAI,YAAY,OAAO,EAAE,IAAI,IAAI,WACpD;IAAE,IAAI,EAAE,EAAE;IAAE,MAAM,EAAE,IAAI;IAAE,MAAM,EAAE,IAAI,IAAI,CAAC;EAAE,IAC7C;AACN,EAAC;AAED,yDAAyD,GACzD,OAAO,IAAI,WAAW,CAAC;EACrB,IAAI,IAAI,OAAO,OAAO;EACtB,OAAO,KAAK,OAAO,EAAE,EAAE,IAAI,WAAW,IAAI;AAC5C,EAAC;AAED;;WAEW,GACX,OAAO,IAAI,OAAO,gBAAe;AAEjC,4EAA4E;AAC5E,4EAA4E;AAC5E,8EAA8E;AAC9E,yEAAyE,GACzE,OAAO,IAAI,WAAW,OAAM;AAE5B,IAAI,SAAS;AAEb,+CAA+C;AAC/C,0EAA0E,GAC1E,OAAO,IAAI,YAAY,CAAC,QAA4B;IAClD;IACA,SAAS,YAAY,IAAI,cAAc,MAAM,CAAC;GAC/C,CAAA;AAED,0CAA0C;AAC1C,4BAA4B;AAC5B,0BAA0B;AAC1B;8BAC8B,GAC9B,OAAO,IAAI,WAAW,CAAC;EACrB,IAAI,QAAQ,QAAQ,MAAM,KAAK,IAAI,CAAC,IAAM,EAAE,IAAI,IAC7C,KAAK,CAAC,IAAM,EAAE,UAAU,CAAC;EAC5B,IAAI,CAAC,OAAO,OAAO;EACnB,IAAI;IACF,OAAO,IAAI,YAAY,SAAS;MAAE,OAAO;IAAK,GAC3C,MAAM,CAAC,cAAc,MAAM,KAAK,CAAC,OAAO,MAAM,OAAO;EAC1D,EAAE,OAAM;IACN,OAAO;EACT;AACF,EAAC"}
|
package/wire.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// The frames between a host's agent and a browser that lends it a tab, said
|
|
2
|
+
// once for every host: a call goes to the browser, an answer comes back with
|
|
3
|
+
// the call's id. The socket they travel on is the host's; how a browser opens
|
|
4
|
+
// it with its bearer token is said here too (`protocols`, `bearerOf`), since
|
|
5
|
+
// both ends must agree on it.
|
|
6
|
+
|
|
7
|
+
import { fromBase64url, toBase64url } from '@yaks/fp'
|
|
8
|
+
|
|
9
|
+
/** A tool call the host sends the browser. */
|
|
10
|
+
export type Call = { call: { id: string; tool: string; args: Args } }
|
|
11
|
+
|
|
12
|
+
/** The arguments a call carries, as the model gave them. */
|
|
13
|
+
export type Args = Record<string, unknown>
|
|
14
|
+
|
|
15
|
+
/** An image an answer carries, base64 so it fits a text frame. */
|
|
16
|
+
export type Shot = { mime: string; base64: string }
|
|
17
|
+
|
|
18
|
+
/** What the browser answers a call: words for the model, an image beside
|
|
19
|
+
* them, or why it could not. */
|
|
20
|
+
export type Answer = {
|
|
21
|
+
answer: { id: string; text?: string; image?: Shot; error?: string }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A call frame. */
|
|
25
|
+
export let call = (id: string, tool: string, args: Args = {}): string =>
|
|
26
|
+
JSON.stringify({ call: { id, tool, args } } satisfies Call)
|
|
27
|
+
|
|
28
|
+
/** An answer frame. */
|
|
29
|
+
export let answer = (a: Answer['answer']): string =>
|
|
30
|
+
JSON.stringify({ answer: a } satisfies Answer)
|
|
31
|
+
|
|
32
|
+
let parsed = (data: unknown): Record<string, unknown> | null => {
|
|
33
|
+
if (typeof data != 'string') return null
|
|
34
|
+
try {
|
|
35
|
+
let v = JSON.parse(data)
|
|
36
|
+
return v && typeof v == 'object' ? v : null
|
|
37
|
+
} catch {
|
|
38
|
+
return null
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The call a frame holds, or null for anything else. */
|
|
43
|
+
export let callOf = (data: unknown): Call['call'] | null => {
|
|
44
|
+
let c = parsed(data)?.call as Call['call'] | undefined
|
|
45
|
+
return c && typeof c.id == 'string' && typeof c.tool == 'string'
|
|
46
|
+
? { id: c.id, tool: c.tool, args: c.args ?? {} }
|
|
47
|
+
: null
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The answer a frame holds, or null for anything else. */
|
|
51
|
+
export let answerOf = (data: unknown): Answer['answer'] | null => {
|
|
52
|
+
let a = parsed(data)?.answer as Answer['answer'] | undefined
|
|
53
|
+
return a && typeof a.id == 'string' ? a : null
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A frame the browser sends every so often to keep its socket open. A host
|
|
57
|
+
* answers nothing and ignores it, as it ignores every frame that is not an
|
|
58
|
+
* answer. */
|
|
59
|
+
export let PING = '{"ping":true}'
|
|
60
|
+
|
|
61
|
+
// A browser's WebSocket can set no header, so the bearer token rides in the
|
|
62
|
+
// subprotocol list beside the plain `yaks` the host answers with. base64url
|
|
63
|
+
// without padding, because a subprotocol is an HTTP token and `=` is not one.
|
|
64
|
+
/** The subprotocol the socket speaks, and the one the host's 101 names. */
|
|
65
|
+
export let PROTOCOL = 'yaks'
|
|
66
|
+
|
|
67
|
+
let BEARER = 'bearer.'
|
|
68
|
+
|
|
69
|
+
/// protocols('abc') -> ['yaks', 'bearer.YWJj']
|
|
70
|
+
/** The subprotocols a browser opens its socket with, for a bearer token. */
|
|
71
|
+
export let protocols = (token: string): string[] => [
|
|
72
|
+
PROTOCOL,
|
|
73
|
+
BEARER + toBase64url(new TextEncoder().encode(token)),
|
|
74
|
+
]
|
|
75
|
+
|
|
76
|
+
/// bearerOf('yaks, bearer.YWJj') -> 'abc'
|
|
77
|
+
/// bearerOf('yaks') -> null
|
|
78
|
+
/// bearerOf(null) -> null
|
|
79
|
+
/** The bearer token a socket's `sec-websocket-protocol` header carries, or
|
|
80
|
+
* null when it carries none. */
|
|
81
|
+
export let bearerOf = (header: string | null): string | null => {
|
|
82
|
+
let named = header?.split(',').map((p) => p.trim())
|
|
83
|
+
.find((p) => p.startsWith(BEARER))
|
|
84
|
+
if (!named) return null
|
|
85
|
+
try {
|
|
86
|
+
return new TextDecoder('utf-8', { fatal: true })
|
|
87
|
+
.decode(fromBase64url(named.slice(BEARER.length))) || null
|
|
88
|
+
} catch {
|
|
89
|
+
return null
|
|
90
|
+
}
|
|
91
|
+
}
|