File size: 2,147 Bytes
c3674d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(() => {
  const findInput = () => {
    return (
      document.querySelector('#chat-input textarea') ||
      document.querySelector('#chat-input input') ||
      document.querySelector('textarea#chat-input') ||
      document.querySelector('input#chat-input') ||
      document.querySelector('#chat-input [contenteditable="true"]')
    );
  };

  const setValue = (el, value) => {
    if (!el) return false;

    if (el.isContentEditable) {
      el.textContent = value;
      el.dispatchEvent(new Event('input', { bubbles: true }));
      el.focus();
      return true;
    }

    const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(el), 'value');
    if (descriptor && typeof descriptor.set === 'function') {
      descriptor.set.call(el, value);
    } else {
      el.value = value;
    }

    el.dispatchEvent(new Event('input', { bubbles: true }));
    el.focus();
    return true;
  };

  const setDisabled = (disabled) => {
    const el = findInput();
    if (!el) return false;
    if (el.isContentEditable) {
      el.setAttribute('contenteditable', disabled ? 'false' : 'true');
    } else {
      el.disabled = !!disabled;
    }
    return true;
  };

  const trySetInput = (value) => {
    const el = findInput();
    return setValue(el, value);
  };

  window.addEventListener('message', (event) => {
    const data = event?.data;
    if (!data) return;

    if (data.type === 'set_chat_input') {
      const value = typeof data.value === 'string' ? data.value : '';

      if (!trySetInput(value)) {
        let attempts = 0;
        const interval = setInterval(() => {
          attempts += 1;
          if (trySetInput(value) || attempts >= 10) {
            clearInterval(interval);
          }
        }, 150);
      }
      return;
    }

    if (data.type === 'set_input_disabled') {
      const disabled = !!data.value;
      if (!setDisabled(disabled)) {
        let attempts = 0;
        const interval = setInterval(() => {
          attempts += 1;
          if (setDisabled(disabled) || attempts >= 10) {
            clearInterval(interval);
          }
        }, 150);
      }
    }
  });
})();