Skip to content

fix(bilibili): filter null subtitle content to prevent literal 'null' strings in video summaries#964

Open
octo-patch wants to merge 1 commit intoChatGPTBox-dev:masterfrom
octo-patch:fix/issue-893-bilibili-null-subtitle-content
Open

fix(bilibili): filter null subtitle content to prevent literal 'null' strings in video summaries#964
octo-patch wants to merge 1 commit intoChatGPTBox-dev:masterfrom
octo-patch:fix/issue-893-bilibili-null-subtitle-content

Conversation

@octo-patch
Copy link
Copy Markdown

@octo-patch octo-patch commented Apr 25, 2026

Fixes #893

Problem

The Bilibili subtitle API sometimes returns entries with null content values. When these are concatenated into a string using JavaScript's + operator, null gets coerced to the literal string "null", polluting the subtitle text sent to the AI model with large amounts of the word "null".

Solution

Replace the manual for loop with .map().filter().join() to skip any subtitle entries where content is null or undefined before building the subtitle string:

// Before
let subtitleContent = ''
for (let i = 0; i < subtitles.length; i++) {
  if (i === subtitles.length - 1) subtitleContent += subtitles[i].content
  else subtitleContent += subtitles[i].content + ','
}

// After
const subtitleContent = subtitles
  .map((s) => s.content)
  .filter((c) => c != null)
  .join(',')

Testing

Reproduced by simulating a subtitle response with null-content entries; confirmed the old code produces "null,null,actual text" and the new code produces "actual text" only.


Open in Devin Review

Summary by CodeRabbit

  • Bug Fixes
    • Improved subtitle handling to gracefully manage cases with missing or incomplete subtitle data.

…ings in video summaries

When Bilibili subtitle API returns entries with null content values,
JavaScript string concatenation converts them to the literal string "null",
polluting the subtitle text sent to the AI model.

Replace the manual loop with .map().filter().join() to skip null/undefined
content entries before building the subtitle string.

Fixes ChatGPTBox-dev#893

Co-Authored-By: Octopus <liyuan851277048@icloud.com>
@qodo-code-review
Copy link
Copy Markdown
Contributor

Review Summary by Qodo

Fix Bilibili null subtitle content filtering

🐞 Bug fix

Grey Divider

Walkthroughs

Description
• Filter null subtitle content from Bilibili API responses
• Replace manual loop with functional chain for cleaner code
• Prevent literal "null" strings polluting AI model input
Diagram
flowchart LR
  A["Bilibili subtitle API response"] -->|contains null entries| B["Old: manual loop concatenation"]
  B -->|produces literal 'null' strings| C["Polluted subtitle text"]
  A -->|contains null entries| D["New: map-filter-join chain"]
  D -->|skips null/undefined values| E["Clean subtitle text"]
Loading

Grey Divider

File Changes

1. src/content-script/site-adapters/bilibili/index.mjs 🐞 Bug fix +4/-5

Refactor subtitle concatenation with null filtering

• Replaced manual for loop with functional chain using .map().filter().join()
• Added null/undefined filtering to skip entries with null content values
• Improved code readability and maintainability with declarative approach
• Changed let to const for better immutability

src/content-script/site-adapters/bilibili/index.mjs


Grey Divider

Qodo Logo

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 25, 2026

📝 Walkthrough

Walkthrough

Changes subtitle text assembly in the Bilibili site adapter from an index-based loop with manual last-element handling to a functional pipeline using map, filter, and join operations. This approach removes null and undefined values and eliminates explicit separator logic.

Changes

Cohort / File(s) Summary
Bilibili Subtitle Processing
src/content-script/site-adapters/bilibili/index.mjs
Refactored subtitle content assembly to use a functional pipeline with map/filter/join instead of index-based loop iteration. Automatically filters out null/undefined entries, improving robustness.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Suggested labels

Review effort 1/5

Poem

🐰 A null string crept into the view,
So we filtered it out—now content's true!
Map and join make the pipeline clear,
Null subtitles vanish, no more to fear! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title directly summarizes the main change: filtering null subtitle content to prevent literal 'null' strings, which matches the core code modification.
Linked Issues check ✅ Passed The code change directly addresses issue #893 by filtering out null/undefined subtitle content to prevent 'null' strings from being sent to the summarization model.
Out of Scope Changes check ✅ Passed The changes are limited to the subtitle content assembly logic in bilibili/index.mjs, which is directly related to fixing the reported null string issue.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@qodo-code-review
Copy link
Copy Markdown
Contributor

qodo-code-review Bot commented Apr 25, 2026

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Qodo Logo

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the subtitle concatenation logic in the Bilibili site adapter, replacing a manual loop with a functional approach using map, filter, and join. Feedback was provided to improve the robustness of this logic by adding a fallback for the subtitles array and using optional chaining to prevent potential TypeErrors from unexpected API responses.

Comment on lines +54 to +57
const subtitleContent = subtitles
.map((s) => s.content)
.filter((c) => c != null)
.join(',')
Copy link
Copy Markdown

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

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 1 additional finding.

Open in Devin Review

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/content-script/site-adapters/bilibili/index.mjs (1)

54-57: LGTM — clean fix for the literal "null" tokens.

Using c != null correctly drops both null and undefined entries, and the functional pipeline removes the manual separator handling. This directly resolves the issue described in #893.

One optional consideration: empty-string content values (if the API ever returns them) would still pass the filter and produce adjacent commas (e.g., text,,more). If you want to be defensive against that, tightening the predicate would also strip blanks:

♻️ Optional: also drop empty / whitespace-only entries
       const subtitleContent = subtitles
         .map((s) => s.content)
-        .filter((c) => c != null)
+        .filter((c) => c != null && String(c).trim() !== '')
         .join(',')
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/content-script/site-adapters/bilibili/index.mjs` around lines 54 - 57,
Currently subtitleContent is built from subtitles.map(s => s.content).filter(c
=> c != null).join(','); keep the existing null/undefined guard but tighten the
predicate to also exclude empty or whitespace-only strings: when mapping to
content in the subtitle pipeline (variable subtitleContent and the subtitles.map
callback), update the filter to require c != null and that c.trim() is not empty
so blank strings are dropped before join, preventing adjacent commas in output.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/content-script/site-adapters/bilibili/index.mjs`:
- Around line 54-57: Currently subtitleContent is built from subtitles.map(s =>
s.content).filter(c => c != null).join(','); keep the existing null/undefined
guard but tighten the predicate to also exclude empty or whitespace-only
strings: when mapping to content in the subtitle pipeline (variable
subtitleContent and the subtitles.map callback), update the filter to require c
!= null and that c.trim() is not empty so blank strings are dropped before join,
preventing adjacent commas in output.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7b8ca80e-64bb-45c0-a78a-e581df365d01

📥 Commits

Reviewing files that changed from the base of the PR and between c236a4b and a19a61d.

📒 Files selected for processing (1)
  • src/content-script/site-adapters/bilibili/index.mjs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

视频总结模型输出结果出现大量"null"字符串,换了别的模型也是一样,只有少数模型不会出现这个现象

1 participant