怎么用脚本清理浏览器缓存

wen 实用脚本 2

本文目录导读:

怎么用脚本清理浏览器缓存

  1. 命令行脚本(跨浏览器)
  2. JavaScript 脚本(通过浏览器控制台)
  3. Python 脚本(更可控)
  4. 使用 Playwright/Puppeteer(自动化测试)
  5. 注意事项

命令行脚本(跨浏览器)

Windows (Batch/PowerShell)

@echo off
:: 清除 Chrome 缓存
del /s /q "%LocalAppData%\Google\Chrome\User Data\Default\Cache\*"
:: 清除 Edge 缓存
del /s /q "%LocalAppData%\Microsoft\Edge\User Data\Default\Cache\*"
:: 清除 Firefox 缓存
del /s /q "%LocalAppData%\Mozilla\Firefox\Profiles\*.default\cache2\*"
echo 缓存清理完成!
pause

macOS/Linux (Shell)

#!/bin/bash
# 清除 Chrome 缓存
rm -rf ~/Library/Caches/Google/Chrome/*  # macOS
# 或 Linux
# rm -rf ~/.cache/google-chrome/*
# 清除 Firefox 缓存
rm -rf ~/Library/Caches/Firefox/*  # macOS
# 或 Linux
# rm -rf ~/.cache/mozilla/firefox/*
echo "缓存清理完成!"

JavaScript 脚本(通过浏览器控制台)

// 在浏览器开发者工具控制台运行
// 注意: 需要用户交互确认
caches.keys().then(function(names) {
    for (let name of names) {
        caches.delete(name);
    }
});
// 清除 Service Worker
if ('serviceWorker' in navigator) {
    navigator.serviceWorker.getRegistrations().then(function(registrations) {
        for(let registration of registrations) {
            registration.unregister();
        }
    });
}
// 清除 localStorage 和 sessionStorage
localStorage.clear();
sessionStorage.clear();

Python 脚本(更可控)

import os
import shutil
import platform
def clear_browser_cache():
    system = platform.system()
    if system == "Windows":
        paths = {
            "Chrome": os.path.expandvars(r"%LocalAppData%\Google\Chrome\User Data\Default\Cache"),
            "Edge": os.path.expandvars(r"%LocalAppData%\Microsoft\Edge\User Data\Default\Cache"),
            "Firefox": os.path.expandvars(r"%LocalAppData%\Mozilla\Firefox\Profiles")
        }
    elif system == "Darwin":  # macOS
        paths = {
            "Chrome": os.path.expanduser("~/Library/Caches/Google/Chrome"),
            "Firefox": os.path.expanduser("~/Library/Caches/Firefox")
        }
    else:  # Linux
        paths = {
            "Chrome": os.path.expanduser("~/.cache/google-chrome"),
            "Firefox": os.path.expanduser("~/.cache/mozilla/firefox")
        }
    for browser, path in paths.items():
        if os.path.exists(path):
            try:
                shutil.rmtree(path)
                print(f"{browser} 缓存已清除")
            except Exception as e:
                print(f"{browser} 清除失败: {e}")
# 使用前请关闭浏览器
# clear_browser_cache()

使用 Playwright/Puppeteer(自动化测试)

// Node.js 脚本
const puppeteer = require('puppeteer');
(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    // 清除所有缓存
    const client = await page.target().createCDPSession();
    await client.send('Network.clearBrowserCache');
    await client.send('Network.clearBrowserCookies');
    // 清除 IndexedDB
    await page.evaluate(async () => {
        const databases = await indexedDB.databases();
        databases.forEach(db => {
            indexedDB.deleteDatabase(db.name);
        });
    });
    await browser.close();
})();

注意事项

  1. 关闭浏览器:执行脚本前确保浏览器已完全关闭
  2. 备份重要数据:清除缓存不会影响密码和书签,但建议定期备份
  3. 管理员权限:某些系统目录需要管理员权限
  4. 定期清理:建议设置定时任务自动清理

需要我详细解释某个方案或提供更具体的实现吗?

抱歉,评论功能暂时关闭!