-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
317 lines (284 loc) · 14.4 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
const chatToolDivId = 'chatToolDiv';
const stateIdentifier = 'chatToolState';
// 接收来自弹出页面的消息
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.action === "clearCache") {
// 执行清除本地存储的操作
localStorage.removeItem(stateIdentifier);
console.log('本地存储已清除');
}
});
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
const url = window.location.href.toLowerCase();
let selectedOptionId = '';
const ChatGPT = "ChatGPT"
const Gemini = "Gemini"
const Tongyi = "Tongyi"
if (url.includes('chatgpt')) {
selectedOptionId = ChatGPT;
} else if (url.includes('gemini')) {
selectedOptionId = Gemini;
} else if (url.includes('tongyi')) {
selectedOptionId = Tongyi;
}
if (message.action === "insertDiv") {
var div_id = document.getElementById(chatToolDivId);
if (!div_id) {
var popup = document.createElement('div');
popup.id = chatToolDivId;
popup.style.position = 'absolute';
popup.style.top = '0';
popup.style.right = '0';
popup.style.width = '300px';
popup.style.background = '#fff';
popup.style.border = '1px solid #ccc';
popup.style.padding = '20px';
popup.style.zIndex = '9999';
var style = document.createElement('style');
style.innerHTML = `
body{
font-size: 14px;
}
.scrollable-container {
max-height: 400px;
overflow-y: auto;
padding: 15px;
border: 1px solid #ccc;
border-radius: 5px;
}
.select {
margin-top: 10px;
margin-bottom: 10px;
}
.cus_textarea{
width: 100%;
height: 150px;
margin-top: 10px;
}
`;
document.head.appendChild(style);
popup.innerHTML = `
<h3>Options:</h3>
<div class="scrollable-container">
<div class="select">
<label class="exclude" for="context">情景</label>
<textarea id="context" class="cus_textarea" placeholder="我正在使用C++开发一个多平台桌面应用\n\n当前的开发环境及版本、使用工具:">我正在使用C++开发一个多平台桌面应用\n\n当前的开发环境及版本、使用工具:</textarea>
</div>
<div class="select">
<label class="exclude" for="objective">目标</label>
<textarea id="objective" class="cus_textarea"
placeholder="修复错误、写出代码,使其能够正常运行">修复错误、写出代码,使其能够正常运行</textarea>
</div>
<div class="select">
<label class="exclude" for="style">风格</label>
<textarea id="style" class="cus_textarea"
placeholder="">- 简洁易懂,符合 C++ 编程规范。\n- 代码结构清晰,易于理解和维护。\n- 注释完整,解释清楚函数和变量的用途。\n- 如果改动较小,则只写出关键修改的代码</textarea>
</div>
<div class="select">
<label class="exclude" for="tone">语调</label>
<textarea id="tone" class="cus_textarea"
placeholder="请以一种积极和乐于助人的语气回应我的请求"></textarea>
</div>
<div class="select">
<label class="exclude" for="audience">受众</label>
<textarea id="audience" class="cus_textarea"
placeholder="面向有C++基础编程经验的开发人员">面向有C++基础编程经验的开发人员</textarea>
</div>
<div class="select">
<label class="exclude" for="format">格式</label>
<textarea id="format" class="cus_textarea"
placeholder="使用中文回答所有问题。\n提供完整代码,无需过多描述。\n代码示例格式化排版,便于阅读。">使用中文回答所有问题。\n提供完整代码,无需过多描述。\n代码示例格式化排版,便于阅读。</textarea>
</div>
</div>
<div style="text-align: center; margin-top: 20px;">
<button id="close" style="font-size: 16px; padding: 10px 20px;">关闭</button>
<button id="chatItButton" style="font-size: 16px; padding: 10px 20px;">Chat it~</button>
</div>
`;
// <hr style="margin-top: 20px; margin-bottom: 20px;"> <!-- 添加分割线 --></hr>
// 将弹出框添加到body中
document.body.appendChild(popup);
// 获取关闭按钮
var closeButton = document.getElementById('close');
// 添加关闭按钮的点击事件
closeButton.addEventListener('click', function () {
var customPopup = document.getElementById(chatToolDivId);
if (customPopup) {
customPopup.style.display = 'none';
saveStateToLocalStorage();
}
});
document.getElementById(chatToolDivId).querySelectorAll('.select:not(:last-child) label:not(.exclude)').forEach(label => {
label.addEventListener('click', () => {
const checkbox = label.previousElementSibling;
if (checkbox && checkbox.type === 'checkbox') {
checkbox.checked = !checkbox.checked;
}
});
});
// 添加 Chat it~ 按钮的点击事件
var chatItButton = document.getElementById('chatItButton');
chatItButton.addEventListener('click', function () {
// 获取选中的选项
// var options = document.querySelectorAll('.select input[type="checkbox"]:checked');
// var selectedOptions = Array.from(options).map(option => option.nextElementSibling.textContent);
// 获取 context 文本
var context = document.getElementById('context').value;
var objective = document.getElementById('objective').value;
var style = document.getElementById('style').value;
var tone = document.getElementById('tone').value;
var audience = document.getElementById('audience').value;
var format = document.getElementById('format').value;
// var bugCodeText = document.getElementById('bugCode').value;
// 拼凑文本
var summary = "";
if (context.trim() !== "") {
summary += "此次问题的情景:" + context + "\n\n";
}
if (objective.trim() !== "") {
summary += "此次问题的目标:" + objective + "\n\n";
}
if (style.trim() !== "") {
summary += "此次问题的风格:" + style + "\n\n";
}
if (tone.trim() !== "") {
summary += "此次问题的语调:" + tone + "\n\n";
}
if (audience.trim() !== "") {
summary += "此次问题的受众:" + audience + "\n\n";
}
if (format.trim() !== "") {
summary += "此次问题的格式:" + format + "\n\n";
}
// 获取单选项的值
// var selectedRadio = document.querySelector('.select input[type="radio"]:checked').value;
// 插入到某个 div 中
var targetDiv;
switch (selectedOptionId) {
case ChatGPT:
targetDiv = document.getElementById('prompt-textarea');
if (targetDiv) {
targetDiv.value = ''; // 清空
targetDiv.value = summary;
var event = new Event('input', {
bubbles: true,
cancelable: true,
});
targetDiv.dispatchEvent(event);
setTimeout(function () {
var button = document.querySelector('button[data-testid="send-button"]');
if (button) {
setTimeout(function () {
button.click();
console.log('已经触发按钮点击事件');
}, 500);
} else {
console.log('没有找到对应的按钮');
}
}, 200);
console.log('已更新textarea并触发了input事件。');
}
break;
case Gemini:
var textarea = document.querySelector('.ql-editor.ql-blank.textarea');
if (textarea) {
var pElement = document.querySelector('.ql-editor p');
if (pElement) {
pElement.innerHTML = summary;
var event = new Event('input', {
bubbles: true,
cancelable: true,
});
pElement.dispatchEvent(event);
setTimeout(function () {
var button = document.querySelector('button[aria-label="Send message"]');
if (button) {
setTimeout(function () {
button.click();
console.log('已经触发按钮点击事件');
}, 500);
} else {
console.log('没有找到对应的按钮');
}
}, 200);
}
} else {
console.log('Gemini 没有找到textarea');
}
break;
case Tongyi:
var textareas = document.getElementsByTagName('textarea');
var found = false;
for (var i = 0; i < textareas.length; i++) {
if (textareas[i].placeholder.includes('在这里输入问题,直接输入')) {
found = true;
textareas[i].innerHTML = summary;
var event = new Event('input', {
bubbles: true,
cancelable: true
});
textareas[i].dispatchEvent(event);
setTimeout(function () {
var operateButtons = document.querySelectorAll('div[class^="operateBtn-"]');
if (operateButtons.length > 0) {
operateButtons[0].click();
console.log('已触发操作按钮的点击事件。');
} else {
console.log('没有找到以operateBtn开头的操作按钮。');
}
}, 200);
console.log('已更新textarea并触发了input事件。');
break;
}
}
if (!found) {
console.log('没有找到具有特定placeholder的textarea');
}
break;
}
saveStateToLocalStorage();
});
} else {
div_id.style.display = 'block';
}
var savedState = localStorage.getItem(stateIdentifier);
if (savedState) {
var state = JSON.parse(savedState);
console.log("读取到了数据", state)
document.getElementById('context').value = state.context;
document.getElementById('objective').value = state.objective;
document.getElementById('style').value = state.style;
document.getElementById('tone').value = state.tone;
document.getElementById('audience').value = state.audience;
document.getElementById('format').value = state.format;
// Restore radio button state
}
if (selectedOptionId) {
const radio = document.getElementById(selectedOptionId);
if (radio) {
radio.checked = true;
}
}
// 发送消息给 popup.js,表示插入成功
chrome.runtime.sendMessage({
action: "insertDivSuccess"
});
}
});
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.action === "tabClosed") {
console.log('tabClosed');
saveStateToLocalStorage();
}
});
function saveStateToLocalStorage() {
var state = {
context: document.getElementById('context').value,
objective: document.getElementById('objective').value,
style: document.getElementById('style').value,
tone: document.getElementById('tone').value,
audience: document.getElementById('audience').value,
format: document.getElementById('format').value,
};
localStorage.setItem(stateIdentifier, JSON.stringify(state));
}