Result of investigation: Injecting a TypeScript Script into Page Context (Chrome Extension V3) #1040
OchirDarmaev
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
This guide explains how to write a TypeScript script, bundle it into a single JavaScript file, and inject it into the page context using a Chrome Extension (Manifest V3). This is essential when you need access to the real
window
object and page-level JavaScript environment — beyond what a content script can do.✅ Why This Approach?
window
variables directly.inject.js
) — no hashed filenames.🗂️ Project Structure
⚙️ Step 1: Write Your Injection Script in TypeScript
src/injection/inject.ts
:src/injection/lib/hello.ts
:🛠️ Step 2: Bundle to a Single JS File with
esbuild
Install esbuild
Add script to
package.json
Run it:
This will generate
public/inject.js
— a fully self-contained script that can be injected.📥 Step 3: Inject the Script via Content Script
In your content script:
🧾 Step 4: Update
manifest.json
🧠 Benefits
inject.js
to inject🧪 Optional: Add Watch Mode for Dev
You can also add a dev watch mode for
inject.ts
:Beta Was this translation helpful? Give feedback.
All reactions