-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
121 lines (109 loc) · 4.42 KB
/
content.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
function getActiveElementType(activeElement) {
if (
(activeElement.tagName.toLowerCase() === 'input' &&
activeElement.type.toLowerCase() === 'text') ||
activeElement.tagName.toLowerCase() === 'textarea'
) {
return 'inputOrTextarea';
} else if (activeElement.getAttribute('contenteditable') === 'true') {
return 'contentEditable';
} else {
return 'none';
}
}
function sendMessage(prompt) {
console.log('querying chatgpt: ' + prompt);
chrome.runtime.sendMessage({ mode: 'chat_gpt', message: prompt }, (response) => {
if (response.mode === 'chat_gpt') {
const activeElement = document.activeElement;
const responseWithBreak = response.message + '\n';
console.log('chatgpt response: ' + response.message);
const activeElementType = getActiveElementType(activeElement);
if (activeElementType === 'inputOrTextarea') {
activeElement.value = activeElement.value.replace('loading...', responseWithBreak);
} else if (activeElementType === 'contentEditable') {
activeElement.innerHTML = activeElement.innerHTML.replace('loading...', responseWithBreak);
} else {
console.log('No active text field, textarea, or content-editable element found.');
}
}
});
}
function doKeyPress(e) {
if (e.keyCode === 13) {
const activeElement = document.activeElement;
let text = '';
const activeElementType = getActiveElementType(activeElement);
if (activeElementType === 'inputOrTextarea') {
text = activeElement.value;
} else if (activeElementType === 'contentEditable') {
text = activeElement.innerText;
}
const aiCommandPattern = /(\/ai:)([\s\S]*?)(;|(?=\n|$))/;
const aiMatch = text.match(aiCommandPattern);
if (aiMatch) {
console.log('ai keyword detected!')
e.preventDefault();
const aiCommand = aiMatch[0];
const prompt = aiMatch[2].trim().split(';')[0];
if (activeElementType === 'inputOrTextarea') {
activeElement.value = activeElement.value.replace(aiCommand, 'loading...');
sendMessage(prompt);
} else if (activeElementType === 'contentEditable') {
activeElement.innerHTML = activeElement.innerHTML.replace(aiCommand, 'loading...');
sendMessage(prompt);
} else {
console.log('no text field / editable content detected')
}
}
}
}
document.addEventListener('keydown', function (event) {
if (event.keyCode === 13) {
const activeElement = document.activeElement;
let text = '';
const activeElementType = getActiveElementType(activeElement);
if (activeElementType === 'inputOrTextarea') {
text = activeElement.value;
} else if (activeElementType === 'contentEditable') {
text = activeElement.innerText;
}
if (text.startsWith('/ai:')) {
event.preventDefault();
}
}
});
if (window == top) {
window.addEventListener('keyup', doKeyPress, false);
}
chrome.runtime.onMessage.addListener((message, sender) => {
console.log(message)
if (message.mode === 'context') {
const activeElement = document.activeElement;
const responseWithBreak = message.message + '\n';
console.log('chatgpt response: ' + message.message);
const activeElementType = getActiveElementType(activeElement);
if (activeElementType === 'inputOrTextarea') {
// Check if there is selected text
if (activeElement.selectionStart !== activeElement.selectionEnd) {
const selectedText = activeElement.value.substring(activeElement.selectionStart, activeElement.selectionEnd);
activeElement.value = activeElement.value.substring(0, activeElement.selectionStart) + responseWithBreak + activeElement.value.substring(activeElement.selectionEnd);
} else {
activeElement.value = responseWithBreak;
}
} else if (activeElementType === 'contentEditable') {
// Check if there is selected text
const selection = window.getSelection();
if (selection.toString() !== "") {
const range = selection.getRangeAt(0);
range.deleteContents();
const replacedText = document.createTextNode(responseWithBreak);
range.insertNode(replacedText);
} else {
activeElement.innerHTML = responseWithBreak;
}
} else {
console.log('No active text field, textarea, or content-editable element found.');
}
}
});