Skip to content

Commit 3354763

Browse files
JOHNJOHN
authored andcommitted
Add debug logging to annotation button and create debug guide
- Add detailed logging to handleTextSelection and showAnnotationButton - Log when annotations are disabled, selection validation fails, button clicks - Create ANNOTATION_DEBUG.md with troubleshooting steps - Helps diagnose why annotation button isn't appearing
1 parent 20519fd commit 3354763

File tree

3 files changed

+143
-1
lines changed

3 files changed

+143
-1
lines changed

ANNOTATION_DEBUG.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Annotation Feature Debug Guide
2+
3+
## How Annotations Should Work
4+
5+
1. **Select text** on any webpage
6+
2. A purple **"Add Annotation"** button should appear near your selection
7+
3. Click the button to open the annotation modal
8+
4. Enter your comment and submit
9+
10+
## Troubleshooting
11+
12+
### Check if Content Script is Loaded
13+
14+
1. Open any webpage
15+
2. Open browser DevTools (F12 or Cmd+Option+I)
16+
3. Go to Console tab
17+
4. Type: `window.__graphitiContentScriptLoaded`
18+
5. Should return `true` if loaded
19+
20+
### Check if Annotations are Enabled
21+
22+
1. Open extension popup
23+
2. Look for "Annotations" toggle switch
24+
3. Make sure it's ON (enabled)
25+
26+
Or in console:
27+
```javascript
28+
chrome.storage.local.get('annotationsEnabled', (result) => {
29+
console.log('Annotations enabled:', result.annotationsEnabled !== false);
30+
});
31+
```
32+
33+
### Check Console for Errors
34+
35+
1. Open DevTools Console
36+
2. Look for errors starting with `[Graphiti]`
37+
3. Check for:
38+
- "Content script loaded" message
39+
- "Annotation manager initialized" message
40+
- Any red error messages
41+
42+
### Test Selection
43+
44+
1. Select some text (at least 1 character, max 1000 characters)
45+
2. Check console for:
46+
- "handleTextSelection called"
47+
- "Text selected" with length
48+
- "Showing annotation button"
49+
- "Annotation button added to DOM"
50+
51+
### Common Issues
52+
53+
**Button doesn't appear:**
54+
- Content script not loaded → Reload extension
55+
- Annotations disabled → Enable in popup
56+
- Text too short/long → Select 1-1000 characters
57+
- Selection collapsed → Make sure you actually selected text
58+
59+
**Button appears but clicking does nothing:**
60+
- Check console for errors
61+
- Try clicking the button directly (not just the text)
62+
63+
### Manual Test
64+
65+
1. Open any webpage (e.g., example.com)
66+
2. Open DevTools Console
67+
3. Select some text
68+
4. You should see debug logs like:
69+
```
70+
[Graphiti] [DEBUG] ContentScript: handleTextSelection called
71+
[Graphiti] [DEBUG] ContentScript: Text selected {length: 10, preview: "..."}
72+
[Graphiti] [INFO] ContentScript: Showing annotation button
73+
[Graphiti] [INFO] ContentScript: Annotation button added to DOM
74+
```
75+
76+
### Keyboard Shortcut
77+
78+
Press `Alt+Shift+A` (Mac: `Option+Shift+A`) to toggle annotations on/off.

src/content/AnnotationManager.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,15 @@ export class AnnotationManager {
375375
}
376376

