Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug Chrome Extensions within Visual Studio Code

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?

like image 966
Dally Horton Avatar asked Jun 20 '18 10:06

Dally Horton


People also ask

How do I debug Chrome extensions in VS Code?

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.

How do I debug Chrome extensions locally?

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.

Can I debug in Visual Studio Code?

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.


1 Answers

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}"]
        }
    ]
}
like image 137
Hans5958 Avatar answered Oct 13 '22 23:10

Hans5958