Skip to content

(extension) inject player script globally to move away from manual script injection [DA-406]#329

Merged
Mr-Quin merged 2 commits intomasterfrom
DA-406_Inject-player-script-globally-to-move-away-from-manual-script-injection-management
Mar 18, 2026
Merged

(extension) inject player script globally to move away from manual script injection [DA-406]#329
Mr-Quin merged 2 commits intomasterfrom
DA-406_Inject-player-script-globally-to-move-away-from-manual-script-injection-management

Conversation

@Mr-Quin
Copy link
Owner

@Mr-Quin Mr-Quin commented Mar 18, 2026

No description provided.

@Mr-Quin Mr-Quin self-assigned this Mar 18, 2026
Copilot AI review requested due to automatic review settings March 18, 2026 01:08
@vercel
Copy link

vercel bot commented Mar 18, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
danmaku-anywhere-web-dvay Ready Ready Preview, Comment Mar 18, 2026 1:24am

@Mr-Quin
Copy link
Owner Author

Mr-Quin commented Mar 18, 2026

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Mar 18, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
danmaku-anywhere-web a3683e8 Commit Preview URL

Branch Preview URL
Mar 18 2026, 01:24 AM

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors the extension's content script injection strategy for the player component. By moving from a manual, polling-based injection mechanism to a declarative approach via the manifest.json, the system gains robustness and simplifies frame management. The changes streamline the communication flow between the player and controller, ensuring a more reliable and efficient user experience.

Highlights

  • Player Script Injection: The player script (src/content/player/index.ts) is now globally injected into all HTTP/HTTPS pages and all frames via the extension's manifest.json using run_at: 'document_start' and world: 'ISOLATED'. This replaces the previous manual, programmatic injection method.
  • Removed Manual Scripting Logic: The ScriptingManager service and its injectVideoScript method, along with the FrameInjector service, have been removed. This eliminates the need for polling frames and manually injecting the player script, simplifying the background and content script architecture.
  • New Frame Management System: A new FrameRegistry service has been introduced in the controller to manage frame lifecycle events (registration, reloads, unloads) based on explicit playerReady and playerUnload RPC events from the player scripts. This replaces the polling-based frame detection.
  • RPC System Enhancements: The RPC client and server implementations have been updated to support an optional flag for RPC calls. This allows calls to be treated as best-effort, preventing errors if no receiver handles the message or if the connection cannot be established, which is useful for non-critical communications like initial player handshakes.
  • Player Script Refactoring: The player script (src/content/player/index.ts) has been refactored into a lightweight bootstrap (PlayerScript.service.ts) and a dedicated PlayerCommandHandler.service.ts. The bootstrap handles the initial handshake and then delegates to the command handler for full player functionality, improving modularity and startup efficiency.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@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 content script injection and frame management logic for the extension. It introduces a new player content script that is injected into all frames at document_start via the manifest, replacing the previous dynamic injection mechanism handled by ScriptingManager and FrameInjector. The ScriptingManager and FrameInjector services have been removed, and their responsibilities for tracking and managing frames are now handled by a new FrameRegistry service and integrated into the FrameManager component. The RPC system has been enhanced to support optional calls and new player lifecycle events (playerReady, playerUnload, controllerReady), improving the robustness of communication between the controller and player scripts, especially during frame reloads and initial handshakes. The FrameState in the store no longer directly stores documentId, as this is now managed internally by the FrameRegistry to differentiate between script retries and frame reloads.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the extension’s “player” content-script architecture to be injected globally (all frames, all HTTP(S) pages) and replaces the prior controller-driven per-frame script injection/polling with a lightweight player bootstrap + handshake-driven “heavy” initialization.

Changes:

  • Add a globally injected player bootstrap (PlayerScript) that performs a best-effort playerReady handshake and only initializes the full player handler after receiving start.
  • Replace the old frame polling/injection logic (FrameInjector) with a controller-side FrameRegistry that registers/unregisters frames based on playerReady / playerUnload events.
  • Extend the RPC layer with RpcOptions.optional for best-effort calls and update RPC payload types to include player-ready metadata.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/danmaku-anywhere/src/content/player/index.ts Simplifies player entrypoint to delegate to PlayerScript bootstrap.
packages/danmaku-anywhere/src/content/player/PlayerScript.service.ts New lightweight bootstrap: lite RPC server, playerReady handshake, transition to full handler.
packages/danmaku-anywhere/src/content/player/PlayerCommandHandler.service.ts New “heavy” handler that owns DOM/event wiring + full RPC command handling after start.
packages/danmaku-anywhere/src/content/controller/store/store.ts Removes documentId from persisted frame state and adjusts addFrame signature.
packages/danmaku-anywhere/src/content/controller/danmaku/frame/FrameRegistry.service.ts New registry tracking documentId per frame to dedupe handshake vs reload.
packages/danmaku-anywhere/src/content/controller/danmaku/frame/FrameManager.tsx Switches to FrameRegistry-based registration and adds controllerReady broadcast + playerUnload handling.
packages/danmaku-anywhere/src/content/controller/danmaku/frame/FrameInjector.service.ts Removes frame polling + manual script injection implementation.
packages/danmaku-anywhere/src/common/standalone/standaloneHandlers.ts Removes obsolete background handlers and adds new relay handlers (controllerReady, playerUnload).
packages/danmaku-anywhere/src/common/rpcClient/background/types.ts Removes getAllFrames/injectScript, adds new relay command/event types and playerReady payload.
packages/danmaku-anywhere/src/common/rpc/types.ts Adds RpcOptions.optional to support best-effort messaging.
packages/danmaku-anywhere/src/common/rpc/server.ts Refactors RPC server into a class and threads options through to handlers.
packages/danmaku-anywhere/src/common/rpc/client.ts Implements optional behavior (suppress errors / return empty response) when calls are best-effort.
packages/danmaku-anywhere/src/background/scripting/ScriptingManager.ts Removes player-script injection helper (no longer needed).
packages/danmaku-anywhere/src/background/rpc/RpcManager.ts Removes getAllFrames/injectScript RPCs and forwards options through relay pass-through.
packages/danmaku-anywhere/manifest.ts Adds globally injected player content_script for all frames on HTTP(S).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +103 to +109
private registerUnloadHandler() {
window.addEventListener('pagehide', () => {
void playerRpcClient.controller['relay:event:playerUnload'](
{ frameId: this.frameId },
{ optional: true }
)
})
run_at: 'document_start',
all_frames: true,
// @ts-expect-error -- crxjs types don't include `world`, but Chrome supports it
world: 'ISOLATED',
@Mr-Quin Mr-Quin merged commit 4e73a77 into master Mar 18, 2026
14 checks passed
@Mr-Quin Mr-Quin deleted the DA-406_Inject-player-script-globally-to-move-away-from-manual-script-injection-management branch March 18, 2026 02:41
@Mr-Quin Mr-Quin added the preview release Create a preview release label Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview release Create a preview release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants