|
| 1 | +import { Chatbot } from '../types/chatbot' |
| 2 | +import { |
| 3 | + add_apply_response_button, |
| 4 | + observe_for_responses |
| 5 | +} from '../utils/add-apply-response-button' |
| 6 | +import { report_initialization_error } from '../utils/report-initialization-error' |
| 7 | + |
| 8 | +export const meta: Chatbot = { |
| 9 | + wait_until_ready: async () => { |
| 10 | + await new Promise((resolve) => { |
| 11 | + const check_for_element = () => { |
| 12 | + if ( |
| 13 | + document.querySelector( |
| 14 | + 'div[contenteditable="true"][data-testid="composer-input"]' |
| 15 | + ) |
| 16 | + ) { |
| 17 | + resolve(null) |
| 18 | + } else { |
| 19 | + setTimeout(check_for_element, 100) |
| 20 | + } |
| 21 | + } |
| 22 | + check_for_element() |
| 23 | + }) |
| 24 | + }, |
| 25 | + enter_message: async (params) => { |
| 26 | + const input_element = document.querySelector( |
| 27 | + 'div[contenteditable="true"][data-testid="composer-input"]' |
| 28 | + ) as HTMLElement |
| 29 | + |
| 30 | + if (!input_element) { |
| 31 | + report_initialization_error({ |
| 32 | + function_name: 'meta.enter_message', |
| 33 | + log_message: 'Message input element not found' |
| 34 | + }) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + input_element.dispatchEvent( |
| 39 | + new InputEvent('input', { |
| 40 | + bubbles: true, |
| 41 | + cancelable: true, |
| 42 | + inputType: 'insertText', |
| 43 | + data: params.message |
| 44 | + }) |
| 45 | + ) |
| 46 | + }, |
| 47 | + setup_observer: (params) => { |
| 48 | + const add_buttons = (footer: Element) => { |
| 49 | + add_apply_response_button({ |
| 50 | + client_id: params.client_id, |
| 51 | + raw_instructions: params.raw_instructions, |
| 52 | + edit_format: params.edit_format, |
| 53 | + footer, |
| 54 | + get_chat_turn: (f) => |
| 55 | + f.closest('div[data-testid="assistant-message"]') as HTMLElement, |
| 56 | + perform_copy: (f) => { |
| 57 | + const copy_button = f.querySelector( |
| 58 | + 'div:nth-child(3) > button' |
| 59 | + ) as HTMLElement |
| 60 | + if (!copy_button) { |
| 61 | + report_initialization_error({ |
| 62 | + function_name: 'meta.perform_copy', |
| 63 | + log_message: 'Copy button not found' |
| 64 | + }) |
| 65 | + return |
| 66 | + } |
| 67 | + copy_button.click() |
| 68 | + }, |
| 69 | + insert_button: (f, b) => |
| 70 | + f.insertBefore(b, f.children[f.children.length]) |
| 71 | + }) |
| 72 | + } |
| 73 | + |
| 74 | + observe_for_responses({ |
| 75 | + chatbot_name: 'Meta', |
| 76 | + is_generating: () => |
| 77 | + !!document.querySelector('button[data-testid="composer-stop-button"]'), |
| 78 | + footer_selector: |
| 79 | + 'div[data-testid="assistant-message"] > div + div > div > div:last-child > div > div', |
| 80 | + add_buttons: params.inject_button ? add_buttons : undefined |
| 81 | + }) |
| 82 | + } |
| 83 | +} |
0 commit comments