Build tool
Vite
Where do you see the problem?
Describe the bug
When my manifest (V3) has a content script with "world": "MAIN", the following error appears in the Chrome console:
TypeError: Cannot read properties of undefined (reading 'getURL')
at my_content_script.js-loader.js:13:22
In the Sources tab, I see that my_content_script.js-loader.js contains:
(function () {
'use strict';
const injectTime = performance.now();
(async () => {
if ("")
await import(
/* @vite-ignore */
chrome.runtime.getURL("")
);
await import(
/* @vite-ignore */
chrome.runtime.getURL("vendor/vite-client.js")
);
const { onExecute } = await import(
/* @vite-ignore */
chrome.runtime.getURL("src/my_content_script.js")
);
onExecute?.({ perf: { injectTime, loadTime: performance.now() - injectTime } });
})().catch(console.error);
})();
CRXJS (or Vite?) is trying to call chrome.runtime.getURL to get the JS paths to import. However, chrome.runtime is not defined in the MAIN world.
These imports should be rewritten to first check for the existence of chrome.runtime — if it doesn't exist, then some sort of shim should be defined for chrome.runtime.getURL to get the desired paths.
As one way to do this, CRXJS could:
- Automatically add another content script in the isolated world
- Establish bidirectional communication between the two worlds (via events on
document.documentElement)
- Send a
CRXJS_getRootURL event to from MAIN to the isolated world
- Handle the event in the isolated world by calling
chrome.runtime.getURL('') and sending it back to MAIN
- Awaiting that response in the MAIN, then using it to define a shim for
chrome.runtime.getURL, then continuing with the imports as shown in the code block above.
Reproduction
mainfest.json:
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["src/my_content_script.js"],
"run_at": "document_start",
"world": "MAIN"
}
]
Logs
TypeError: Cannot read properties of undefined (reading 'getURL')
at my_content_script.js-loader.js:13:22
System Info
System:
OS: macOS 12.6.5
CPU: (8) arm64 Apple M1
Memory: 85.05 MB / 16.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 16.14.2 - /usr/local/bin/node
Yarn: 1.22.15 - /opt/homebrew/bin/yarn
npm: 8.3.0 - /opt/homebrew/bin/npm
Browsers:
Firefox: 72.0.2
Safari: 16.4.1
npmPackages:
@crxjs/vite-plugin: 2.0.0-beta.16 => 2.0.0-beta.16
vite: ^4.1.1 => 4.1.1
Severity
blocking an upgrade
Build tool
Vite
Where do you see the problem?
Describe the bug
When my manifest (V3) has a content script with
"world": "MAIN", the following error appears in the Chrome console:In the Sources tab, I see that my_content_script.js-loader.js contains:
CRXJS (or Vite?) is trying to call
chrome.runtime.getURLto get the JS paths to import. However,chrome.runtimeis not defined in the MAIN world.These imports should be rewritten to first check for the existence of
chrome.runtime— if it doesn't exist, then some sort of shim should be defined forchrome.runtime.getURLto get the desired paths.As one way to do this, CRXJS could:
document.documentElement)CRXJS_getRootURLevent to from MAIN to the isolated worldchrome.runtime.getURL('')and sending it back to MAINchrome.runtime.getURL, then continuing with the imports as shown in the code block above.Reproduction
mainfest.json:
Logs
System Info
System: OS: macOS 12.6.5 CPU: (8) arm64 Apple M1 Memory: 85.05 MB / 16.00 GB Shell: 5.8.1 - /bin/zsh Binaries: Node: 16.14.2 - /usr/local/bin/node Yarn: 1.22.15 - /opt/homebrew/bin/yarn npm: 8.3.0 - /opt/homebrew/bin/npm Browsers: Firefox: 72.0.2 Safari: 16.4.1 npmPackages: @crxjs/vite-plugin: 2.0.0-beta.16 => 2.0.0-beta.16 vite: ^4.1.1 => 4.1.1Severity
blocking an upgrade