|
| 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 kimi: Chatbot = { |
| 16 | + wait_until_ready: async () => { |
| 17 | + await new Promise((resolve) => { |
| 18 | + const check_for_element = () => { |
| 19 | + if (document.querySelector('.home-case-list')) { |
| 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 | + '.chat-input-editor[data-lexical-editor="true"]' |
| 31 | + ) as HTMLElement |
| 32 | + |
| 33 | + if (!input_element) { |
| 34 | + throw new Error('Kimi input element not found') |
| 35 | + } |
| 36 | + |
| 37 | + // Focus the element first |
| 38 | + input_element.focus() |
| 39 | + |
| 40 | + // Clear existing content by selecting all and deleting |
| 41 | + document.execCommand('selectAll', false) |
| 42 | + document.execCommand('delete', false) |
| 43 | + |
| 44 | + // Insert the new text using execCommand which works better with Lexical |
| 45 | + document.execCommand('insertText', false, message) |
| 46 | + |
| 47 | + // Dispatch input event to trigger Lexical's change detection |
| 48 | + input_element.dispatchEvent(new Event('input', { bubbles: true })) |
| 49 | + input_element.dispatchEvent(new Event('change', { bubbles: true })) |
| 50 | + |
| 51 | + new Promise<void>((resolve) => { |
| 52 | + const check_button_state = () => { |
| 53 | + const send_button_container = document.querySelector( |
| 54 | + '.send-button-container' |
| 55 | + ) |
| 56 | + const send_button = document.querySelector( |
| 57 | + 'div.send-button' |
| 58 | + ) as HTMLElement |
| 59 | + |
| 60 | + if ( |
| 61 | + send_button && |
| 62 | + !send_button_container?.classList.contains('disabled') |
| 63 | + ) { |
| 64 | + send_button.click() |
| 65 | + resolve() |
| 66 | + } else { |
| 67 | + setTimeout(check_button_state, 100) |
| 68 | + } |
| 69 | + } |
| 70 | + check_button_state() |
| 71 | + }) |
| 72 | + }, |
| 73 | + inject_apply_response_button: (client_id: number) => { |
| 74 | + const add_buttons = (params: { footer: Element }) => { |
| 75 | + // Check if buttons already exist by text content to avoid duplicates |
| 76 | + const existing_apply_response_button = Array.from( |
| 77 | + params.footer.querySelectorAll('button') |
| 78 | + ).find((btn) => btn.textContent == apply_response_button_text) |
| 79 | + |
| 80 | + if (existing_apply_response_button) return |
| 81 | + |
| 82 | + const chat_turn = params.footer.closest('.segment-content') as HTMLElement |
| 83 | + const code_blocks = chat_turn.querySelectorAll('code') |
| 84 | + let has_eligible_block = false |
| 85 | + for (const code_block of Array.from(code_blocks)) { |
| 86 | + const first_line_text = code_block?.textContent?.split('\n')[0] |
| 87 | + if (first_line_text && is_eligible_code_block(first_line_text)) { |
| 88 | + has_eligible_block = true |
| 89 | + break |
| 90 | + } |
| 91 | + } |
| 92 | + if (!has_eligible_block) return |
| 93 | + |
| 94 | + const create_apply_response_button = () => { |
| 95 | + const apply_response_button = document.createElement('button') |
| 96 | + apply_response_button.textContent = apply_response_button_text |
| 97 | + apply_response_button.title = apply_response_button_title |
| 98 | + apply_chat_response_button_style(apply_response_button) |
| 99 | + |
| 100 | + apply_response_button.addEventListener('click', async () => { |
| 101 | + set_button_disabled_state(apply_response_button) |
| 102 | + const copy_button = document.querySelector( |
| 103 | + '.segment-assistant-actions-content > div:first-child' |
| 104 | + ) as HTMLElement |
| 105 | + copy_button.click() |
| 106 | + await new Promise((resolve) => setTimeout(resolve, 500)) |
| 107 | + browser.runtime.sendMessage<Message>({ |
| 108 | + action: 'apply-chat-response', |
| 109 | + client_id |
| 110 | + }) |
| 111 | + }) |
| 112 | + |
| 113 | + params.footer.insertBefore( |
| 114 | + apply_response_button, |
| 115 | + params.footer.children[6] |
| 116 | + ) |
| 117 | + |
| 118 | + apply_response_button.focus() |
| 119 | + } |
| 120 | + |
| 121 | + create_apply_response_button() |
| 122 | + } |
| 123 | + |
| 124 | + const observer = new MutationObserver((mutations) => { |
| 125 | + mutations.forEach(() => { |
| 126 | + if ( |
| 127 | + document.querySelector( |
| 128 | + 'path[d="M331.946667 379.904c-11.946667 23.466667-11.946667 54.186667-11.946667 115.626667v32.938666c0 61.44 0 92.16 11.946667 115.626667 10.538667 20.650667 27.306667 37.418667 47.957333 47.957333 23.466667 11.946667 54.186667 11.946667 115.626667 11.946667h32.938666c61.44 0 92.16 0 115.626667-11.946667 20.650667-10.538667 37.418667-27.306667 47.957333-47.957333 11.946667-23.466667 11.946667-54.186667 11.946667-115.626667v-32.938666c0-61.44 0-92.16-11.946667-115.626667a109.696 109.696 0 0 0-47.957333-47.957333c-23.466667-11.946667-54.186667-11.946667-115.626667-11.946667h-32.938666c-61.44 0-92.16 0-115.626667 11.946667-20.650667 10.538667-37.418667 27.306667-47.957333 47.957333z"]' |
| 129 | + ) |
| 130 | + ) { |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + show_response_ready_notification({ chatbot_name: 'Kimi' }) |
| 135 | + |
| 136 | + const all_footers = document.querySelectorAll( |
| 137 | + '.segment-assistant-actions-content' |
| 138 | + ) |
| 139 | + all_footers.forEach((footer) => { |
| 140 | + add_buttons({ |
| 141 | + footer |
| 142 | + }) |
| 143 | + }) |
| 144 | + }) |
| 145 | + }) |
| 146 | + |
| 147 | + observer.observe(document.body, { |
| 148 | + childList: true, |
| 149 | + subtree: true, |
| 150 | + characterData: true |
| 151 | + }) |
| 152 | + } |
| 153 | +} |
0 commit comments