Add commands for dealing with cache and site data, cookies, etc

This commit is contained in:
Josh at WLTechBlog
2025-09-30 08:00:30 -05:00
parent 74d99db629
commit 72d4a5653d
10 changed files with 1553 additions and 1 deletions

View File

@@ -1035,6 +1035,120 @@ func (d *Daemon) handleCommand(w http.ResponseWriter, r *http.Request) {
response = Response{Success: true, Data: result}
}
case "disable-cache":
tabID := cmd.Params["tab"]
timeoutStr := cmd.Params["timeout"]
// Parse timeout (default to 5 seconds if not specified)
timeout := 5
if timeoutStr != "" {
if parsedTimeout, err := strconv.Atoi(timeoutStr); err == nil && parsedTimeout > 0 {
timeout = parsedTimeout
}
}
err := d.setCacheDisabled(tabID, true, timeout)
if err != nil {
response = Response{Success: false, Error: err.Error()}
} else {
response = Response{Success: true}
}
case "enable-cache":
tabID := cmd.Params["tab"]
timeoutStr := cmd.Params["timeout"]
// Parse timeout (default to 5 seconds if not specified)
timeout := 5
if timeoutStr != "" {
if parsedTimeout, err := strconv.Atoi(timeoutStr); err == nil && parsedTimeout > 0 {
timeout = parsedTimeout
}
}
err := d.setCacheDisabled(tabID, false, timeout)
if err != nil {
response = Response{Success: false, Error: err.Error()}
} else {
response = Response{Success: true}
}
case "clear-cache":
tabID := cmd.Params["tab"]
timeoutStr := cmd.Params["timeout"]
// Parse timeout (default to 5 seconds if not specified)
timeout := 5
if timeoutStr != "" {
if parsedTimeout, err := strconv.Atoi(timeoutStr); err == nil && parsedTimeout > 0 {
timeout = parsedTimeout
}
}
err := d.clearBrowserCache(tabID, timeout)
if err != nil {
response = Response{Success: false, Error: err.Error()}
} else {
response = Response{Success: true}
}
case "clear-all-site-data":
tabID := cmd.Params["tab"]
timeoutStr := cmd.Params["timeout"]
// Parse timeout (default to 5 seconds if not specified)
timeout := 5
if timeoutStr != "" {
if parsedTimeout, err := strconv.Atoi(timeoutStr); err == nil && parsedTimeout > 0 {
timeout = parsedTimeout
}
}
err := d.clearAllSiteData(tabID, timeout)
if err != nil {
response = Response{Success: false, Error: err.Error()}
} else {
response = Response{Success: true}
}
case "clear-cookies":
tabID := cmd.Params["tab"]
timeoutStr := cmd.Params["timeout"]
// Parse timeout (default to 5 seconds if not specified)
timeout := 5
if timeoutStr != "" {
if parsedTimeout, err := strconv.Atoi(timeoutStr); err == nil && parsedTimeout > 0 {
timeout = parsedTimeout
}
}
err := d.clearCookies(tabID, timeout)
if err != nil {
response = Response{Success: false, Error: err.Error()}
} else {
response = Response{Success: true}
}
case "clear-storage":
tabID := cmd.Params["tab"]
timeoutStr := cmd.Params["timeout"]
// Parse timeout (default to 5 seconds if not specified)
timeout := 5
if timeoutStr != "" {
if parsedTimeout, err := strconv.Atoi(timeoutStr); err == nil && parsedTimeout > 0 {
timeout = parsedTimeout
}
}
err := d.clearStorage(tabID, timeout)
if err != nil {
response = Response{Success: false, Error: err.Error()}
} else {
response = Response{Success: true}
}
default:
d.debugLog("Unknown action: %s", cmd.Action)
response = Response{Success: false, Error: "Unknown action"}
@@ -5271,3 +5385,289 @@ func (d *Daemon) queryAccessibilityTree(tabID, selector, accessibleName, role st
d.debugLog("Successfully queried accessibility tree with %d matching nodes for tab: %s", len(axResult.Nodes), tabID)
return &axResult, nil
}
// setCacheDisabled enables or disables browser cache for a tab
func (d *Daemon) setCacheDisabled(tabID string, disabled bool, timeout int) error {
d.debugLog("Setting cache disabled=%v for tab: %s with timeout: %d", disabled, tabID, timeout)
// Use current tab if not specified
if tabID == "" {
tabID = d.currentTab
}
if tabID == "" {
return fmt.Errorf("no tab specified and no current tab available")
}
page, err := d.getTab(tabID)
if err != nil {
return fmt.Errorf("failed to get page: %v", err)
}
// Create a context with timeout if specified
if timeout > 0 {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()
// Create a channel to signal completion
done := make(chan error, 1)
// Execute the cache setting in a goroutine
go func() {
err := proto.NetworkSetCacheDisabled{CacheDisabled: disabled}.Call(page)
done <- err
}()
// Wait for completion or timeout
select {
case err := <-done:
if err != nil {
return fmt.Errorf("failed to set cache disabled: %v", err)
}
case <-ctx.Done():
return fmt.Errorf("timeout setting cache disabled after %d seconds", timeout)
}
} else {
// No timeout - execute directly
err := proto.NetworkSetCacheDisabled{CacheDisabled: disabled}.Call(page)
if err != nil {
return fmt.Errorf("failed to set cache disabled: %v", err)
}
}
d.debugLog("Successfully set cache disabled=%v for tab: %s", disabled, tabID)
return nil
}
// clearBrowserCache clears the browser cache for a tab
func (d *Daemon) clearBrowserCache(tabID string, timeout int) error {
d.debugLog("Clearing browser cache for tab: %s with timeout: %d", tabID, timeout)
// Use current tab if not specified
if tabID == "" {
tabID = d.currentTab
}
if tabID == "" {
return fmt.Errorf("no tab specified and no current tab available")
}
page, err := d.getTab(tabID)
if err != nil {
return fmt.Errorf("failed to get page: %v", err)
}
// Create a context with timeout if specified
if timeout > 0 {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()
// Create a channel to signal completion
done := make(chan error, 1)
// Execute the cache clearing in a goroutine
go func() {
err := proto.NetworkClearBrowserCache{}.Call(page)
done <- err
}()
// Wait for completion or timeout
select {
case err := <-done:
if err != nil {
return fmt.Errorf("failed to clear browser cache: %v", err)
}
case <-ctx.Done():
return fmt.Errorf("timeout clearing browser cache after %d seconds", timeout)
}
} else {
// No timeout - execute directly
err := proto.NetworkClearBrowserCache{}.Call(page)
if err != nil {
return fmt.Errorf("failed to clear browser cache: %v", err)
}
}
d.debugLog("Successfully cleared browser cache for tab: %s", tabID)
return nil
}
// clearAllSiteData clears all site data including cookies, storage, cache, etc. for a tab
func (d *Daemon) clearAllSiteData(tabID string, timeout int) error {
d.debugLog("Clearing all site data for tab: %s with timeout: %d", tabID, timeout)
// Use current tab if not specified
if tabID == "" {
tabID = d.currentTab
}
if tabID == "" {
return fmt.Errorf("no tab specified and no current tab available")
}
page, err := d.getTab(tabID)
if err != nil {
return fmt.Errorf("failed to get page: %v", err)
}
// Create a context with timeout if specified
if timeout > 0 {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()
// Create a channel to signal completion
done := make(chan error, 1)
// Execute the site data clearing in a goroutine
go func() {
// Clear all types of site data
err := proto.StorageClearDataForOrigin{
Origin: "*", // Clear for all origins
StorageTypes: "appcache,cookies,file_systems,indexeddb,local_storage,shader_cache,websql,service_workers,cache_storage",
}.Call(page)
done <- err
}()
// Wait for completion or timeout
select {
case err := <-done:
if err != nil {
return fmt.Errorf("failed to clear all site data: %v", err)
}
case <-ctx.Done():
return fmt.Errorf("timeout clearing all site data after %d seconds", timeout)
}
} else {
// No timeout - execute directly
err := proto.StorageClearDataForOrigin{
Origin: "*", // Clear for all origins
StorageTypes: "appcache,cookies,file_systems,indexeddb,local_storage,shader_cache,websql,service_workers,cache_storage",
}.Call(page)
if err != nil {
return fmt.Errorf("failed to clear all site data: %v", err)
}
}
d.debugLog("Successfully cleared all site data for tab: %s", tabID)
return nil
}
// clearCookies clears cookies for a tab
func (d *Daemon) clearCookies(tabID string, timeout int) error {
d.debugLog("Clearing cookies for tab: %s with timeout: %d", tabID, timeout)
// Use current tab if not specified
if tabID == "" {
tabID = d.currentTab
}
if tabID == "" {
return fmt.Errorf("no tab specified and no current tab available")
}
page, err := d.getTab(tabID)
if err != nil {
return fmt.Errorf("failed to get page: %v", err)
}
// Create a context with timeout if specified
if timeout > 0 {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()
// Create a channel to signal completion
done := make(chan error, 1)
// Execute the cookie clearing in a goroutine
go func() {
// Clear cookies only
err := proto.StorageClearDataForOrigin{
Origin: "*", // Clear for all origins
StorageTypes: "cookies",
}.Call(page)
done <- err
}()
// Wait for completion or timeout
select {
case err := <-done:
if err != nil {
return fmt.Errorf("failed to clear cookies: %v", err)
}
case <-ctx.Done():
return fmt.Errorf("timeout clearing cookies after %d seconds", timeout)
}
} else {
// No timeout - execute directly
err := proto.StorageClearDataForOrigin{
Origin: "*", // Clear for all origins
StorageTypes: "cookies",
}.Call(page)
if err != nil {
return fmt.Errorf("failed to clear cookies: %v", err)
}
}
d.debugLog("Successfully cleared cookies for tab: %s", tabID)
return nil
}
// clearStorage clears web storage (localStorage, sessionStorage, IndexedDB, etc.) for a tab
func (d *Daemon) clearStorage(tabID string, timeout int) error {
d.debugLog("Clearing storage for tab: %s with timeout: %d", tabID, timeout)
// Use current tab if not specified
if tabID == "" {
tabID = d.currentTab
}
if tabID == "" {
return fmt.Errorf("no tab specified and no current tab available")
}
page, err := d.getTab(tabID)
if err != nil {
return fmt.Errorf("failed to get page: %v", err)
}
// Create a context with timeout if specified
if timeout > 0 {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()
// Create a channel to signal completion
done := make(chan error, 1)
// Execute the storage clearing in a goroutine
go func() {
// Clear storage types (excluding cookies and cache)
err := proto.StorageClearDataForOrigin{
Origin: "*", // Clear for all origins
StorageTypes: "appcache,file_systems,indexeddb,local_storage,websql,service_workers,cache_storage",
}.Call(page)
done <- err
}()
// Wait for completion or timeout
select {
case err := <-done:
if err != nil {
return fmt.Errorf("failed to clear storage: %v", err)
}
case <-ctx.Done():
return fmt.Errorf("timeout clearing storage after %d seconds", timeout)
}
} else {
// No timeout - execute directly
err := proto.StorageClearDataForOrigin{
Origin: "*", // Clear for all origins
StorageTypes: "appcache,file_systems,indexeddb,local_storage,websql,service_workers,cache_storage",
}.Call(page)
if err != nil {
return fmt.Errorf("failed to clear storage: %v", err)
}
}
d.debugLog("Successfully cleared storage for tab: %s", tabID)
return nil
}