-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick-prompt.user.js
More file actions
175 lines (155 loc) · 7.66 KB
/
quick-prompt.user.js
File metadata and controls
175 lines (155 loc) · 7.66 KB
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
// ==UserScript==
// @name Quick Prompt
// @namespace http://tampermonkey.net/
// @version 1.4
// @description AI 对话网站快速插入预设提示语
// @match https://chatgpt.com/*
// @match https://gemini.google.com/*
// @match https://claude.ai/*
// @match https://www.blackbox.ai/*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
// --- 1. 样式定义 (保持亮暗色支持) ---
GM_addStyle(`
:root {
--qp-bg: #ffffff; --qp-header-bg: #f7f7f8; --qp-text: #333333;
--qp-border: #e5e5e5; --qp-item-hover: #f0f0f0;
--qp-input-bg: #ffffff; --qp-input-text: #333333; --qp-accent: #10a37f;
}
@media (prefers-color-scheme: dark) {
:root {
--qp-bg: #202123; --qp-header-bg: #2d2d2e; --qp-text: #ececf1;
--qp-border: #4d4d4f; --qp-item-hover: #2a2b32;
--qp-input-bg: #343541; --qp-input-text: #ececf1;
}
}
html.dark :root, [data-theme='dark'] :root, .dark-mode :root {
--qp-bg: #202123; --qp-header-bg: #2d2d2e; --qp-text: #ececf1;
--qp-border: #4d4d4f; --qp-item-hover: #2a2b32;
--qp-input-bg: #343541; --qp-input-text: #ececf1;
}
#qp-container {
position: fixed; right: 20px; top: 100px; width: 220px;
background: var(--qp-bg); border: 1px solid var(--qp-border); border-radius: 8px;
box-shadow: 0 4px 12px rgba(0,0,0,0.2); z-index: 9999;
font-family: -apple-system, system-ui, Segoe UI, Roboto, sans-serif;
display: flex; flex-direction: column; max-height: 70vh; color: var(--qp-text);
}
.qp-header { padding: 10px; background: var(--qp-header-bg); border-bottom: 1px solid var(--qp-border); font-weight: bold; }
.qp-list { overflow-y: auto; flex-grow: 1; padding: 5px; }
.qp-item {
padding: 8px; margin: 4px 0; background: var(--qp-bg); border: 1px solid var(--qp-border);
border-radius: 4px; cursor: pointer; font-size: 13px; display: flex; justify-content: space-between;
}
.qp-item:hover { background: var(--qp-item-hover); }
.qp-shortcut-hint { color: #888; font-size: 9px; border: 1px solid var(--qp-border); padding: 0 3px; border-radius: 3px; margin-left: auto; align-self: center;}
.qp-delete { color: #ff4d4f; margin-left: 8px; cursor: pointer; opacity: 0.6; }
.qp-footer { padding: 10px; border-top: 1px solid var(--qp-border); display: flex; flex-direction: column; gap: 5px; }
.qp-input { width: 100%; padding: 5px; box-sizing: border-box; border: 1px solid var(--qp-border); border-radius: 4px; background: var(--qp-input-bg); color: var(--qp-input-text); font-size: 12px;}
.qp-add-btn { width: 100%; background: var(--qp-accent); color: white; border: none; padding: 8px; border-radius: 4px; cursor: pointer; font-weight: bold; }
`);
// --- 2. 逻辑函数 ---
const getPrompts = () => GM_getValue('quick_prompts', [
{ title: '总结内容', content: '请帮我总结以下内容的重点:' },
{ title: '翻译英文', content: '请将以下内容翻译成专业英文:' }
]);
function insertText(text) {
const selectors = ['#prompt-textarea', 'div[contenteditable="true"]', 'textarea', '.ql-editor', '[contenteditable]'];
let inputElement = null;
for (const s of selectors) {
const el = document.querySelector(s);
if (el) { inputElement = el; break; }
}
if (inputElement) {
inputElement.focus();
if (inputElement.tagName !== 'TEXTAREA' && inputElement.tagName !== 'INPUT') {
// 使用这种方式可以更好地触发现代框架(React/Vue)的监听
const selection = window.getSelection();
if (!selection.rangeCount) return;
document.execCommand('insertText', false, text);
} else {
const start = inputElement.selectionStart;
const end = inputElement.selectionEnd;
inputElement.value = inputElement.value.substring(0, start) + text + inputElement.value.substring(end);
inputElement.dispatchEvent(new Event('input', { bubbles: true }));
}
}
}
// --- 3. 快捷键监听 (针对 Windows 优化) ---
window.addEventListener('keydown', (e) => {
// 使用 Ctrl + Alt + 数字 (避免与系统切换输入法冲突)
if (e.ctrlKey && e.altKey && e.key >= '1' && e.key <= '9') {
const index = parseInt(e.key) - 1;
const prompts = getPrompts();
if (prompts[index]) {
e.preventDefault();
e.stopPropagation();
insertText(prompts[index].content);
console.log(`Quick Prompt: Triggered index ${index + 1}`);
}
}
}, true); // 使用 Capture 模式确保优先捕获
// --- 4. UI 渲染 (DOM 方式) ---
const container = document.createElement('div');
container.id = 'qp-container';
const header = document.createElement('div');
header.className = 'qp-header';
header.textContent = 'Quick Prompt 🚀';
container.appendChild(header);
const listEl = document.createElement('div');
listEl.id = 'qp-list';
listEl.className = 'qp-list';
container.appendChild(listEl);
const footer = document.createElement('div');
footer.className = 'qp-footer';
const inTitle = document.createElement('input'); inTitle.placeholder = '标题'; inTitle.className = 'qp-input';
const inContent = document.createElement('textarea'); inContent.placeholder = '内容'; inContent.className = 'qp-input'; inContent.rows = 2;
const addBtn = document.createElement('button'); addBtn.className = 'qp-add-btn'; addBtn.textContent = '添加';
addBtn.onclick = () => {
if (inTitle.value.trim() && inContent.value.trim()) {
const p = getPrompts();
p.push({ title: inTitle.value, content: inContent.value });
GM_setValue('quick_prompts', p);
renderList();
inTitle.value = ''; inContent.value = '';
}
};
footer.append(inTitle, inContent, addBtn);
container.appendChild(footer);
document.body.appendChild(container);
function renderList() {
listEl.textContent = '';
getPrompts().forEach((item, index) => {
const div = document.createElement('div');
div.className = 'qp-item';
const txt = document.createElement('span');
txt.textContent = item.title;
const right = document.createElement('div');
right.style.display = 'flex';
if (index < 9) {
const hint = document.createElement('span');
hint.className = 'qp-shortcut-hint';
hint.textContent = `C+A+${index+1}`; // 提示改为 Ctrl + Alt
right.appendChild(hint);
}
const del = document.createElement('span');
del.className = 'qp-delete';
del.textContent = '✕';
del.onclick = (e) => {
e.stopPropagation();
const p = getPrompts().filter((_, i) => i !== index);
GM_setValue('quick_prompts', p);
renderList();
};
right.appendChild(del);
div.append(txt, right);
div.onclick = () => insertText(item.content);
listEl.appendChild(div);
});
}
renderList();
})();