能否自定义Chrome或其他浏览器以在所有打开的标签页运行本地JavaScript文件?如何为Chrome附加JS文件实现页面加载自动执行及Chrome是否存储加载的HTML页面至本地C盘可访问位置?
Great questions! Let's break this down one by one:
1. Running a local JavaScript file across all open browser tabs
Absolutely, you can set up Chrome (or most modern browsers) to run a local JS file across every open tab. Here are two reliable methods:
Method 1: Use a userscript manager (like Tampermonkey)
This is the most straightforward approach for most users:
- Install the Tampermonkey extension from Chrome's web store
- Click the Tampermonkey icon in your toolbar and select "Create a new script"
- Replace the default template with your local JS code, or use the
@requiredirective to link directly to your local file (note: you'll need to enable "Allow access to file URLs" in Tampermonkey's settings to let it load local files) - Set the
@matchrule to*://*/*so the script triggers on every website - Save the script, and it will automatically run on all existing and new tabs
Method 2: Chrome's Local Overrides (for advanced customization)
If you want more control over how the script integrates with page content:
- Open Chrome DevTools (F12), switch to the "Sources" panel
- Click the "Overrides" tab on the left, check "Enable local overrides", and select a local folder to store your modified files
- Right-click the page's HTML file in the "Page" tab and choose "Save for overrides"
- Edit the local copy of the HTML file, adding
<script src="your-local-script.js"></script>right before the closing</body>tag - From now on, Chrome will load your modified local HTML version instead of the remote one, automatically running your JS file
2. Attaching a JS file to run automatically on page load + Chrome's local HTML storage
Auto-executing JS on page load
The methods above already cover this, but here's an alternative using a simple Chrome extension (great if you want to package your script for consistent use):
- Create a new folder and add two files:
manifest.json(configure the extension):{ "manifest_version": 3, "name": "My Custom Auto-Run Script", "version": "1.0", "content_scripts": [ { "matches": ["*://*/*"], "js": ["your-script.js"], "run_at": "document_idle" // Ensures the script runs after the page fully loads } ] }your-script.js(your custom JavaScript code)
- Open Chrome's extensions page (
chrome://extensions/), enable "Developer mode", then click "Load unpacked" and select your folder - The script will now run automatically every time any page finishes loading
Does Chrome store loaded HTML pages locally on the C drive?
Yes, but it's not designed for manual access:
- Chrome caches HTML, CSS, JS, and other resources to speed up future visits. On Windows, the main cache is located at
C:\Users\<YourUsername>\AppData\Local\Google\Chrome\User Data\Default\Cache(with compiled JS stored in theCode Cachesubfolder) - However, cached files use hashed filenames instead of their original URLs, so finding a specific HTML page manually is practically impossible
- Chrome also automatically clears old cache files to free up space, so these files aren't guaranteed to stick around long-term
- If you want to save a specific HTML page locally, your best bet is to right-click the page > "Save as..." and choose a location on your C drive directly
内容的提问来源于stack exchange,提问作者engrgolden
相关产品推荐
相关产品推荐

