Repository Public Research preview
1-- Set the value of a form field so that the page's own code notices.2-- Usage: osascript fill-field.applescript <window-id> <tab-index> '<css-selector>' '<value>'3-- Assigning el.value alone is ignored by React, Vue and similar frameworks: they patch the4-- property. Calling the native setter and then firing "input" and "change" works everywhere.5-- Prints "ok", or "not-found".6on run argv7 set w to (item 1 of argv) as integer8 set t to (item 2 of argv) as integer9 set js to "(function(sel, val){var el=document.querySelector(sel); if(!el) return 'not-found';" & ¬10 "var proto = el.tagName==='TEXTAREA' ? HTMLTextAreaElement.prototype : el.tagName==='SELECT' ? HTMLSelectElement.prototype : HTMLInputElement.prototype;" & ¬11 "Object.getOwnPropertyDescriptor(proto,'value').set.call(el, val);" & ¬12 "el.dispatchEvent(new Event('input',{bubbles:true})); el.dispatchEvent(new Event('change',{bubbles:true})); return 'ok';})"13 -- JSON.stringify-style quoting of the two arguments is done in JavaScript, not here.14 set call to js & "(decodeURIComponent('" & my encode(item 3 of argv) & "'), decodeURIComponent('" & my encode(item 4 of argv) & "'))"15 tell application "Safari"16 return do JavaScript call in tab t of window id w17 end tell18end run1920-- Percent-encode every byte, so no character of the value can break out of the JavaScript string.21on encode(s)22 return do shell script "/usr/bin/python3 -c 'import sys,urllib.parse; print(urllib.parse.quote(sys.argv[1], safe=\"\"))' " & quoted form of s23end encode24