-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgemini-toc.user.js
More file actions
334 lines (297 loc) · 10.1 KB
/
gemini-toc.user.js
File metadata and controls
334 lines (297 loc) · 10.1 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// ==UserScript==
// @name Gemini TOC (Table of Contents)
// @namespace http://tampermonkey.net/
// @version 1.2
// @description Add a floating TOC for Gemini conversations
// @author You
// @match https://gemini.google.com/*
// @grant none
// ==/UserScript==
(function () {
"use strict";
// 创建TOC容器
function createTOC() {
const toc = document.createElement("div");
toc.id = "gemini-toc";
toc.style.cssText = `
position: fixed;
top: 50%;
transform: translateY(-50%);
right: 2px;
width: 8px;
max-height: 70vh;
background: rgba(255, 255, 255, 0.6);
border: 1px solid #ddd;
border-radius: 6px;
padding: 8px;
z-index: 10000;
opacity: 0.3;
transition: opacity 0.1s ease;
overflow-y: auto;
overflow-x: hidden;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
`;
// 添加鼠标悬停效果
toc.addEventListener("mouseenter", () => {
toc.style.width = "250px";
toc.style.opacity = "1.0";
// 显示所有文本
const items = toc.querySelectorAll(".toc-item-text");
items.forEach((item) => {
item.style.opacity = "1";
});
});
toc.addEventListener("mouseleave", () => {
toc.style.width = "8px";
toc.style.opacity = "0.3";
// 隐藏所有文本
const items = toc.querySelectorAll(".toc-item-text");
items.forEach((item) => {
item.style.opacity = "0";
});
});
document.body.appendChild(toc);
return toc;
}
// 截取文本并添加省略号
function truncateText(text, maxLength = 30) {
if (text.length <= maxLength) {
return text;
}
return text.substring(0, maxLength) + "...";
}
// 查找用户提问元素
function findUserPrompts() {
const prompts = [];
// 首先尝试查找带有 class="query-text-line" 的 p 标签
let queryTextElements = document.querySelectorAll(
"p.query-text-line.ng-star-inserted"
);
// if (queryTextElements.length === 0) {
// queryTextElements = document.querySelectorAll('p[class*="query-text"]');
// }
if (queryTextElements.length > 0) {
const processedElements = new Set();
queryTextElements.forEach((element) => {
// 如果这个元素已经被处理过,跳过
if (processedElements.has(element)) {
return;
}
const text = element.textContent.trim();
if (text && text.length > 0) {
// 查找连续的兄弟节点并合并文本
const mergedText = [];
const elementsGroup = [element];
let currentElement = element;
// 添加当前元素的文本
mergedText.push(text);
processedElements.add(element);
// 向后查找连续的兄弟节点
let nextSibling = currentElement.nextElementSibling;
while (
nextSibling &&
nextSibling.matches("p.query-text-line.ng-star-inserted")
) {
const siblingText = nextSibling.textContent.trim();
if (siblingText && siblingText.length > 0) {
mergedText.push(siblingText);
elementsGroup.push(nextSibling);
processedElements.add(nextSibling);
}
nextSibling = nextSibling.nextElementSibling;
}
// 向前查找连续的兄弟节点(以防顺序不同)
let prevSibling = currentElement.previousElementSibling;
while (
prevSibling &&
prevSibling.matches("p.query-text-line.ng-star-inserted") &&
!processedElements.has(prevSibling)
) {
const siblingText = prevSibling.textContent.trim();
if (siblingText && siblingText.length > 0) {
mergedText.unshift(siblingText);
elementsGroup.unshift(prevSibling);
processedElements.add(prevSibling);
}
prevSibling = prevSibling.previousElementSibling;
}
// 将合并的文本和第一个元素添加到prompts中
prompts.push({
element: elementsGroup[0], // 使用第一个元素作为跳转目标
text: mergedText.join(" "), // 用空格连接所有文本
elementsGroup: elementsGroup, // 保存所有相关元素,以备将来使用
});
}
});
return prompts;
}
// // 尝试多种选择器来找到用户输入
// const selectors = [
// '[data-message-author-role="user"]',
// ".user-message",
// '[role="user"]',
// ".message.user",
// 'div[data-test-id*="user"]',
// 'div[data-test-id*="prompt"]',
// ".prompt-content",
// ".user-input",
// '[class*="user"][class*="message"]',
// ];
// for (const selector of selectors) {
// const elements = document.querySelectorAll(selector);
// if (elements.length > 0) {
// elements.forEach((element) => {
// const text = element.textContent.trim();
// if (text && text.length > 0) {
// prompts.push({
// element: element,
// text: text,
// });
// }
// });
// break;
// }
// }
// // 如果上述选择器都没找到,尝试通过文本内容和位置来识别
// if (prompts.length === 0) {
// // 尝试查找所有可能包含用户输入的元素
// const allElements = document.querySelectorAll("p, div, span");
// allElements.forEach((element) => {
// const text = element.textContent.trim();
// // 简单启发式:查找可能是用户输入的元素
// if (
// text &&
// text.length > 10 &&
// text.length < 1000 &&
// !text.includes("Gemini") &&
// !text.includes("Google") &&
// !text.includes("AI") &&
// element.children.length === 0
// ) {
// // 检查是否在对话容器中或者有相关的类名
// const parent = element.closest(
// '[role="main"], .conversation, .chat, main'
// );
// const hasUserClass =
// element.className &&
// (element.className.includes("user") ||
// element.className.includes("prompt") ||
// element.className.includes("query"));
// if (parent || hasUserClass) {
// prompts.push({
// element: element,
// text: text,
// });
// }
// }
// });
// }
return prompts;
}
// 更新TOC内容
function updateTOC(tocContainer) {
const prompts = findUserPrompts();
// 清除现有内容
// 使用安全的DOM操作替代innerHTML
while (tocContainer.firstChild) {
tocContainer.removeChild(tocContainer.firstChild);
}
if (prompts.length === 0) {
const noContent = document.createElement("div");
noContent.textContent = "暂无对话内容";
noContent.className = "toc-item-text";
noContent.style.cssText = `
color: #999;
font-style: italic;
text-align: center;
padding: 15px 0;
opacity: 0;
transition: opacity 0.3s ease;
white-space: nowrap;
`;
tocContainer.appendChild(noContent);
return;
}
// 创建TOC条目
prompts.forEach((prompt, index) => {
const item = document.createElement("div");
item.className = "toc-item";
item.style.cssText = `
padding: 6px 8px;
margin: 2px 0;
background: rgba(240, 240, 240, 0.5);
border-radius: 3px;
cursor: pointer;
transition: background-color 0.2s ease;
font-size: 12px;
line-height: 1.3;
border-left: 2px solid #4285f4;
min-height: 8px;
`;
// 添加文本
const itemText = document.createElement("span");
itemText.className = "toc-item-text";
itemText.textContent = truncateText(prompt.text, 25);
itemText.style.cssText = `
opacity: 0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
`;
item.appendChild(itemText);
// 添加悬停效果
item.addEventListener("mouseenter", () => {
item.style.backgroundColor = "rgba(66, 133, 244, 0.1)";
});
item.addEventListener("mouseleave", () => {
item.style.backgroundColor = "rgba(240, 240, 240, 0.5)";
});
// 添加点击跳转功能
item.addEventListener("click", () => {
prompt.element.scrollIntoView({
behavior: "smooth",
block: "center",
});
// 高亮目标元素
prompt.element.style.transition = "background-color 0.5s ease";
const originalBg = prompt.element.style.backgroundColor;
prompt.element.style.backgroundColor = "rgba(66, 133, 244, 0.2)";
setTimeout(() => {
prompt.element.style.backgroundColor = originalBg;
}, 2000);
});
tocContainer.appendChild(item);
});
}
// 初始化
function init() {
// 等待页面加载完成
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
return;
}
// 创建TOC
const tocContainer = createTOC();
// 初始更新
setTimeout(() => {
updateTOC(tocContainer);
}, 500);
// 监听DOM变化以自动更新TOC
const observer = new MutationObserver(() => {
// 防抖:延迟更新以避免频繁刷新
clearTimeout(window.tocUpdateTimeout);
window.tocUpdateTimeout = setTimeout(() => {
updateTOC(tocContainer);
}, 500);
});
// 开始观察
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: false,
});
}
// 启动脚本
init();
})();