-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisual_trainer.js
More file actions
233 lines (205 loc) · 9.66 KB
/
Copy pathvisual_trainer.js
File metadata and controls
233 lines (205 loc) · 9.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
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
(function() {
// Create Button
const btn = document.createElement('div');
btn.className = 'geocoach-trainer-btn';
btn.innerHTML = `
<svg viewBox="0 0 24 24">
<path d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z"/>
</svg>
`;
document.body.appendChild(btn);
// Create Panel
const panel = document.createElement('div');
panel.className = 'geocoach-trainer-panel';
panel.innerHTML = `
<div class="geocoach-panel-header">
<h3>🤖 GeoCoach Trainer</h3>
<div style="display: flex; gap: 8px;">
<button class="geocoach-reset-btn" title="Reset">↻</button>
<button class="geocoach-close-btn" title="Close">×</button>
</div>
</div>
<div class="geocoach-panel-content">
<div class="geocoach-loading" style="display:none;">
<div class="geocoach-spinner"></div>
Analyzing view...
</div>
<div id="geocoach-results"></div>
</div>
`;
document.body.appendChild(panel);
const closeBtn = panel.querySelector('.geocoach-close-btn');
const resetBtn = panel.querySelector('.geocoach-reset-btn');
const content = panel.querySelector('#geocoach-results');
const loading = panel.querySelector('.geocoach-loading');
const header = panel.querySelector('.geocoach-panel-header');
// Drag Functionality
let isDragging = false;
let dragOffsetX = 0;
let dragOffsetY = 0;
header.addEventListener('mousedown', (e) => {
isDragging = true;
// Calculate offset from the top-left of the panel
const rect = panel.getBoundingClientRect();
dragOffsetX = e.clientX - rect.left;
dragOffsetY = e.clientY - rect.top;
// Disable transitions during drag for responsiveness
panel.style.transition = 'none';
// Ensure we switch to explicit left/top positioning if not already
// This prevents fighting with bottom/right CSS
panel.style.bottom = 'auto';
panel.style.right = 'auto';
panel.style.left = rect.left + 'px';
panel.style.top = rect.top + 'px';
});
document.addEventListener('mousemove', (e) => {
if (!isDragging) return;
e.preventDefault(); // Prevent selection
const newLeft = e.clientX - dragOffsetX;
const newTop = e.clientY - dragOffsetY;
panel.style.left = newLeft + 'px';
panel.style.top = newTop + 'px';
});
document.addEventListener('mouseup', () => {
if (isDragging) {
isDragging = false;
// Re-enable transitions if you had any (optional)
// panel.style.transition = '';
}
});
// Toggle Panel
function togglePanel(show) {
if (show) {
panel.classList.add('visible');
} else {
panel.classList.remove('visible');
}
}
// Fix: Use onclick directly to ensure event fires or add event listener correctly
// Sometimes elements are recreated or events lost if not careful.
closeBtn.onclick = (e) => {
e.stopPropagation();
togglePanel(false);
};
resetBtn.onclick = (e) => {
e.stopPropagation();
content.innerHTML = '';
// Optional: Close panel or keep it open empty?
// User asked for reset, likely just clearing results.
// Let's keep it open but empty.
content.innerHTML = `<div class="geocoach-clue-item">
<div class="geocoach-clue-text">Trainer reset. Click the robot button to analyze again.</div>
</div>`;
};
// Button Click Handler
btn.addEventListener('click', () => {
togglePanel(true);
content.innerHTML = '';
loading.style.display = 'block';
// Send message to background to capture screenshot and analyze
chrome.runtime.sendMessage({ type: "ANALYZE_SCREENSHOT" }, (response) => {
loading.style.display = 'none';
if (response && response.success) {
renderResults(response.data);
} else {
content.innerHTML = `<div class="geocoach-clue-item geocoach-confidence-low">
<div class="geocoach-clue-text">Error: ${response ? response.error : 'Unknown error'}</div>
</div>`;
}
});
});
async function renderResults(data) {
if (!data) {
content.innerHTML = `<div class="geocoach-clue-item">
<div class="geocoach-clue-text">No data received.</div>
</div>`;
return;
}
// Load Preferences
const prefs = await chrome.storage.local.get(['showKeyHint', 'showCoachAnalysis', 'showVisualClues']);
const showKeyHint = prefs.showKeyHint !== undefined ? prefs.showKeyHint : true;
const showCoachAnalysis = prefs.showCoachAnalysis !== undefined ? prefs.showCoachAnalysis : true;
const showVisualClues = prefs.showVisualClues !== undefined ? prefs.showVisualClues : true;
// 0. Display Key Hint (Top Priority)
if (showKeyHint && data.key_hint) {
const hintDiv = document.createElement('div');
hintDiv.className = 'geocoach-clue-item';
hintDiv.style.borderLeft = '3px solid #FFD700'; // Gold for Key Hint
hintDiv.style.background = 'rgba(255, 215, 0, 0.1)';
hintDiv.innerHTML = `
<div class="geocoach-clue-category" style="color: #FFD700;">🔑 KEY HINT</div>
<div class="geocoach-clue-text" style="font-weight: 500;">${data.key_hint}</div>
`;
content.appendChild(hintDiv);
}
// 1. Display Reasoning (The "Coach" part)
if (showCoachAnalysis && data.reasoning) {
const reasoningDiv = document.createElement('div');
reasoningDiv.className = 'geocoach-clue-item';
reasoningDiv.style.borderLeft = '3px solid #2196F3'; // Blue for info
reasoningDiv.innerHTML = `
<div class="geocoach-clue-category">💡 COACH ANALYSIS</div>
<div class="geocoach-clue-text" style="white-space: pre-wrap;">${data.reasoning}</div>
`;
content.appendChild(reasoningDiv);
}
// 2. Display Visual Clues
if (showVisualClues && data.clues && data.clues.length > 0) {
data.clues.forEach(clue => {
const item = document.createElement('div');
item.className = `geocoach-clue-item geocoach-confidence-${clue.confidence ? clue.confidence.toLowerCase() : 'medium'}`;
item.innerHTML = `
<div class="geocoach-clue-category">${clue.category}</div>
<div class="geocoach-clue-text">${clue.description}</div>
`;
content.appendChild(item);
});
}
// 3. Hidden Answer Section
if (data.country) {
const answerContainer = document.createElement('div');
answerContainer.style.marginTop = '15px';
answerContainer.style.textAlign = 'center';
answerContainer.style.borderTop = '1px solid #333';
answerContainer.style.paddingTop = '10px';
const revealBtn = document.createElement('button');
revealBtn.textContent = '🕵️ Reveal Answer';
revealBtn.style.cssText = 'padding: 8px 16px; background: #2a2a2a; color: #ddd; border: 1px solid #444; border-radius: 4px; cursor: pointer; font-size: 14px; transition: all 0.2s;';
revealBtn.onmouseover = () => revealBtn.style.background = '#3a3a3a';
revealBtn.onmouseout = () => revealBtn.style.background = '#2a2a2a';
const answerText = document.createElement('div');
answerText.style.display = 'none';
answerText.style.marginTop = '8px';
answerText.style.fontWeight = 'bold';
answerText.style.fontSize = '1.2em';
answerText.style.color = '#4CAF50';
answerText.style.textAlign = 'left';
answerText.style.background = 'rgba(0,0,0,0.3)';
answerText.style.padding = '10px';
answerText.style.borderRadius = '8px';
let answerHTML = `<div style="font-size: 1.4em; margin-bottom: 8px;">📍 ${data.country}</div>`;
if (data.city && data.city !== 'Unknown') {
answerHTML += `<div style="font-size: 1.1em; color: #e0e0e0; margin-top: 4px;">🏙️ ${data.city}</div>`;
}
if (data.region) {
answerHTML += `<div style="font-size: 1.0em; color: #bbb; margin-top: 4px; font-style: italic;">🧭 Position: ${data.region}</div>`;
}
answerText.innerHTML = answerHTML;
revealBtn.onclick = () => {
revealBtn.style.display = 'none';
answerText.style.display = 'block';
// Scroll to bottom
setTimeout(() => {
content.scrollTop = content.scrollHeight;
}, 10);
};
answerContainer.appendChild(revealBtn);
answerContainer.appendChild(answerText);
content.appendChild(answerContainer);
} else if (!data.reasoning && (!data.clues || data.clues.length === 0)) {
content.innerHTML = `<div class="geocoach-clue-item">
<div class="geocoach-clue-text">No specific clues found.</div>
</div>`;
}
}
})();