|
| 1 | +function serializeValue(value) { |
| 2 | + if (value === undefined) { |
| 3 | + return null; |
| 4 | + } |
| 5 | + return JSON.parse(JSON.stringify(value)); |
| 6 | +} |
| 7 | + |
| 8 | +function serializeError(error) { |
| 9 | + return { |
| 10 | + name: error?.name || "Error", |
| 11 | + message: error?.message || String(error), |
| 12 | + stack: error?.stack || "", |
| 13 | + }; |
| 14 | +} |
| 15 | + |
| 16 | +function getValueSetter(win, element) { |
| 17 | + if (element instanceof win.HTMLTextAreaElement) { |
| 18 | + return Object.getOwnPropertyDescriptor( |
| 19 | + win.HTMLTextAreaElement.prototype, |
| 20 | + "value", |
| 21 | + )?.set; |
| 22 | + } |
| 23 | + if (element instanceof win.HTMLInputElement) { |
| 24 | + return Object.getOwnPropertyDescriptor( |
| 25 | + win.HTMLInputElement.prototype, |
| 26 | + "value", |
| 27 | + )?.set; |
| 28 | + } |
| 29 | + return null; |
| 30 | +} |
| 31 | + |
| 32 | +function waitForDocumentReady(document, { allowInteractiveAfter = 0 } = {}) { |
| 33 | + if (!document) { |
| 34 | + return Promise.resolve(); |
| 35 | + } |
| 36 | + |
| 37 | + const readyState = document.readyState; |
| 38 | + if (readyState === "complete") { |
| 39 | + return Promise.resolve(); |
| 40 | + } |
| 41 | + if (readyState === "interactive" && allowInteractiveAfter !== false) { |
| 42 | + return new Promise((resolve) => { |
| 43 | + setTimeout(resolve, allowInteractiveAfter || 0); |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + return new Promise((resolve) => { |
| 48 | + const onReadyStateChange = () => { |
| 49 | + const state = document.readyState; |
| 50 | + if (state === "complete") { |
| 51 | + cleanup(); |
| 52 | + resolve(); |
| 53 | + return; |
| 54 | + } |
| 55 | + if (state === "interactive" && allowInteractiveAfter !== false) { |
| 56 | + cleanup(); |
| 57 | + setTimeout(resolve, allowInteractiveAfter || 0); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + const cleanup = () => { |
| 62 | + document.removeEventListener("readystatechange", onReadyStateChange, true); |
| 63 | + document.removeEventListener("DOMContentLoaded", onReadyStateChange, true); |
| 64 | + document.defaultView?.removeEventListener("load", onReadyStateChange, true); |
| 65 | + }; |
| 66 | + |
| 67 | + document.addEventListener("readystatechange", onReadyStateChange, true); |
| 68 | + document.addEventListener("DOMContentLoaded", onReadyStateChange, true); |
| 69 | + document.defaultView?.addEventListener("load", onReadyStateChange, true); |
| 70 | + onReadyStateChange(); |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +export class JasminumHeadlessChild extends JSWindowActorChild { |
| 75 | + _getWindow() { |
| 76 | + if (!this.contentWindow) { |
| 77 | + throw new Error("Headless actor contentWindow is not available"); |
| 78 | + } |
| 79 | + return this.contentWindow; |
| 80 | + } |
| 81 | + |
| 82 | + _getDocument() { |
| 83 | + const doc = this.document; |
| 84 | + if (!doc) { |
| 85 | + throw new Error("Headless actor document is not available"); |
| 86 | + } |
| 87 | + return doc; |
| 88 | + } |
| 89 | + |
| 90 | + _getElement(selector) { |
| 91 | + const element = this._getDocument().querySelector(selector); |
| 92 | + if (!element) { |
| 93 | + throw new Error(`Element not found for selector: ${selector}`); |
| 94 | + } |
| 95 | + return element; |
| 96 | + } |
| 97 | + |
| 98 | + async receiveMessage(message) { |
| 99 | + const doc = this.document; |
| 100 | + if (doc) { |
| 101 | + await waitForDocumentReady(doc, { allowInteractiveAfter: 0 }); |
| 102 | + } |
| 103 | + |
| 104 | + switch (message.name) { |
| 105 | + case "evaluate": |
| 106 | + return this._evaluate(message.data || {}); |
| 107 | + |
| 108 | + case "click": |
| 109 | + return this._click(message.data || {}); |
| 110 | + |
| 111 | + case "fill": |
| 112 | + return this._fill(message.data || {}); |
| 113 | + |
| 114 | + case "press": |
| 115 | + return this._press(message.data || {}); |
| 116 | + } |
| 117 | + |
| 118 | + throw new Error(`Unknown headless actor message: ${message.name}`); |
| 119 | + } |
| 120 | + |
| 121 | + async _evaluate(payload) { |
| 122 | + try { |
| 123 | + const fn = eval(`(${payload.source})`); |
| 124 | + if (typeof fn !== "function") { |
| 125 | + throw new Error("evaluate() expects a function source"); |
| 126 | + } |
| 127 | + const value = await fn( |
| 128 | + this._getWindow(), |
| 129 | + this._getDocument(), |
| 130 | + ...(payload.args || []), |
| 131 | + ); |
| 132 | + return { |
| 133 | + ok: true, |
| 134 | + value: serializeValue(value), |
| 135 | + }; |
| 136 | + } |
| 137 | + catch (error) { |
| 138 | + return { |
| 139 | + ok: false, |
| 140 | + error: serializeError(error), |
| 141 | + }; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + _click(payload) { |
| 146 | + const win = this._getWindow(); |
| 147 | + const element = this._getElement(payload.selector); |
| 148 | + element.scrollIntoView?.({ block: "center", inline: "center" }); |
| 149 | + element.click(); |
| 150 | + return { |
| 151 | + url: win.location.href, |
| 152 | + }; |
| 153 | + } |
| 154 | + |
| 155 | + _fill(payload) { |
| 156 | + const win = this._getWindow(); |
| 157 | + const element = this._getElement(payload.selector); |
| 158 | + |
| 159 | + if ( |
| 160 | + !( |
| 161 | + element instanceof win.HTMLInputElement |
| 162 | + || element instanceof win.HTMLTextAreaElement |
| 163 | + || element instanceof win.HTMLSelectElement |
| 164 | + ) |
| 165 | + ) { |
| 166 | + throw new Error(`Element is not a form control: ${payload.selector}`); |
| 167 | + } |
| 168 | + |
| 169 | + element.focus?.(); |
| 170 | + |
| 171 | + if (element instanceof win.HTMLSelectElement) { |
| 172 | + element.value = payload.value; |
| 173 | + } |
| 174 | + else { |
| 175 | + const setter = getValueSetter(win, element); |
| 176 | + const nextValue = payload.value == null ? "" : String(payload.value); |
| 177 | + if (payload.clear !== false && setter) { |
| 178 | + setter.call(element, ""); |
| 179 | + } |
| 180 | + if (setter) { |
| 181 | + setter.call(element, nextValue); |
| 182 | + } |
| 183 | + else { |
| 184 | + element.value = nextValue; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + element.dispatchEvent( |
| 189 | + new win.Event("input", { bubbles: true, cancelable: true }), |
| 190 | + ); |
| 191 | + if (payload.dispatchChange !== false) { |
| 192 | + element.dispatchEvent( |
| 193 | + new win.Event("change", { bubbles: true, cancelable: true }), |
| 194 | + ); |
| 195 | + } |
| 196 | + if (payload.blur) { |
| 197 | + element.blur?.(); |
| 198 | + } |
| 199 | + |
| 200 | + return { |
| 201 | + value: element.value, |
| 202 | + url: win.location.href, |
| 203 | + }; |
| 204 | + } |
| 205 | + |
| 206 | + _press(payload) { |
| 207 | + const win = this._getWindow(); |
| 208 | + const doc = this._getDocument(); |
| 209 | + const target = payload.selector |
| 210 | + ? this._getElement(payload.selector) |
| 211 | + : doc.activeElement || doc.body; |
| 212 | + |
| 213 | + target?.focus?.(); |
| 214 | + |
| 215 | + const eventInit = { |
| 216 | + key: payload.key, |
| 217 | + code: payload.code || payload.key, |
| 218 | + keyCode: payload.keyCode || (payload.key === "Enter" ? 13 : 0), |
| 219 | + which: payload.keyCode || (payload.key === "Enter" ? 13 : 0), |
| 220 | + bubbles: true, |
| 221 | + cancelable: true, |
| 222 | + }; |
| 223 | + |
| 224 | + for (const type of ["keydown", "keypress", "keyup"]) { |
| 225 | + target?.dispatchEvent(new win.KeyboardEvent(type, eventInit)); |
| 226 | + } |
| 227 | + |
| 228 | + if (payload.key === "Enter" && target instanceof win.HTMLInputElement) { |
| 229 | + target.form?.requestSubmit?.(); |
| 230 | + } |
| 231 | + |
| 232 | + return { |
| 233 | + url: win.location.href, |
| 234 | + }; |
| 235 | + } |
| 236 | +} |
0 commit comments