|
| 1 | +import { Chatbot } from '../types/chatbot' |
| 2 | +// import browser from 'webextension-polyfill' |
| 3 | +// import { |
| 4 | +// apply_chat_response_button_style, |
| 5 | +// set_button_disabled_state |
| 6 | +// } from '../utils/apply-response-styles' |
| 7 | +// import { Message } from '@/types/messages' |
| 8 | +// import { is_eligible_code_block } from '../utils/is-eligible-code-block' |
| 9 | +// import { |
| 10 | +// apply_response_button_text, |
| 11 | +// apply_response_button_title |
| 12 | +// } from '../constants/copy' |
| 13 | +// import { show_response_ready_notification } from '../utils/show-response-ready-notification' |
| 14 | + |
| 15 | +export const meta: Chatbot = { |
| 16 | + wait_until_ready: async () => { |
| 17 | + await new Promise((resolve) => { |
| 18 | + const check_for_element = () => { |
| 19 | + if (document.querySelector('i[data-visualcompletion="css-img"]')) { |
| 20 | + resolve(null) |
| 21 | + } else { |
| 22 | + setTimeout(check_for_element, 100) |
| 23 | + } |
| 24 | + } |
| 25 | + check_for_element() |
| 26 | + }) |
| 27 | + }, |
| 28 | + enter_message_and_send: async (message: string) => { |
| 29 | + const input_element = document.querySelector( |
| 30 | + 'div[contenteditable=true]' |
| 31 | + ) as HTMLElement |
| 32 | + |
| 33 | + if (!input_element) return |
| 34 | + |
| 35 | + input_element.dispatchEvent( |
| 36 | + new InputEvent('input', { |
| 37 | + bubbles: true, |
| 38 | + cancelable: true, |
| 39 | + inputType: 'insertText', |
| 40 | + data: message |
| 41 | + }) |
| 42 | + ) |
| 43 | + |
| 44 | + new Promise<void>((resolve) => { |
| 45 | + const check_button_state = () => { |
| 46 | + const path_element = document.querySelector( |
| 47 | + 'path[d="M13 7.414V19a1 1 0 1 1-2 0V7.414l-3.293 3.293a1 1 0 0 1-1.414-1.414l5-5a1 1 0 0 1 1.414 0l5 5a1 1 0 0 1-1.414 1.414L13 7.414z"]' |
| 48 | + ) |
| 49 | + const send_button = path_element?.closest( |
| 50 | + 'div[role="button"]' |
| 51 | + ) as HTMLElement |
| 52 | + |
| 53 | + if ( |
| 54 | + send_button && |
| 55 | + send_button.getAttribute('aria-disabled') != 'true' |
| 56 | + ) { |
| 57 | + send_button.click() |
| 58 | + resolve() |
| 59 | + } else { |
| 60 | + setTimeout(check_button_state, 100) |
| 61 | + } |
| 62 | + } |
| 63 | + check_button_state() |
| 64 | + }) |
| 65 | + } |
| 66 | + // Disabled for now because code blocks in copied text are not enclosed in markdown blocks (```) |
| 67 | + // inject_apply_response_button: (client_id: number) => { |
| 68 | + // const add_buttons = (params: { footer: Element }) => { |
| 69 | + // // Check if buttons already exist by text content to avoid duplicates |
| 70 | + // const existing_apply_response_button = Array.from( |
| 71 | + // params.footer.querySelectorAll('button') |
| 72 | + // ).find((btn) => btn.textContent == apply_response_button_text) |
| 73 | + |
| 74 | + // if (existing_apply_response_button) return |
| 75 | + |
| 76 | + // const chat_turn = params.footer.parentElement as HTMLElement |
| 77 | + // if (!chat_turn) return |
| 78 | + |
| 79 | + // const code_blocks = chat_turn.querySelectorAll('code') |
| 80 | + // let has_eligible_block = false |
| 81 | + // for (const code_block of Array.from(code_blocks)) { |
| 82 | + // const first_line_text = code_block?.textContent?.split('\n')[0] |
| 83 | + // if (first_line_text && is_eligible_code_block(first_line_text)) { |
| 84 | + // has_eligible_block = true |
| 85 | + // break |
| 86 | + // } |
| 87 | + // } |
| 88 | + // if (!has_eligible_block) return |
| 89 | + |
| 90 | + // const create_apply_response_button = () => { |
| 91 | + // const apply_response_button = document.createElement('button') |
| 92 | + // apply_response_button.textContent = apply_response_button_text |
| 93 | + // apply_response_button.title = apply_response_button_title |
| 94 | + // apply_chat_response_button_style(apply_response_button) |
| 95 | + |
| 96 | + // apply_response_button.addEventListener('click', async () => { |
| 97 | + // set_button_disabled_state(apply_response_button) |
| 98 | + // const copy_button = params.footer.querySelector( |
| 99 | + // 'div[role="button"]' |
| 100 | + // ) as HTMLDivElement |
| 101 | + // if (copy_button) { |
| 102 | + // copy_button.click() |
| 103 | + // } |
| 104 | + // await new Promise((resolve) => setTimeout(resolve, 500)) |
| 105 | + // browser.runtime.sendMessage<Message>({ |
| 106 | + // action: 'apply-chat-response', |
| 107 | + // client_id |
| 108 | + // }) |
| 109 | + // }) |
| 110 | + |
| 111 | + // params.footer.insertBefore( |
| 112 | + // apply_response_button, |
| 113 | + // params.footer.children[params.footer.children.length] |
| 114 | + // ) |
| 115 | + // } |
| 116 | + |
| 117 | + // create_apply_response_button() |
| 118 | + // } |
| 119 | + |
| 120 | + // const observer = new MutationObserver(() => { |
| 121 | + // const thumb_up_paths = document.querySelectorAll( |
| 122 | + // 'div[role="button"] path[d="m10.894 6.447 1.715-3.429c.56.107.939.669.794 1.247L13.04 5.72l-.988 2.964A1 1 0 0 0 13 10h6.53a.7.7 0 0 1 .436 1.246l-.526.421a1.09 1.09 0 0 0-.226 1.457.818.818 0 0 1-.19 1.108l-.624.468a1 1 0 0 0-.294 1.247l.105.211a1 1 0 0 1-.447 1.342l-.211.106a1 1 0 0 0-.502 1.21l.092.276a.69.69 0 0 1-.655.908h-4.204a4.515 4.515 0 0 1-3.192-1.323A5.727 5.727 0 0 0 5.042 17H4a1 1 0 1 0 0 2h1.042c.988 0 1.936.393 2.636 1.092A6.515 6.515 0 0 0 12.284 22h4.204a2.69 2.69 0 0 0 2.67-3.025 3.004 3.004 0 0 0 1.06-3.138l.006-.005a2.819 2.819 0 0 0 1.015-3.043A2.7 2.7 0 0 0 19.53 8h-5.142l.562-1.684a.993.993 0 0 0 .021-.073l.373-1.493A3.018 3.018 0 0 0 12.416 1c-.634 0-1.213.358-1.496.925L9.127 5.51l-1.3 2.08A3 3 0 0 1 5.283 9H4a1 1 0 0 0 0 2h1.283a5 5 0 0 0 4.24-2.35l1.325-2.12a.977.977 0 0 0 .046-.083z"]' |
| 123 | + // ) |
| 124 | + // const thumb_up_buttons: Element[] = [] |
| 125 | + // thumb_up_paths.forEach((path) => |
| 126 | + // thumb_up_buttons.push(path.closest('div[role="button"]') as HTMLElement) |
| 127 | + // ) |
| 128 | + // const all_footers: Element[] = [] |
| 129 | + // thumb_up_buttons.forEach((button) => |
| 130 | + // all_footers.push(button.closest('div:not([role="button"])') as Element) |
| 131 | + // ) |
| 132 | + |
| 133 | + // if ( |
| 134 | + // !all_footers.length || |
| 135 | + // !document.querySelector( |
| 136 | + // 'div[aria-disabled="true"] path[d="M3 6a3 3 0 0 1 3-3h12a3 3 0 0 1 3 3v12a3 3 0 0 1-3 3H6a3 3 0 0 1-3-3V6z"]' |
| 137 | + // ) |
| 138 | + // ) { |
| 139 | + // return |
| 140 | + // } |
| 141 | + |
| 142 | + // show_response_ready_notification({ chatbot_name: 'Meta' }) |
| 143 | + |
| 144 | + // all_footers.forEach((footer) => add_buttons({ footer })) |
| 145 | + // }) |
| 146 | + |
| 147 | + // observer.observe(document.body, { childList: true, subtree: true }) |
| 148 | + // } |
| 149 | +} |
0 commit comments