Skip to content

Bilibili API Integration

Relevant source files

The following files were used as context for generating this wiki page:

This page documents the Bilibili API endpoints integrated into BiliChrome and how they are utilized throughout the codebase. It serves as a reference for understanding the web requests made to Bilibili's servers and how the returned data is processed within the extension.

For information about the Plugin System that extends BiliChrome's functionality, see Plugin System.

1. API Integration Overview

BiliChrome integrates with numerous Bilibili API endpoints to provide core functionality. The extension makes HTTP requests to these endpoints and processes the JSON responses to display content within the extension UI.

mermaid Diagram

Sources: js/components/player.js:1-361, js/components/search.js:1-209, js/components/dynamic.js:1-142, js/components/space.js:1-281, js/components/message.js:1-231

2. Authentication and Request Handling

2.1 Authentication

Bilibili's API requires authentication for many endpoints. BiliChrome relies on browser cookies for maintaining authentication with Bilibili's services.

Key authentication components:

  • biliJctData - A CSRF token extracted from cookies and used for authenticated POST requests
mermaid Diagram

Sources: js/components/player.js:324-359

2.2 Common Response Structure

Most Bilibili API responses follow a consistent structure:

FieldTypeDescription
codeIntegerStatus code (0 = success)
messageStringError message (if applicable)
dataObjectResponse payload

Example error handling:

javascript
// Example from player.js
.done((res) => {
    if (res.code == 0) { modal.toast("点赞成功"); }
    else if (res.code == 65006) { modal.toast("已赞过"); }
    else { modal.toast("点赞失败 [" + res.code + "] \n(" + res.message + ")"); }
})

Sources: js/components/player.js:326-334

3.1 Video Information API

The extension uses this API to retrieve metadata about videos, including title, description, author, and component media (like multiple parts).

Endpoint: https://api.bilibili.com/x/web-interface/view
Parameters:

  • bvid - Video ID in BV format
  • aid - Alternative video ID format

Usage example:

mermaid Diagram

Sources: js/components/player.js:126-147

3.2 Video Playback URL API

Used to obtain the actual video stream URL for playback.

Endpoint: https://api.bilibili.com/x/player/playurl
Parameters:

  • bvid - Video ID
  • cid - Content ID (obtained from video info)
  • qn - Quality level
  • platform - Platform type (html5)
  • high_quality - Request high quality (1 or 0)

Implementation:

javascript
// From player.js
$.get(`https://api.bilibili.com/x/player/playurl?type=mp4&platform=html5&bvid=${bvid}&cid=${cid}&qn=64&high_quality=${this.config.HD_Quality_As_Default ? 1 : 0}`, (result) => {
    this.ele_videoContainer.attr("src", result.data.durl[0].url);
});

Sources: js/components/player.js:295-302

3.3 Danmaku (Comment Overlay) API

Fetches the XML file containing danmaku comments for a specific video.

Endpoint: https://comment.bilibili.com/${cid}.xml
Parameters:

  • cid - Content ID of the video

The extension parses this XML response to extract time-coded comments for overlay display.

Sources: js/components/player.js:271-293

3.4 Comments API

Retrieves user comments on videos.

Endpoint: https://api.bilibili.com/x/v2/reply
Parameters:

  • type - Content type (1 for video)
  • oid - Object ID (video aid)
  • pn - Page number
  • ps - Page size (items per page)
  • sort - Sort method

Comment replies endpoint: https://api.bilibili.com/x/v2/reply/reply
For fetching replies to a specific comment.

Sources: js/components/player.js:153-156, js/components/player.js:259-269

3.5 Video Interaction APIs

Several endpoints for user interactions with videos:

ActionEndpointMethodParameters
Like/x/web-interface/archive/likePOSTbvid, like, csrf
Coin/x/web-interface/coin/addPOSTbvid, multiply, csrf
Add to Watch Later/x/v2/history/toview/addPOSTbvid, csrf

Sources: js/components/player.js:324-360

4. User and Space APIs

4.1 User Profile API

Retrieves information about a user.

Endpoint: https://api.bilibili.com/x/web-interface/card
Parameters:

  • mid - User ID

Response data includes:

  • Basic user information (name, avatar)
  • Level information
  • Statistics (followers, following)

Sources: js/components/space.js:4-35

4.2 User Subscription API

Retrieves a list of users that the specified user is following.

Endpoint: https://api.bilibili.com/x/relation/followings
Parameters:

  • vmid - User ID to query
  • pn - Page number
  • ps - Page size (items per page)
  • order - Sort order
  • order_type - Type of ordering

Sources: js/components/space.js:116-141

4.3 User History API

Retrieves the user's video viewing history.

Endpoint: https://api.bilibili.com/x/web-interface/history/cursor
Parameters:

  • ps - Page size (number of items)
  • type - Content type (archive for videos)

