Local Overrides in Chrome's DevTools are indeed a powerful feature for development, allowing you to persist changes to CSS, JavaScript, and other resources locally. However, as you noted, these overrides are only active while the DevTools are open and do not persist across sessions when the DevTools are closed.
To achieve the desired effect of injecting custom CSS and JS without using extensions and without needing to open DevTools every time, here are a few alternative approaches:
- Custom User Scripts with Tampermonkey or Greasemonkey
Tampermonkey (for Chrome) and Greasemonkey (for Firefox) are popular browser extensions that allow you to create and manage custom user scripts. These scripts can inject custom CSS and JS into web pages.
- Stylus for Custom CSS
Stylus is a user style manager that allows you to easily apply custom CSS to websites.
User Stylesheets (For Custom CSS)
If you only need to apply custom CSS, you can create a custom user stylesheet and configure your browser to use it. This approach varies by browser, but for Chrome, you can use the Stylish or Stylus extension mentioned above.
Developing a Custom Chrome Extension
If you're comfortable with a bit of coding, developing a custom Chrome extension is a powerful way to inject both CSS and JS into web pages. This would allow you to create a lightweight, personalized solution.
Here's a basic outline of what you'd need to do:
- manifest.json: Define your extension's permissions and content scripts.
- content.js: Write your custom JS.
- styles.css: Write your custom CSS.
- Injecting Scripts: Use the content script to inject the CSS and JS into the desired web pages.
Example manifest.json
:
{
"manifest_version": 3,
"name": "Custom Injector",
"version": "1.0",
"description": "Inject custom CSS and JS into web pages",
"permissions": ["activeTab"],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"],
"css": ["styles.css"]
}
]
}
Example content.js
:
// Your custom JS here
console.log('Custom JS loaded');
Example styles.css
:
/* Your custom CSS here */
body {
background-color: #f0f0f0;
}
- Using Bookmarklets
For simple scripts, you can use bookmarklets. A bookmarklet is a bookmark that contains JavaScript code.
Example Bookmarklet:
You can create a new bookmark and set the URL to the JavaScript code prefixed with javascript:
.
javascript
javascript:(function() {
// Your custom JS code
alert('Hello, World!');
})();
Conclusion
While Chrome DevTools Local Overrides cannot be used without keeping the DevTools open, these alternative methods should help you achieve the desired effect of injecting custom CSS and JS into web pages without relying on opening the DevTools every time.