-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
121 lines (107 loc) · 3.35 KB
/
Copy pathcontent.js
File metadata and controls
121 lines (107 loc) · 3.35 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
// content.js - Fixed version
let lastUrl = location.href;
let processedPrompts = new Set();
let isInitialized = false;
function sendMessageSafely(message, callback) {
try {
chrome.runtime.sendMessage(message, (response) => {
if (chrome.runtime.lastError) {
console.log('Message error:', chrome.runtime.lastError.message);
} else if (callback) {
callback(response);
}
});
} catch (error) {
console.log('Send message failed:', error);
}
}
function detectAndSendPrompt() {
// Try multiple selectors for ChatGPT user messages
const selectors = [
'[data-message-author-role="user"] .whitespace-pre-wrap',
'[data-message-author-role="user"] div[class*="markdown"]',
'.group\\/conversation-turn[data-author="user"] .whitespace-pre-wrap',
'div[class*="user"] div[class*="markdown"]',
'div[class*="from-user"] .whitespace-pre-wrap'
];
for (const selector of selectors) {
const elements = document.querySelectorAll(selector);
elements.forEach(element => {
const text = element.textContent?.trim();
if (text && text.length > 5 && !processedPrompts.has(text)) {
processedPrompts.add(text);
console.log('Found prompt:', text.substring(0, 50) + '...');
sendMessageSafely({ action: 'newPrompt', prompt: text });
}
});
}
}
function startObserving() {
const targetNode = document.body;
const observer = new MutationObserver((mutations) => {
let shouldCheck = false;
mutations.forEach((mutation) => {
if (mutation.addedNodes.length > 0) {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === 1 && node.textContent?.length > 0) {
shouldCheck = true;
}
});
}
});
if (shouldCheck) {
setTimeout(detectAndSendPrompt, 1000);
}
});
observer.observe(targetNode, {
childList: true,
subtree: true
});
}
// Handle scroll requests
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'scrollToPrompt') {
const prompt = message.prompt;
const selectors = [
'[data-message-author-role="user"] .whitespace-pre-wrap',
'[data-message-author-role="user"] div[class*="markdown"]',
'.group\\/conversation-turn[data-author="user"] .whitespace-pre-wrap'
];
for (const selector of selectors) {
const elements = document.querySelectorAll(selector);
for (const element of elements) {
if (element.textContent?.trim() === prompt) {
element.scrollIntoView({ behavior: 'smooth', block: 'center' });
sendResponse({ success: true });
return;
}
}
}
sendResponse({ success: false });
}
return true;
});
// Monitor URL changes
function checkUrlChange() {
const currentUrl = location.href;
if (currentUrl !== lastUrl) {
lastUrl = currentUrl;
processedPrompts.clear();
sendMessageSafely({ action: 'chatChanged', newUrl: currentUrl });
}
}
// Initialize
function initialize() {
if (isInitialized) return;
isInitialized = true;
console.log('ChatGPT Prompt Recorder initialized');
detectAndSendPrompt();
startObserving();
setInterval(checkUrlChange, 2000);
}
// Start when ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initialize);
} else {
setTimeout(initialize, 1000);
}