如何使用Custom JavaScript for Websites 2在无需开发者工具的情况下保持谷歌创建快捷方式窗口的标题栏颜色
Hey there, let's fix that annoying issue where your caption bar color resets when you close DevTools. The problem is simple: the code you're running in the console only lives as long as DevTools is open. Custom JavaScript for Websites 2 lets you inject that code automatically every time your shortcut window loads, so the color stays put permanently.
Here's a step-by-step breakdown to get it working:
Step 1: Install the Extension
First, grab the "Custom JavaScript for Websites 2" extension from the Chrome Web Store (look for the one by Rob W). Once installed, you'll see its icon in your Chrome toolbar.
Step 2: Target Your Shortcut Window
Open the Chrome shortcut window you want to modify. Click the extension's icon, then select "Add new script" for this window's URL.
Quick note: For standalone app-mode windows (the ones without tabs), the URL might be the web app's actual address or a chrome-extension:// path if it's a packaged app. Make sure you select the correct context so the script only applies to this specific window.
Step 3: Add Your Caption Bar Color Code
Paste the code that changes your caption bar color into the extension's script editor. Depending on the method you used from that original guide, pick one of these approaches:
Option 1: CSS Variable Method (Most Common)
If your original fix used the --window-caption-color variable, use this code to inject the style persistently:
// Create and inject a style element with your custom color const customStyle = document.createElement('style'); customStyle.textContent = ` :root { --window-caption-color: #your-hex-color; /* Replace with your desired color (e.g., #2c3e50) */ --window-caption-text-color: #ffffff; /* Optional: set text color for better contrast */ } `; document.head.appendChild(customStyle);
Option 2: Theme Color Meta Tag
If you were using the theme color meta tag to affect the caption bar, use this to update it dynamically:
// Check for existing theme color tag, create it if missing let themeMeta = document.querySelector('meta[name="theme-color"]'); if (!themeMeta) { themeMeta = document.createElement('meta'); themeMeta.name = 'theme-color'; document.head.appendChild(themeMeta); } // Set your desired color themeMeta.content = '#your-hex-color';
Option 3: Adapt Your Original Code
If you had a specific snippet from that guide or the comment you found, wrap it in a DOM load listener to ensure it runs when the window is ready:
document.addEventListener('DOMContentLoaded', () => { // Paste your original code here // Example: document.body.style.setProperty('--window-caption-color', '#ff5722'); });
Step 4: Save and Test
Click "Save" in the extension's editor, then close and reopen your shortcut window. Your caption bar color should now stick around without needing DevTools open.
Troubleshooting Tips
- Script not applying?: Double-check that the extension is set to run on the exact URL of your window. Sometimes app-mode windows have slightly different URLs than the regular web page.
- Color being overridden?: Add
!importantto your CSS rule to prioritize it::root { --window-caption-color: #your-color !important; } - Packaged Chrome Apps: If you're working with a packaged app (not a web shortcut), try using the Chrome App API method:
Note: This only works for official packaged Chrome Apps, not web shortcuts in app mode.chrome.app.window.current().setBounds({ frameColor: '#your-color' });
内容的提问来源于stack exchange,提问作者C M

