Skip to content

Strategies for injecting code with registerContentScripts and CRXJS #643

Answered by jacksteamdev
jonlong asked this question in Q&A
Discussion options

You must be logged in to vote

My suggestion for this use case is to use two build processes:

  1. Build the main world script (main-world.ts -> main-world.iife.js) in a separate process using vite build --watch in library mode without CRXJS.
  2. In the main build process, include the output IIFE script file (main-world.iife.js) in web_accessible_resources in the manifest.
  3. In background.ts, register the content script as main-world.iife.js (don't import the script):
chrome.scripting
  .registerContentScripts([
    {
      id: "session-script",
      js: ["main-world.iife.js"],
      persistAcrossSessions: false,
      matches: ["*://www.google.com/*"],
      runAt: "document_start",
      world: "MAIN", // this fails
    },
 …

Replies: 4 comments 12 replies

Comment options

You must be logged in to vote
7 replies
@jonlong
Comment options

@jonlong
Comment options

@AntonOfTheWoods
Comment options

@AntonOfTheWoods
Comment options

@jacksteamdev
Comment options

Comment options

You must be logged in to vote
4 replies
@AntonOfTheWoods
Comment options

@remmel
Comment options

@remmel
Comment options

@remmel
Comment options

Answer selected by jonlong
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@remmel
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants