I'm trying to develop a Chrome extension within Visual Studio Code and I can't for the life of me figure out how to debug it properly. I can install the extension in Chrome and debug it there with Inspect popup, but I can't find the background.js or any other JavaScript files. I've installed Debugger for Chrome in Visual Studio Code although it doesn't seem to work for Chrome extensions.
Anyone have any ideas?
To debug any project in either Chrome or Microsoft Edge, all you need to do is to start a session by pressing F5 or activating the debug icon in the menu bar and selecting “Run and debug”. Alternatively, you can also use the Visual Studio Code command palette and run the “Debug: Open Link” command.
Navigate to the chrome extensions management page at chrome://extensions and ensure developer mode is on. Click the Load Unpacked button and select the broken extension directory. After the extension is loaded, it should have three buttons: Details, Remove and Errors in red letters.
VS Code has built-in debugging support for the Node.js runtime and can debug JavaScript, TypeScript, or any other language that gets transpiled to JavaScript.
Instead of having native configuration support like Firefox does, you need to supply arguments to load the extension before running Chrome, specifically the load-extension
argument.
Add this line inside your Chrome configuration object with the launch request, located on your .vscode/launch.json
file. This assumes that your manifest.json
file is directly on the workspace folder. If your manifest.json
file is located in another folder, change the ${workspaceFolder}
accordingly.
{
"runtimeArgs": ["--load-extension=${workspaceFolder}"]
}
For example, this is how I do it on the launch.json
file in my workspace.
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome",
"url": "https://example.com",
"runtimeArgs": ["--load-extension=${workspaceFolder}"]
}
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With