How It Works
Visual Studio Code does not provide a native API for deep customization of its user interface with custom CSS or JavaScript. This extension works by directly modifying one of VS Code's core files responsible for rendering the workbench (the main editor window).
This approach, often called "monkey-patching," is powerful but also the reason why administrative permissions are required and why VS Code shows a "corrupted" warning.
The Patching Process
When you run the Enable Custom CSS and JS command, the extension performs a series of automated steps:
-
Locate the Workbench HTML: The extension first needs to find the correct HTML file. It searches for
workbench.html(or variants likeworkbench.esm.html) in likely VS Code installation directories, such as.../vs/code/electron-browser/workbench. -
Create a Backup: Before making any modifications, the extension creates a clean backup of the original
workbench.htmlfile. This backup is namedworkbench.<SESSION_ID>.bak-custom-cssand is stored in the same directory. This ensures that theDisable Custom CSS and JScommand can reliably restore VS Code to its original state. -
Fetch and Inline Content: The extension iterates through the URLs specified in your
vscode_custom_css.importssetting. For each URL:- It fetches the content of the file (either by reading it from the local disk or downloading it from the web).
- The content is then prepared for injection. CSS content is wrapped in
<style>tags, and JavaScript content is wrapped in<script>tags.
-
Inject into HTML: The inlined
<style>and<script>blocks are injected into theworkbench.htmlfile just before the closing</html>tag. A unique session ID comment is also added to track that the file has been patched. -
Remove Content Security Policy (CSP): To allow the newly injected inline styles and scripts to execute, the extension removes the
<meta http-equiv="Content-Security-Policy" ... />tag from the HTML file's header. This is a crucial step that relaxes VS Code's security model for the workbench.
The Disabling Process
When you run the Disable Custom CSS and JS command, the process is simple and safe:
- The extension locates the backup file (
workbench.<SESSION_ID>.bak-custom-css). - It replaces the modified
workbench.htmlwith the original content from the backup file. - It deletes the backup file to clean up.
After a restart, VS Code is back to its original, unmodified state.