Sources: js/components/space.js:102-114

4.4 Multiple User Info API

Batch retrieves information for multiple users at once.

Endpoint: https://api.bilibili.com/x/polymer/pc-electron/v1/user/cards
Parameters:

  • uids - Comma-separated list of user IDs

Sources: js/components/message.js:131-138

5. Dynamic/Feed APIs

5.1 User Dynamic Feed API

Retrieves dynamic posts from a specific user.

Endpoint: https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/space
Parameters:

  • host_mid - User ID
  • offset - Pagination offset
  • timezone_offset - User's timezone offset
  • platform - Platform type (web)
  • type - Content type filter (optional)

Sources: js/components/space.js:37-100, js/components/dynamic.js:116-141

5.2 All Dynamics Feed API

Retrieves the feed of all subscribed users.

Endpoint: https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/all
Parameters:

  • timezone_offset - User's timezone offset
  • offset - Pagination offset
  • type - Content type filter
  • platform - Platform type (web)
  • page - Page number

Sources: js/components/dynamic.js:79-114

5.3 UP List API

Retrieves the list of followed content creators with updates.

Endpoint: https://api.bilibili.com/x/polymer/web-dynamic/v1/portal
Parameters:

  • up_list_more - Flag controlling list expansion

Sources: js/components/dynamic.js:46-77

mermaid Diagram

Sources: js/components/dynamic.js:1-142

6. Messaging and Notification APIs

6.1 Message Notification APIs

Various endpoints for retrieving different types of notifications.

Message TypeEndpointParameters
Replies/x/msgfeed/replyplatform, build, mobi_app, ps
Mentions/x/msgfeed/atbuild, mobi_app
Likes/x/msgfeed/likeplatform, build, mobi_app
System/x/sys-msg/query_user_notifypage_size, build, mobi_app
Unread Count/x/msgfeed/unreadNone

Sources: js/components/message.js:1-107

6.2 Private Message APIs

APIs for retrieving and managing private conversations.

Sessions list endpoint: https://api.vc.bilibili.com/session_svr/v1/session_svr/get_sessions
Parameters:

  • session_type - Type of sessions
  • group_fold - Group folding option
  • unfollow_fold - Unfollow folding option
  • sort_rule - Sort method
  • mobi_app - App platform

Messages endpoint: https://api.vc.bilibili.com/svr_sync/v1/svr_sync/fetch_session_msgs
Parameters:

  • talker_id - User ID of the conversation partner
  • session_type - Type of session
  • size - Number of messages to retrieve

Sources: js/components/message.js:110-160

mermaid Diagram

Sources: js/components/message.js:1-231

7. Search API

The extension uses a single endpoint for searching across different content types on Bilibili.

Endpoint: https://api.bilibili.com/x/web-interface/wbi/search/type
Parameters:

  • search_type - Content type to search (video, bili_user, live_room, etc.)
  • keyword - Search query
  • page - Page number

The search results are processed differently based on the content type selected.

Sources: js/components/search.js:110-208

mermaid Diagram

Sources: js/components/search.js:1-209

8. Favorites and Collection APIs

8.1 Collection List API

Retrieves a user's collection folders.

Endpoint: https://api.bilibili.com/x/v3/fav/folder/created/list-all
Parameters:

  • up_mid - User ID
  • ps - Page size
  • pn - Page number

Sources: js/components/space.js:143-161

8.2 Collection Content API

Retrieves items within a specific collection folder.

Endpoint: https://api.bilibili.com/x/v3/fav/resource/list
Parameters:

  • media_id - Collection ID
  • ps - Page size
  • pn - Page number

Sources: js/components/space.js:163-181

8.3 Watch Later API

Retrieves the user's "Watch Later" list.

Endpoint: https://api.bilibili.com/x/v2/history/toview

Sources: js/components/space.js:183-197, js/components/player.js:350-360

9. Error Handling

The codebase implements different approaches to error handling for API requests:

  1. Result code checking: Most endpoints return a code field that is checked to determine success/failure

  2. Promise-based error handling:

    javascript
    .done((res) => {
        // Success handling
    })
    .fail((res) => {
        // Error handling
    })
  3. Try-catch blocks for parsing and processing API responses:

    javascript
    try {
        // Process response data
    } catch (e) {
        console.error("Error message", e);
    }

Sources: js/components/player.js:326-348, js/components/space.js:96-99, js/components/player.js:276-288

10. API Request Flow

mermaid Diagram

The diagram above illustrates the typical flow of an API request within BiliChrome, from user interaction to UI update.

Sources: js/components/player.js:107-202, js/components/search.js:110-208, js/components/dynamic.js:79-141, js/components/space.js:37-100, js/components/message.js:1-99