Skip to content

Commit fb64fd4

Browse files
claude[bot]claude
andcommitted
Fix Copy Thread bug: use extractComments() instead of broken DOM traversal
The Copy Thread feature was failing because it tried to use a non-existent selector '#story_comments > ol.comments' and fell back to the main comments container which includes the comment form. This broke the DOM traversal. Fixed by: - Using the existing extractComments() function which correctly identifies comments - Building comment hierarchy from parent-child relationships in the extracted data - Removing dependency on the non-existent #story_comments selector 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 2a95de6 commit fb64fd4

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

lobsters-bookmarklet.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -164,29 +164,34 @@
164164
const storyAuthor = storyEl ? getAuthor(storyEl) : null;
165165
result.push(`[1] ${storyAuthor || 'Unknown'}: ${storyTitle}`);
166166

167-
function processLevel(parentEl, pathPrefix) {
168-
const subtrees = parentEl.querySelectorAll(':scope > .comments_subtree');
167+
// Use extractComments() to get all comments and build hierarchy
168+
const comments = extractComments();
169+
170+
// Build a map of comment ID to comment data for quick lookup
171+
const commentMap = new Map();
172+
comments.forEach(c => commentMap.set(c.id, c));
173+
174+
// Build hierarchy by processing top-level comments and their children
175+
function buildHierarchy(parentId = null, pathPrefix = '1') {
169176
let counter = 0;
170-
subtrees.forEach(subtree => {
171-
const comment = subtree.querySelector(':scope > .comment[id^="c_"]');
172-
if (!comment) return;
177+
178+
// Find all comments that are direct children of parentId
179+
const children = comments.filter(c => c.parentId === parentId);
180+
181+
children.forEach(comment => {
173182
counter++;
174-
const path = `${pathPrefix}.${counter}`;
175-
const author = getAuthor(comment) || 'Anonymous';
176-
const textEl = comment.querySelector('.comment_text');
183+
const path = parentId === null ? `${pathPrefix}.${counter}` : `${pathPrefix}.${counter}`;
184+
const author = comment.author || 'Anonymous';
185+
const textEl = comment.element.querySelector('.comment_text');
177186
const text = textEl ? textEl.textContent.trim().replace(/\s+/g, ' ') : '';
178187
result.push(`[${path}] ${author}: ${text}`);
179-
const nestedOl = subtree.querySelector(':scope > ol');
180-
if (nestedOl) {
181-
processLevel(nestedOl, path);
182-
}
188+
189+
// Recursively process children of this comment
190+
buildHierarchy(comment.id, path);
183191
});
184192
}
185-
186-
// The actual comment tree is inside #story_comments > ol.comments,
187-
// not the top-level ol.comments which also contains the comment form
188-
const actualComments = document.querySelector('#story_comments > ol.comments') || commentsContainer;
189-
processLevel(actualComments, '1');
193+
194+
buildHierarchy();
190195
return result.join('\n\n');
191196
}
192197

0 commit comments

Comments
 (0)