377377
private handleTextSelection(event: MouseEvent) {
378+
logger.debug('ContentScript', 'handleTextSelection called', {
379+
annotationsEnabled: this.annotationsEnabled,
380+
pageX: event.pageX,
381+
pageY: event.pageY
382+
});
383+
378384
// Don't process if annotations are disabled
379385
if (!this.annotationsEnabled) {
386+
logger.debug('ContentScript', 'Annotations disabled, skipping');
380387
return;
381388
}
382389

@@ -385,12 +392,14 @@ export class AnnotationManager {
385392
if (target && typeof target === 'object' && 'closest' in target) {
386393
const element = target as HTMLElement;
387394
if (element.closest('.pubky-annotation-button') || element.closest('.pubky-annotation-modal')) {
395+
logger.debug('ContentScript', 'Clicked on annotation UI, ignoring');
388396
return;
389397
}
390398
}
391399

392400
const selection = window.getSelection();
393401
if (!selection || selection.isCollapsed) {
402+
logger.debug('ContentScript', 'No selection or collapsed selection');
394403
// Delay hiding to allow button click to register
395404
setTimeout(() => {
396405
const button = document.querySelector('.pubky-annotation-button');
@@ -402,16 +411,27 @@ export class AnnotationManager {
402411
}
403412

404413
const selectedText = selection.toString().trim();
414+
logger.debug('ContentScript', 'Text selected', {
415+
length: selectedText.length,
416+
preview: selectedText.substring(0, 50)
417+
});
405418

406419
// Validate selected text using centralized validation
407420
const validation = validateSelectedText(selectedText);
408421
if (!validation.valid) {
409-
logger.debug('ContentScript', 'Selection validation failed', { error: validation.error });
422+
logger.warn('ContentScript', 'Selection validation failed', {
423+
error: validation.error,
424+
textLength: selectedText.length
425+
});
410426
return;
411427
}
412428

413429
const range = selection.getRangeAt(0);
414430
this.currentSelection = { range, text: validation.sanitized || selectedText };
431+
logger.info('ContentScript', 'Showing annotation button', {
432+
x: event.pageX,
433+
y: event.pageY
434+
});
415435
this.showAnnotationButton(event.pageX, event.pageY);
416436
}
417437

@@ -434,6 +454,7 @@ export class AnnotationManager {
434454
button.onmousedown = (e) => {
435455
e.preventDefault();
436456
e.stopPropagation();
457+
logger.info('ContentScript', 'Annotation button clicked (mousedown)');
437458
// Show modal immediately on mousedown to prevent button from being hidden
438459
this.showAnnotationModal();
439460
};
@@ -442,13 +463,18 @@ export class AnnotationManager {
442463
button.onclick = (e) => {
443464
e.preventDefault();
444465
e.stopPropagation();
466+
logger.info('ContentScript', 'Annotation button clicked (click)');
445467
// If modal isn't already showing, show it
446468
if (!document.querySelector('.pubky-annotation-modal')) {
447469
this.showAnnotationModal();
448470
}
449471
};
450472

451473
document.body.appendChild(button);
474+
logger.info('ContentScript', 'Annotation button added to DOM', {
475+
x: button.style.left,
476+
y: button.style.top
477+
});
452478
}
453479

454480
private hideAnnotationButton() {

test-annotation-debug.html

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Annotation Debug Test</title>
5+
</head>
6+
<body>
7+
<h1>Test Page for Annotations</h1>
8+
<p>Select some text on this page. A button should appear.</p>
9+
<p>This is a test paragraph with some text that you can select to test the annotation feature.</p>
10+
11+
<script>
12+
// Check if content script loaded
13+
setTimeout(() => {
14+
if (window.__graphitiContentScriptLoaded) {
15+
console.log('✅ Content script loaded');
16+
} else {
17+
console.error('❌ Content script NOT loaded');
18+
}
19+
20+
// Check if annotations are enabled
21+
if (typeof chrome !== 'undefined' && chrome.storage) {
22+
chrome.storage.local.get('annotationsEnabled', (result) => {
23+
console.log('Annotations enabled:', result.annotationsEnabled !== false);
24+
});
25+
}
26+
27+
// Listen for selection
28+
document.addEventListener('mouseup', () => {
29+
const selection = window.getSelection();
30+
if (selection && !selection.isCollapsed) {
31+
console.log('Text selected:', selection.toString().substring(0, 50));
32+
console.log('Selection length:', selection.toString().length);
33+
}
34+
});
35+
}, 2000);
36+
</script>
37+
</body>
38+
</html>

0 commit comments

Comments
 (0)