Skip to Content
Multi-Platform Content Distribution 🚀 Read more → 
BlogMain World in Action: Making Chrome Extensions Talk Directly to Web Pages

Main World in Action: Making Chrome Extensions Talk Directly to Web Pages

I was working on a project recently where I needed to tweak a webpage’s global configuration object, but regular content scripts just couldn’t get to it. After struggling with this for way too long, I stumbled upon Chrome’s main world functionality - and man, was it a game-changer.

What Exactly is Main World?

Think of main world as the webpage’s personal JavaScript playground. It’s where all the code you type in the browser console hangs out, plus all the JavaScript that the webpage itself brings to the party. Unlike your extension’s isolated little bubble, scripts running in main world can directly mess with the webpage’s global objects - stuff like the window object and any variables the page defines.

It’s like moving from your extension’s cozy little office to the webpage’s wide-open coworking space where you can actually touch everything.

Using Main World in Plasmo is a Breeze

The cool thing about using main world in Plasmo is how stupid simple they’ve made it. Just toss world: "MAIN" into your content script config and you’re basically done:

// content.ts export const config: PlasmoContentScript = { matches: ["https://*.example.com/*"], world: "MAIN" // That's literally it }

Plasmo takes care of all the gnarly details behind the scenes. This declarative approach saves so much time - no more fighting with complicated registration processes.

Under the Hood: How Main World Works

Plasmo does a bunch of heavy lifting behind the curtain to make main world work like magic. It uses Chrome’s scripting.registerContentScripts API to dynamically register these scripts and automatically hooks up the required scripting permission.

When your project has main world scripts, Plasmo also fires up a background service to keep everything running smoothly, making sure the injection process works like a well-oiled machine.

When Should You Reach for Main World?

From my own trial and error, main world absolutely kills it in these scenarios:

  • Modifying webpage globals: When you need to tweak the page’s configuration objects or override functions
  • Deep interaction needs: For complex data exchange with the page’s native code
  • CSP workarounds: Main world can sometimes bypass strict content security policies
  • Third-party library injection: Some libraries just need to run in the page’s main environment to work properly

Remember that project I mentioned? The one where I needed to tweak a global config object? Main world made it a breeze after regular content scripts completely dropped the ball.

Main World vs Regular Content Scripts: The Real Difference

The main difference boils down to where your code actually runs:

  • Regular content scripts: Run in the extension’s isolated sandbox - safe but limited
  • Main world scripts: Run in the page’s main environment - powerful but requires more care

Which one you pick depends on what you’re trying to do. For simple stuff like reading page content or adding UI elements, regular content scripts will usually do the trick. But when you need to really get in there and modify how the page behaves, main world is definitely the way to go.

Handy Development Tips

Here are some handy tips I’ve learned along the way:

Checking for main world scripts: Plasmo gives you hasMainWorldScript for quick sanity checks and mainWorldScriptList to see all your main world scripts at a glance.

Automatic updates: When your content scripts change, Plasmo automatically updates the background service if you’ve got main world scripts, keeping everything fresh and up-to-date.

Code generation magic: Plasmo generates all the import statements and registration code you need for each main world script, taking care of all the fiddly little details.

Things to Keep in Mind

While main world is seriously powerful, there are some things you’ll want to keep in mind:

  1. Security is key: Running in the page’s main environment means you gotta be extra careful about code safety
  2. Compatibility matters: Make sure your code behaves well across different webpage environments
  3. Watch performance: Direct page manipulation can sometimes put a dent in performance

Wrapping Up

Main world is one of those advanced Chrome extension features that Plasmo has made surprisingly approachable. As developers, we get to focus on the actual business logic while the framework takes care of all the registration, permission, and background service headaches.

This “declarative development” approach fits right in with modern frontend practices - let developers focus on what actually matters while the framework handles the underlying machinery. If you’ve ever found yourself struggling to access webpage globals, definitely give main world a try. It might just be the exact solution you’ve been searching for.

Got questions or want to share your own war stories? Drop a comment below and let’s chat!

Last updated on