-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathprompthelper.js
306 lines (268 loc) · 11.5 KB
/
prompthelper.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
// ==UserScript==
// @name ChatGPT 框架助手
// @namespace http://tampermonkey.net/
// @version 1.0.202402232156
// @description 在ChatGPT页面侧边显示框架助手
// @author iaiuse.com
// @match https://chat.openai.com/*
// @match https://claude.ai/chat/*
// @match https://aistudio.google.com/*
// @match https://chatglm.cn/main/*
// @match https://kimi.moonshot.cn/*
// @match https://gemini.google.com/*
// @icon https://www.iaiuse.com/img/avatar.png
// @grant GM_xmlhttpRequest
// @grant GM_addStyle
// @grant GM_getResourceText
// @require https://code.jquery.com/jquery-3.7.1.min.js
// ==/UserScript==
(function() {
'use strict';
// 动态添加Tailwind CSS
const tailwindLink = document.createElement('link');
tailwindLink.rel = 'stylesheet';
tailwindLink.href = 'https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css';
document.head.appendChild(tailwindLink);
// 动态添加Font Awesome
const faLink = document.createElement('link');
faLink.rel = 'stylesheet';
faLink.href = 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css';
document.head.appendChild(faLink);
//const tailwind_css = GM_getResourceText('https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css');
//GM_addStyle(tailwind_css);
// 添加侧边栏样式
GM_addStyle(`
#frameHelper {
position: fixed;
right: 0;
top: 50%;
transform: translateY(-50%);
background-color: #f9f9f9;
width: 50px; /* Initial width */
height: auto;
transition: width 0.5s;
overflow: hidden;
z-index: 1000;
padding: 10px;
box-shadow: -2px 0 5px rgba(0,0,0,0.2);
border-radius: 5px 0 0 5px;
cursor: pointer;
}
#frameHelper.expanded {
width: 600px; /* Expanded width */
}
.frame-content, .dynamic-content {
display: none;
}
#frameHelper.expanded .frame-content,
#frameHelper.expanded .dynamic-content {
display: block;
}
.top-container {
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
}
.select-container, .icon-container {
padding: 5px;
}
.icon-container i {
cursor: pointer;
}
.arrow{
position: relative;
display: inline-block;
padding-left: 20px;
}
.arrow::before{
content: '';
width: 6px;
height: 6px;
border: 0px;
border-top: solid 2px #5bc0de;
border-right: solid 2px #5bc0de;
-ms-transform: rotate(45deg);
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
position: absolute;
top: 50%;
left: 0;
margin-top: -4px;
}
`);
// 创建侧边栏元素
const frameHelper = document.createElement('div');
frameHelper.id = 'frameHelper';
frameHelper.className = 'bg-gray-100 text-gray-800 p-1 rounded-lg shadow transition-all ease-in-out duration-500 cursor-pointer hover:bg-gray-200';
document.body.appendChild(frameHelper);
// 创建顶部容器,用于放置select和图标
const topContainer = document.createElement('div');
topContainer.className = 'top-container flex justify-between items-center mb-1'; //'frame-content flex justify-between items-center mb-4 p-2 bg-white rounded-lg shadow';
frameHelper.appendChild(topContainer);
// 创建并配置select容器
const selectContainer = document.createElement('div');
selectContainer.className = 'select-container';
// 创建select元素
const selectElement = document.createElement('select');
selectElement.className = 'flex-grow';
// 添加选项到select...
selectElement.addEventListener('change', function() {
if('-请选择-' == this.value )
return false;
loadFrameworkData(this.value);
});
selectContainer.appendChild(selectElement);
// 创建并配置图标容器
const iconContainer = document.createElement('div');
iconContainer.className = 'icon-container flex-shrink-0 ml-1 arrow';
iconContainer.addEventListener('click', function() {
// 切换侧边栏大小的逻辑
const frameHelper = document.getElementById('frameHelper');
frameHelper.classList.toggle('expanded');
});
// 创建缩放图标
// 使用Font Awesome图标
const shrinkIcon = document.createElement('i');
shrinkIcon.className = 'fas fa-angle-double-right text-gray-600 hover:text-gray-800 cursor-pointer';
shrinkIcon.style.fontSize = '1.25rem';
shrinkIcon.style.fontSize = '20px'; // 调整图标大小
iconContainer.appendChild(shrinkIcon);
// START FRAMEWORKS
// END FRAMEWORKS
// 组装顶部容器
topContainer.appendChild(selectContainer);
topContainer.appendChild(iconContainer);
// 动态内容区域
const dynamicContentDiv = document.createElement('div');
dynamicContentDiv.className = 'dynamic-content p-1';
frameHelper.appendChild(dynamicContentDiv);
// 加载框架数据
function loadFrameworkData(frameworkName) {
// 假设frameworks变量已经定义并包含所有框架数据
const data = frameworks.find(framework => Object.keys(framework)[0] === frameworkName);
if (data) {
const frameworkData = data[frameworkName];
updateDynamicContent(frameworkData);
} else {
console.error('Framework data not found for:', frameworkName);
}
}
// 更新动态内容区域
function updateDynamicContent(frameworkData) {
dynamicContentDiv.innerHTML = ''; // 清除现有内容
// 根据框架数据创建新的UI元素(例如,输入框和标签)
const description = document.createElement('p');
description.textContent = frameworkData.description;
dynamicContentDiv.appendChild(description);
// 遍历fields创建输入框和标签
frameworkData.fields.forEach(fieldObj => {
// 直接使用Object.entries获取键值对
Object.entries(fieldObj).forEach(([fieldName, field]) => {
const fieldLabel = document.createElement('label');
fieldLabel.className = 'block text-gray-700 text-sm font-bold mb-1';
fieldLabel.textContent = fieldName + ": ";
if (field.info) {
// 添加info文本,用灰色文字表示
const infoText = document.createElement('span');
infoText.textContent = ` (${field.info})`;
infoText.style.color = 'grey';
fieldLabel.appendChild(infoText);
}
const textarea = document.createElement('textarea');
textarea.className = 'w-full p-1 border border-gray-300 rounded-md'; // Tailwind类
// 处理text,可能是数组也可能是字符串
let textContent = '';
if (Array.isArray(field.text)) {
textContent = field.text.map(textItem => {
if (typeof textItem === 'string') {
return textItem;
} else if (typeof textItem === 'object') {
return Object.values(textItem).join('\n');
}
}).join('\n');
} else {
textContent = field.text;
}
textarea.setAttribute('placeholder', textContent);
textarea.setAttribute('name', fieldName);
dynamicContentDiv.appendChild(fieldLabel);
//dynamicContentDiv.appendChild(document.createElement('br'));
dynamicContentDiv.appendChild(textarea);
dynamicContentDiv.appendChild(document.createElement('br'));
});
});
// 提交按钮
const submitButton = document.createElement('button');
submitButton.textContent = '发送';
submitButton.className = 'bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded rounded-md';
submitButton.onclick = () => submitForm(frameworkData.fields);
dynamicContentDiv.appendChild(submitButton);
// 提交按钮
const copyButton = document.createElement('button');
copyButton.textContent = '复制';
copyButton.className = 'bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded';
copyButton.onclick = () => copyForm(frameworkData.fields);
dynamicContentDiv.appendChild(copyButton);
}
// 初始化下拉框选项
function initSelectOptions() {
// 添加默认选项
const defaultOption = document.createElement('option');
defaultOption.value = '';
defaultOption.textContent = '-请选择-';
selectElement.appendChild(defaultOption);
// 遍历frameworks数组创建选项
frameworks.forEach(framework => {
// 获取框架的键名和值
const key = Object.keys(framework)[0]; // 框架的键名(如"BROKE")
const value = framework[key]; // 框架的值,包含name, author等
// 创建选项
const option = document.createElement('option');
option.value = key; // 选项的value是框架的键名
option.textContent = `${value.name}-${value.description}`; // 选项的文本是框架的名称和描述
selectElement.appendChild(option);
});
}
// 获取内容
function getFormValue(fields) {
let prompt = "";
fields.forEach(fieldObj => {
// 直接使用Object.entries获取键值对
Object.entries(fieldObj).forEach(([fieldName, field]) => {
const input = document.querySelector(`textarea[name="${fieldName}"]`);
prompt += `${fieldName}:\n${input.value}\n\n`;
})});
return prompt;
}
// 提交表单的逻辑
function submitForm(fields) {
let prompt = getFormValue(fields);
$('#prompt-textarea').val(prompt);
// 这里应该将prompt发送给ChatGPT的输入框
console.log(prompt); // 示例:打印到控制台
}
// 提交表单的逻辑
function copyForm(fields) {
let prompt = getFormValue(fields);
// 使用navigator.clipboard API复制文本到剪贴板
navigator.clipboard.writeText(prompt).then(() => {
// 复制成功后的操作,比如显示一个提示
console.log('文本已复制到剪贴板');
}).catch(err => {
// 复制失败的操作
console.error('复制到剪贴板失败:', err);
});
// 这里应该将prompt发送给ChatGPT的输入框
console.log(prompt); // 示例:打印到控制台
}
initSelectOptions(); // 调用函数来填充下拉框
// 鼠标悬停和移开事件
frameHelper.addEventListener('mouseenter', function() {
this.classList.add('expanded');
});
frameHelper.addEventListener('mouseleave', function() {
//this.classList.remove('expanded');
//this.innerText = '框架助手';
});
})();