How palxiao/bilibili-banner works
Step 1: Identify the Main Components
• Frontend/UI Component
– Files: index.html, index.css, index.js
– Purpose: Render the webpage and implement the animated, interactive banner effect using native JS and CSS.
– Interaction: Uses fetch (driven by config.js) to load JSON configuration from assets for banner effects.
• Data Fetcher/Grabber
– Files: grab.js (invoked as npm run grab xxx)
– Purpose: Automatically grab the dynamic banner data from Bilibili’s homepage and generate configuration data.
– Function: Organizes fetched data under assets in subdirectories named by date; may also generate additional media resources.
• Configuration Module
– File: config.js
– Purpose: Store and provide configurable parameters (including possible API endpoints or fetch URL details) for the frontend.
– Interaction: Acts as a bridge so index.js knows how to load the correct JSON files from assets.
• Assets/Data Storage
– Directory: assets
– Contents: Dated folders (e.g., “2023-10-01”, “2024-06-26”, etc.) that contain JSON files (and sometimes additional files) with banner parameters like acceleration (a), rotation (deg), gravity (g), scale factor (f), and opacity ranges.
– Role: Provides the dynamic configuration data that the frontend uses to replicate the banner effects.
• Build/Package Management
– Files: package.json, .gitignore, LICENSE
– Purpose: Define dependencies (even though the application is built in native JavaScript with no third-party libraries) and scripts (for running the grab process and serving the application).
Step 2: Outline the Relationships and Data Flow
Data Generation Flow
– Developers run npm run grab with a banner name.
– grab.js fetches the banner data from Bilibili, storing the results under a new or existing dated folder inside assets (with associated JSON and media files).
– This process ensures that configuration files (data.json, etc.) are up-to-date.
Application Serving and Rendering Flow
– Developers run npm run serve to run a local static server.
– The browser loads index.html, which in turn loads index.css and index.js.
– index.js consults config.js to determine which JSON data to load.
– Using fetch, the browser obtains JSON from the assets folder and dynamically renders the banner based on parameters (such as acceleration, rotation, gravity, scaling, and opacity).
Step 3: Architectural Patterns and Design Principles
• Client-Side Rendering
– Entire banner functionality is implemented in the client using native JavaScript (no frameworks). This simplifies the architecture by eliminating a dedicated backend render layer.
• Separation of Concerns
– The project clearly distinguishes between data fetching (grab.js), configuration (config.js), static assets (assets directory), and rendering logic (index.js and index.html).
• Automated Data Update Process
– The grab script encapsulates the dynamic part where live data is obtained and stored. This enables rapid updating of the banner without manual intervention.
– Differentiate between static resources (assets, configuration file) and dynamic components (data grabber, frontend logic).
Since this is not a full-stack application but rather a front-end centric project with a dynamic data fetching process, emphasize:
– The split between the static rendering code (index.html/index.js) and the dynamic data source (assets folder updated by grab.js).
– How configuration and adjustments can be made manually by modifying JSON files in the assets folder.
– The role of the configuration file (config.js) in controlling fetch behavior and parameters.