Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/content-script/site-adapters/bilibili/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ export default {
const subtitleData = await subtitleResponse.json()
const subtitles = subtitleData.body

let subtitleContent = ''
for (let i = 0; i < subtitles.length; i++) {
if (i === subtitles.length - 1) subtitleContent += subtitles[i].content
else subtitleContent += subtitles[i].content + ','
}
const subtitleContent = subtitles
.map((s) => s.content)
.filter((c) => c != null)
.join(',')
Comment on lines +54 to +57
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation assumes that subtitles is always an array and that each element s is a valid object. If the API returns an unexpected response (e.g., subtitleData.body is missing or contains null entries), this code will throw a TypeError. Adding a fallback for subtitles and using optional chaining for s.content would make this more robust.

Suggested change
const subtitleContent = subtitles
.map((s) => s.content)
.filter((c) => c != null)
.join(',')
const subtitleContent = (subtitles || [])
.map((s) => s?.content)
.filter((c) => c != null)
.join(',')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coderabbitai, what do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PeterDaveHello, the Gemini suggestion has some merit, but I'd call it partially worthwhile rather than essential. Here's my breakdown:

(subtitles || []) — Reasonable defensive guard. If subtitleData.body is null or undefined (malformed API response), calling .map() on it would throw a TypeError. That said, the entire inputQuery is already wrapped in a try/catch, so the error would be silently swallowed anyway. Adding || [] makes the failure mode slightly more graceful (returns '' instead of throwing), which is a minor improvement.

s?.content — This is the weaker part of the suggestion. The original issue (#893) was specifically that the content field within a subtitle entry can be null — not that the subtitle entries themselves can be null. The Bilibili API is unlikely to return null elements inside the body array. Optional chaining here is defensive against a case that isn't observed in practice. It's harmless since undefined is already filtered out by .filter((c) => c != null), but it's essentially noise.

My take: The PR as-is already addresses the reported bug cleanly. If you want to adopt part of the suggestion, subtitles || [] is a reasonable addition. I'd skip s?.content as it addresses an untested and unlikely edge case, adding minor cognitive overhead without a clear corresponding bug report.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, let’s keep this PR focused on content: null; malformed body handling is pre-existing and can be addressed separately.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PeterDaveHello, that makes sense — the PR is nicely scoped as-is. Would you like me to open a follow-up GitHub issue to track the pre-existing malformed body handling (e.g., guarding against null/undefined subtitleData.body)? That way it won't get lost.


return await cropText(
`You are an expert video summarizer. Create a comprehensive summary of the following Bilibili video in markdown format, ` +
Expand Down