-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.js
More file actions
249 lines (206 loc) · 9.61 KB
/
Copy pathcontent.js
File metadata and controls
249 lines (206 loc) · 9.61 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
console.log("Chat Thread Saver Content Script Loaded");
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "get_chat_content") {
try {
const hostname = window.location.hostname;
let data, title;
const date = new Date().toISOString().split('T')[0];
if (hostname.includes('gemini.google.com')) {
data = scrapeGemini();
title = `gemini-thread-${date}.md`;
} else if (hostname.includes('grok.com')) {
data = scrapeGrok();
title = `grok-thread-${date}.md`;
} else {
data = scrapeChatGPT();
title = `chatgpt-thread-${date}.md`;
}
sendResponse({ data: data, title: title });
} catch (e) {
console.error("Scraping error:", e);
sendResponse({ error: e.message });
}
}
return true;
});
function nodeToMarkdown(node) {
if (node.nodeType === Node.TEXT_NODE) {
const text = node.textContent;
const inTable = node.parentElement && ["TR", "TD", "TH", "THEAD", "TBODY", "TABLE"].includes(node.parentElement.tagName.toUpperCase());
return inTable ? text.trim() : text;
}
if (node.nodeType !== Node.ELEMENT_NODE) {
return "";
}
let childrenMarkdown = "";
node.childNodes.forEach(child => {
childrenMarkdown += nodeToMarkdown(child);
});
const tag = node.tagName.toUpperCase();
switch (tag) {
case "STRONG":
case "B":
return childrenMarkdown.trim() ? `**${childrenMarkdown.trim()}**` : "";
case "EM":
case "I":
return childrenMarkdown.trim() ? `*${childrenMarkdown.trim()}*` : "";
case "H1": return `\n# ${childrenMarkdown.trim()}\n`;
case "H2": return `\n## ${childrenMarkdown.trim()}\n`;
case "H3": return `\n### ${childrenMarkdown.trim()}\n`;
case "H4": return `\n#### ${childrenMarkdown.trim()}\n`;
case "H5": return `\n##### ${childrenMarkdown.trim()}\n`;
case "H6": return `\n###### ${childrenMarkdown.trim()}\n`;
case "P": return `\n${childrenMarkdown.trim()}\n`;
case "BR": return `\n`;
case "SUP": return childrenMarkdown.trim() ? `<sup>${childrenMarkdown.trim()}</sup>` : "";
case "SUB": return childrenMarkdown.trim() ? `<sub>${childrenMarkdown.trim()}</sub>` : "";
case "SPAN":
// Gemini Math support (KaTeX)
if (node.classList.contains('math-inline') || node.classList.contains('math-block')) {
const math = node.getAttribute('data-math');
if (math) {
const isBlock = node.classList.contains('math-block');
return isBlock ? `\n$$\n${math}\n$$\n` : `$${math}$`;
}
}
return childrenMarkdown;
case "CODE":
if (node.parentNode.tagName === "PRE") {
return childrenMarkdown;
}
return `\`${childrenMarkdown.trim()}\``;
case "PRE":
const lang = node.className.match(/language-(\w+)/);
return `\n\`\`\`${lang ? lang[1] : ""}\n${childrenMarkdown}\n\`\`\`\n`;
case "UL": return `\n${childrenMarkdown}\n`;
case "OL": return `\n${childrenMarkdown}\n`;
case "LI":
const parent = node.parentNode.tagName;
const prefix = parent === "OL" ? "1. " : "* ";
return `${prefix}${childrenMarkdown.trim()}\n`;
case "BLOCKQUOTE":
return `\n> ${childrenMarkdown.trim().split('\n').join('\n> ')}\n`;
case "TABLE": return `\n${childrenMarkdown.trim()}\n`;
case "THEAD": return childrenMarkdown.trim() + "\n";
case "TBODY": return childrenMarkdown.trim() + "\n";
case "TR":
const rowContent = childrenMarkdown.split('|').map(s => s.trim()).filter(s => s.length > 0).join(' | ');
if (!rowContent) return "";
let row = `| ${rowContent} |\n`;
if (node.parentNode.tagName === "THEAD" || (node.parentNode.tagName === "TBODY" && !node.previousElementSibling)) {
const cells = node.querySelectorAll('th, td').length;
row += `| ${Array(cells).fill('---').join(' | ')} |\n`;
}
return row;
case "TH":
case "TD":
return `| ${childrenMarkdown.trim()}`;
case "A":
return `[${childrenMarkdown.trim()}](${node.href})`;
default:
return childrenMarkdown;
}
}
function scrapeChatGPT() {
let md = "# ChatGPT Conversation\n\n";
// Modern ChatGPT uses 'section' tags or direct '[data-message-author-role]' attributes
// We'll query for the messages directly as it's the most reliable across UI versions
const messageElements = document.querySelectorAll('[data-message-author-role]');
if (messageElements.length === 0) {
// Fallback for older versions that might still use 'article'
const articles = document.querySelectorAll('article');
if (articles.length === 0) {
return "No messages found. Ensure you are on a ChatGPT chat page and the content has loaded.";
}
articles.forEach((article) => {
const userIcon = article.querySelector('[data-testid="icon-user"]');
const botIcon = article.querySelector('[data-testid="icon-openai"]');
let role = "Unknown";
const messageBlock = article.querySelector('[data-message-author-role]');
if (messageBlock) {
role = messageBlock.getAttribute('data-message-author-role');
} else {
if (userIcon) role = "user";
if (botIcon) role = "assistant";
}
const roleDisplay = role.charAt(0).toUpperCase() + role.slice(1);
const contentDiv = article.querySelector('.markdown') || article.querySelector('[data-message-author-role]') || article;
let text = nodeToMarkdown(contentDiv);
md += `**${roleDisplay}:**\n\n${text}\n\n---\n\n`;
});
} else {
messageElements.forEach((msgElement) => {
const role = msgElement.getAttribute('data-message-author-role');
const roleDisplay = role.charAt(0).toUpperCase() + role.slice(1);
// Assistant messages use '.markdown'
// User messages are typically in a div sibling to the avatar/icon
let contentDiv = msgElement.querySelector('.markdown');
if (!contentDiv) {
// For user messages, we want the container that doesn't include the 'edit' buttons
// Usually it's a div with a lot of classes, but we can try to find the one with the text
const potentialContent = msgElement.querySelector('div[class*="flex-col"]');
contentDiv = potentialContent || msgElement;
}
let text = nodeToMarkdown(contentDiv);
// Cleanup: Sometimes buttons like "Copy" or "Edit" get included if selectors are too broad
// But nodeToMarkdown is generally good at only picking up text and structural elements
md += `**${roleDisplay}:**\n\n${text}\n\n---\n\n`;
});
}
return md;
}
function scrapeGemini() {
let md = "# Gemini Conversation\n\n";
const possibleMessages = [];
function walk(node) {
if (!node) return;
const tagName = node.tagName ? node.tagName.toUpperCase() : "";
if (tagName === 'USER-QUERY' || tagName === 'MODEL-RESPONSE') {
possibleMessages.push(node);
return;
}
if (node.shadowRoot) {
walk(node.shadowRoot);
}
node.childNodes.forEach(child => walk(child));
}
walk(document.body);
if (possibleMessages.length === 0) {
return "No Gemini messages found. Ensure you are on a Gemini chat page.";
}
possibleMessages.forEach(msg => {
const tagName = msg.tagName.toUpperCase();
let role = (tagName === 'USER-QUERY') ? "User" : "Assistant";
const contentContainer = (role === "User")
? msg.querySelector('.query-content')
: msg.querySelector('.markdown') || msg.querySelector('message-content');
let text = "";
if (contentContainer) {
text = nodeToMarkdown(contentContainer);
} else {
text = nodeToMarkdown(msg);
}
md += `**${role}:**\n\n${text}\n\n---\n\n`;
});
return md;
}
function scrapeGrok() {
let md = "# Grok Conversation\n\n";
// Grok uses data-testid="user-message" and data-testid="assistant-message"
const messageElements = document.querySelectorAll('[data-testid$="-message"]');
if (messageElements.length === 0) {
return "No messages found. Ensure you are on a Grok chat page and the content has loaded.";
}
messageElements.forEach((msgElement) => {
const testId = msgElement.getAttribute('data-testid');
let role = testId.includes('user') ? "User" : "Assistant";
// For Grok, we look for markdown containers or the message bubble itself
const contentDiv = msgElement.querySelector('.markdown') ||
msgElement.querySelector('.response-content-markdown') ||
msgElement.querySelector('.message-bubble') ||
msgElement;
let text = nodeToMarkdown(contentDiv);
md += `**${role}:**\n\n${text}\n\n---\n\n`;
});
return md;
}