@addai/node 0.29.0 → 0.30.0

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.
@@ -92,10 +92,10 @@ function wrapperScript(realBrowser, extensionDir) {
92
92
  const seed = extensionDir
93
93
  ? `[ -f ${exports.SEED_SCRIPT} ] && (node ${exports.SEED_SCRIPT} >/dev/null 2>&1 &)\n`
94
94
  : '';
95
- return `#!/bin/sh
96
- # Written by @addai/node so +Ai Learn can watch this browser${extensionDir ? ' and the +Ai Vault can fill logins in it' : ''}.
97
- # Removing it costs you the page detail in a lesson${extensionDir ? ' and vault autofill' : ''} and nothing else.
98
- ${seed}exec ${realBrowser} --remote-debugging-port=${exports.DEBUG_PORT} --remote-debugging-address=127.0.0.1${ext} "$@"
95
+ return `#!/bin/sh
96
+ # Written by @addai/node so +Ai Learn can watch this browser${extensionDir ? ' and the +Ai Vault can fill logins in it' : ''}.
97
+ # Removing it costs you the page detail in a lesson${extensionDir ? ' and vault autofill' : ''} and nothing else.
98
+ ${seed}exec ${realBrowser} --remote-debugging-port=${exports.DEBUG_PORT} --remote-debugging-address=127.0.0.1${ext} "$@"
99
99
  `;
100
100
  }
101
101
  /** Is what is on disk already what we would write? Exact match, so a change to
@@ -1,173 +1,173 @@
1
- // The recorder that runs INSIDE every page of a desktop's browser.
2
- //
3
- // Injected through CDP (Page.addScriptToEvaluateOnNewDocument), so its source
4
- // is stringified and shipped as text. That has one hard consequence: nothing
5
- // here may reference module scope. The helpers are therefore passed into
6
- // pageRecorder as arguments and stitched together by injectedSource() below,
7
- // which keeps the functions the tests exercise and the functions the page runs
8
- // literally the same code.
9
- //
10
- // The rule this file exists to enforce: describe the CONTROL, never its VALUE.
11
- // The viewer already records what was typed, and that half gets redacted
12
- // server-side against the secret windows this file reports. If a value ever
13
- // leaked out of here it would bypass that entirely — it would be in the "safe"
14
- // track, the one stored raw.
15
-
16
- /** A control, as a person would point at it. No values, ever. */
17
- export function describeTarget(el) {
18
- if (!el || !el.tagName) return { tag: 'unknown' };
19
- const attr = (n) => {
20
- try { return el.getAttribute ? el.getAttribute(n) : null; } catch { return null; }
21
- };
22
- const clean = (s) => {
23
- const t = String(s == null ? '' : s).replace(/\s+/g, ' ').trim();
24
- return t ? t.slice(0, 60) : null;
25
- };
26
- let labelText = null;
27
- try {
28
- if (el.labels && el.labels.length) labelText = clean(el.labels[0].innerText);
29
- } catch { labelText = null; }
30
-
31
- let css = null;
32
- try {
33
- const bits = [];
34
- let node = el;
35
- for (let depth = 0; node && node.tagName && depth < 4; depth++) {
36
- let bit = node.tagName.toLowerCase();
37
- if (node.id) { bits.unshift(bit + '#' + node.id); break; }
38
- const cls = String(node.className || '').split(/\s+/).filter(Boolean).slice(0, 2);
39
- if (cls.length) bit += '.' + cls.join('.');
40
- bits.unshift(bit);
41
- node = node.parentElement;
42
- }
43
- css = bits.join('>') || null;
44
- } catch { css = null; }
45
-
46
- return {
47
- tag: el.tagName.toLowerCase(),
48
- role: attr('role') || null,
49
- // innerText, not value: a button says what it does, a field does not.
50
- text: el.tagName.toLowerCase() === 'input' ? null : clean(el.innerText),
51
- label: labelText || clean(attr('aria-label')),
52
- name: clean(el.name) || clean(attr('name')),
53
- id: clean(el.id) || null,
54
- placeholder: clean(el.placeholder) || clean(attr('placeholder')),
55
- css,
56
- };
57
- }
58
-
59
- /**
60
- * Is focus in something we must go blind for?
61
- *
62
- * Deliberately wider than input[type=password]: plenty of one-time-code and
63
- * "access code" fields are plain text inputs, and a lesson that teaches a
64
- * login will walk straight through one. Word-bounded so that "shipping" is not
65
- * a PIN and "cardigan" is not a card number.
66
- */
67
- export function isSecretField(el) {
68
- if (!el || !el.tagName) return false;
69
- const attr = (n) => {
70
- try { return el.getAttribute ? el.getAttribute(n) : null; } catch { return null; }
71
- };
72
- const type = String(el.type || attr('type') || '').toLowerCase();
73
- if (type === 'password') return true;
74
-
75
- const auto = String(attr('autocomplete') || '').toLowerCase();
76
- if (auto.includes('password') || auto.includes('one-time-code') || auto.includes('cc-number')) {
77
- return true;
78
- }
79
-
80
- let labelText = '';
81
- try {
82
- if (el.labels && el.labels.length) labelText = String(el.labels[0].innerText || '');
83
- } catch { labelText = ''; }
84
-
85
- const hay = [el.name, attr('name'), el.id, el.placeholder, attr('placeholder'),
86
- attr('aria-label'), labelText]
87
- .map((v) => String(v == null ? '' : v))
88
- .join(' ')
89
- .toLowerCase()
90
- .replace(/[^a-z0-9]+/g, ' ');
91
-
92
- const SECRET = new RegExp('(^| )(password|passwd|pass|passcode|secret|token|otp|' +
93
- 'cvv|cvc|pin|card number|cardnumber|security code|access code|auth code)( |$)');
94
- return SECRET.test(' ' + hay + ' ');
95
- }
96
-
97
- /**
98
- * Watch one document. Returns nothing; reports through window.__addaiLearn,
99
- * the CDP binding the recorder installs.
100
- *
101
- * Capture phase throughout, so a page that stops propagation on its own
102
- * handlers (most single-page apps do) cannot make a lesson go blank.
103
- */
104
- export function pageRecorder(describe, isSecret) {
105
- if (window.__addaiLearnInstalled) return;
106
- window.__addaiLearnInstalled = true;
107
-
108
- const send = (ev) => {
109
- try {
110
- if (typeof window.__addaiLearn === 'function') window.__addaiLearn(JSON.stringify(ev));
111
- } catch { /* a lesson must never break the page it is watching */ }
112
- };
113
-
114
- // Secret tracking has TWO independent triggers on purpose.
115
- //
116
- // focusin is the obvious one and it is not reliable: a browser window that
117
- // does not itself have focus changes activeElement without firing any focus
118
- // event at all (headless Chrome does exactly this, and so does a desktop
119
- // whose window manager has put another window on top). Relying on it alone
120
- // gives a silent failure whose consequence is a password in the timeline.
121
- //
122
- // So `input` opens a window too. Whatever else is true, a keystroke reaching
123
- // a secret field is proof we should already be blind.
124
- let secretOpen = false;
125
- const openSecret = () => {
126
- if (!secretOpen) { secretOpen = true; send({ kind: 'secret_focus' }); }
127
- };
128
- const closeSecret = () => {
129
- if (secretOpen) { secretOpen = false; send({ kind: 'secret_blur' }); }
130
- };
131
-
132
- const editable = (t) => {
133
- const tag = t && t.tagName ? t.tagName.toLowerCase() : '';
134
- return tag === 'input' || tag === 'textarea' || tag === 'select' ||
135
- !!(t && t.isContentEditable);
136
- };
137
-
138
- document.addEventListener('click', (e) => {
139
- send({ kind: 'page_click', target: describe(e.target) });
140
- // Deliberately does NOT close a secret window. Pressing "show password" or
141
- // "Sign in" mid-entry is a click on something harmless, and closing there
142
- // would leave a hole between it and the next keystroke — one that a
143
- // coalesced `type` event could land in unredacted. Over-inclusive is the
144
- // right direction for redaction; a window closes when focus really moves.
145
- }, true);
146
-
147
- const arrivedAt = (t) => {
148
- if (isSecret(t)) { openSecret(); return; }
149
- if (editable(t)) { closeSecret(); send({ kind: 'page_focus', target: describe(t) }); }
150
- };
151
-
152
- document.addEventListener('focusin', (e) => arrivedAt(e.target), true);
153
- // The backstop. Never carries what was typed — only that typing happened.
154
- document.addEventListener('input', (e) => arrivedAt(e.target), true);
155
-
156
- // A page can be torn down mid-password. Closing the window on unload means
157
- // an unclosed one really does mean "we never saw it end".
158
- window.addEventListener('pagehide', closeSecret, true);
159
-
160
- document.addEventListener('submit', (e) => {
161
- send({ kind: 'submit', target: describe(e.target) });
162
- }, true);
163
- }
164
-
165
- /** The exact text handed to Page.addScriptToEvaluateOnNewDocument. */
166
- export function injectedSource() {
167
- return '(function(){\n' +
168
- 'var describeTarget = ' + describeTarget.toString() + ';\n' +
169
- 'var isSecretField = ' + isSecretField.toString() + ';\n' +
170
- 'var pageRecorder = ' + pageRecorder.toString() + ';\n' +
171
- 'try { pageRecorder(describeTarget, isSecretField); } catch (e) {}\n' +
172
- '})();';
173
- }
1
+ // The recorder that runs INSIDE every page of a desktop's browser.
2
+ //
3
+ // Injected through CDP (Page.addScriptToEvaluateOnNewDocument), so its source
4
+ // is stringified and shipped as text. That has one hard consequence: nothing
5
+ // here may reference module scope. The helpers are therefore passed into
6
+ // pageRecorder as arguments and stitched together by injectedSource() below,
7
+ // which keeps the functions the tests exercise and the functions the page runs
8
+ // literally the same code.
9
+ //
10
+ // The rule this file exists to enforce: describe the CONTROL, never its VALUE.
11
+ // The viewer already records what was typed, and that half gets redacted
12
+ // server-side against the secret windows this file reports. If a value ever
13
+ // leaked out of here it would bypass that entirely — it would be in the "safe"
14
+ // track, the one stored raw.
15
+
16
+ /** A control, as a person would point at it. No values, ever. */
17
+ export function describeTarget(el) {
18
+ if (!el || !el.tagName) return { tag: 'unknown' };
19
+ const attr = (n) => {
20
+ try { return el.getAttribute ? el.getAttribute(n) : null; } catch { return null; }
21
+ };
22
+ const clean = (s) => {
23
+ const t = String(s == null ? '' : s).replace(/\s+/g, ' ').trim();
24
+ return t ? t.slice(0, 60) : null;
25
+ };
26
+ let labelText = null;
27
+ try {
28
+ if (el.labels && el.labels.length) labelText = clean(el.labels[0].innerText);
29
+ } catch { labelText = null; }
30
+
31
+ let css = null;
32
+ try {
33
+ const bits = [];
34
+ let node = el;
35
+ for (let depth = 0; node && node.tagName && depth < 4; depth++) {
36
+ let bit = node.tagName.toLowerCase();
37
+ if (node.id) { bits.unshift(bit + '#' + node.id); break; }
38
+ const cls = String(node.className || '').split(/\s+/).filter(Boolean).slice(0, 2);
39
+ if (cls.length) bit += '.' + cls.join('.');
40
+ bits.unshift(bit);
41
+ node = node.parentElement;
42
+ }
43
+ css = bits.join('>') || null;
44
+ } catch { css = null; }
45
+
46
+ return {
47
+ tag: el.tagName.toLowerCase(),
48
+ role: attr('role') || null,
49
+ // innerText, not value: a button says what it does, a field does not.
50
+ text: el.tagName.toLowerCase() === 'input' ? null : clean(el.innerText),
51
+ label: labelText || clean(attr('aria-label')),
52
+ name: clean(el.name) || clean(attr('name')),
53
+ id: clean(el.id) || null,
54
+ placeholder: clean(el.placeholder) || clean(attr('placeholder')),
55
+ css,
56
+ };
57
+ }
58
+
59
+ /**
60
+ * Is focus in something we must go blind for?
61
+ *
62
+ * Deliberately wider than input[type=password]: plenty of one-time-code and
63
+ * "access code" fields are plain text inputs, and a lesson that teaches a
64
+ * login will walk straight through one. Word-bounded so that "shipping" is not
65
+ * a PIN and "cardigan" is not a card number.
66
+ */
67
+ export function isSecretField(el) {
68
+ if (!el || !el.tagName) return false;
69
+ const attr = (n) => {
70
+ try { return el.getAttribute ? el.getAttribute(n) : null; } catch { return null; }
71
+ };
72
+ const type = String(el.type || attr('type') || '').toLowerCase();
73
+ if (type === 'password') return true;
74
+
75
+ const auto = String(attr('autocomplete') || '').toLowerCase();
76
+ if (auto.includes('password') || auto.includes('one-time-code') || auto.includes('cc-number')) {
77
+ return true;
78
+ }
79
+
80
+ let labelText = '';
81
+ try {
82
+ if (el.labels && el.labels.length) labelText = String(el.labels[0].innerText || '');
83
+ } catch { labelText = ''; }
84
+
85
+ const hay = [el.name, attr('name'), el.id, el.placeholder, attr('placeholder'),
86
+ attr('aria-label'), labelText]
87
+ .map((v) => String(v == null ? '' : v))
88
+ .join(' ')
89
+ .toLowerCase()
90
+ .replace(/[^a-z0-9]+/g, ' ');
91
+
92
+ const SECRET = new RegExp('(^| )(password|passwd|pass|passcode|secret|token|otp|' +
93
+ 'cvv|cvc|pin|card number|cardnumber|security code|access code|auth code)( |$)');
94
+ return SECRET.test(' ' + hay + ' ');
95
+ }
96
+
97
+ /**
98
+ * Watch one document. Returns nothing; reports through window.__addaiLearn,
99
+ * the CDP binding the recorder installs.
100
+ *
101
+ * Capture phase throughout, so a page that stops propagation on its own
102
+ * handlers (most single-page apps do) cannot make a lesson go blank.
103
+ */
104
+ export function pageRecorder(describe, isSecret) {
105
+ if (window.__addaiLearnInstalled) return;
106
+ window.__addaiLearnInstalled = true;
107
+
108
+ const send = (ev) => {
109
+ try {
110
+ if (typeof window.__addaiLearn === 'function') window.__addaiLearn(JSON.stringify(ev));
111
+ } catch { /* a lesson must never break the page it is watching */ }
112
+ };
113
+
114
+ // Secret tracking has TWO independent triggers on purpose.
115
+ //
116
+ // focusin is the obvious one and it is not reliable: a browser window that
117
+ // does not itself have focus changes activeElement without firing any focus
118
+ // event at all (headless Chrome does exactly this, and so does a desktop
119
+ // whose window manager has put another window on top). Relying on it alone
120
+ // gives a silent failure whose consequence is a password in the timeline.
121
+ //
122
+ // So `input` opens a window too. Whatever else is true, a keystroke reaching
123
+ // a secret field is proof we should already be blind.
124
+ let secretOpen = false;
125
+ const openSecret = () => {
126
+ if (!secretOpen) { secretOpen = true; send({ kind: 'secret_focus' }); }
127
+ };
128
+ const closeSecret = () => {
129
+ if (secretOpen) { secretOpen = false; send({ kind: 'secret_blur' }); }
130
+ };
131
+
132
+ const editable = (t) => {
133
+ const tag = t && t.tagName ? t.tagName.toLowerCase() : '';
134
+ return tag === 'input' || tag === 'textarea' || tag === 'select' ||
135
+ !!(t && t.isContentEditable);
136
+ };
137
+
138
+ document.addEventListener('click', (e) => {
139
+ send({ kind: 'page_click', target: describe(e.target) });
140
+ // Deliberately does NOT close a secret window. Pressing "show password" or
141
+ // "Sign in" mid-entry is a click on something harmless, and closing there
142
+ // would leave a hole between it and the next keystroke — one that a
143
+ // coalesced `type` event could land in unredacted. Over-inclusive is the
144
+ // right direction for redaction; a window closes when focus really moves.
145
+ }, true);
146
+
147
+ const arrivedAt = (t) => {
148
+ if (isSecret(t)) { openSecret(); return; }
149
+ if (editable(t)) { closeSecret(); send({ kind: 'page_focus', target: describe(t) }); }
150
+ };
151
+
152
+ document.addEventListener('focusin', (e) => arrivedAt(e.target), true);
153
+ // The backstop. Never carries what was typed — only that typing happened.
154
+ document.addEventListener('input', (e) => arrivedAt(e.target), true);
155
+
156
+ // A page can be torn down mid-password. Closing the window on unload means
157
+ // an unclosed one really does mean "we never saw it end".
158
+ window.addEventListener('pagehide', closeSecret, true);
159
+
160
+ document.addEventListener('submit', (e) => {
161
+ send({ kind: 'submit', target: describe(e.target) });
162
+ }, true);
163
+ }
164
+
165
+ /** The exact text handed to Page.addScriptToEvaluateOnNewDocument. */
166
+ export function injectedSource() {
167
+ return '(function(){\n' +
168
+ 'var describeTarget = ' + describeTarget.toString() + ';\n' +
169
+ 'var isSecretField = ' + isSecretField.toString() + ';\n' +
170
+ 'var pageRecorder = ' + pageRecorder.toString() + ';\n' +
171
+ 'try { pageRecorder(describeTarget, isSecretField); } catch (e) {}\n' +
172
+ '})();';
173
+ }