Skip to content

defineContentScript doesn't work with world: "MAIN" #282

@aklinker1

Description

@aklinker1

Describe the bug

Main world doesn't support browser APIs, but defineContentScript imports the polyfill because of ContentScriptContext.

Because these content scripts need sandboxed and can't have a ctx, WXT should export a separate function, defineMainWorldContentScript, from wxt/sandbox module, which doesn't include the webextension-polyfill, and thus avoids the error.

Alternatively, we could try and define globals inside content-script-entrypoint.ts and tree-shake the polyfill out. Not sure if that's possible though.

Or a third option, if the content script runs in the main world, alias webextension-polyfill to a noop module, similar to how we alias it for experimental.includeWebextensionPolyfill.

To Reproduce

Archive.zip

pnpm dev

Then go to https://google.com and open the console.

Screenshot 2023-12-13 at 11 36 00 AM

Expected behavior

Main world content scripts shouldn't import the polyfill and the script shouldn't crash.

Environment

 System: OS: macOS 13.4.1 CPU: (8) arm64 Apple M1 Pro Memory: 3.02 GB / 16.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 18.16.1 - ~/.asdf/installs/nodejs/18.16.1/bin/node Yarn: 1.22.19 - ~/.asdf/installs/nodejs/18.16.1/bin/yarn npm: 9.5.1 - ~/.asdf/plugins/nodejs/shims/npm pnpm: 8.9.0 - ~/.asdf/installs/nodejs/18.16.1/bin/pnpm Browsers: Chrome: 120.0.6099.71 Safari: 16.5.2 npmPackages: wxt: ^0.11.0 => 0.11.2 

Additional context

CC @AndrewWalsh

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions