send.js
1 import { cli, Strategy } from '@jackwener/opencli/registry'; 2 import { SelectorError } from '@jackwener/opencli/errors'; 3 export const sendCommand = cli({ 4 site: 'chatwise', 5 name: 'send', 6 description: 'Send a message to the active ChatWise conversation', 7 domain: 'localhost', 8 strategy: Strategy.UI, 9 browser: true, 10 args: [{ name: 'text', required: true, positional: true, help: 'Message to send' }], 11 columns: ['Status', 'InjectedText'], 12 func: async (page, kwargs) => { 13 const text = kwargs.text; 14 const injected = await page.evaluate(` 15 (function(text) { 16 // ChatWise input can be textarea or contenteditable 17 let composer = document.querySelector('textarea'); 18 if (!composer) { 19 const editables = Array.from(document.querySelectorAll('[contenteditable="true"]')); 20 composer = editables.length > 0 ? editables[editables.length - 1] : null; 21 } 22 23 if (!composer) return false; 24 25 composer.focus(); 26 27 if (composer.tagName === 'TEXTAREA') { 28 // For textarea, set value and dispatch input event 29 const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, 'value').set; 30 nativeInputValueSetter.call(composer, text); 31 composer.dispatchEvent(new Event('input', { bubbles: true })); 32 } else { 33 document.execCommand('insertText', false, text); 34 } 35 return true; 36 })(${JSON.stringify(text)}) 37 `); 38 if (!injected) 39 throw new SelectorError('ChatWise input element'); 40 await page.wait(0.5); 41 await page.pressKey('Enter'); 42 return [ 43 { 44 Status: 'Success', 45 InjectedText: text, 46 }, 47 ]; 48 }, 49 });