fix(webkit): prevent script injection into iframes causing message spam
Changed all UserContent script injections from UserContentInjectAllFrames to UserContentInjectTopFrame. Previously, scripts were being injected into every iframe on a page, causing duplicate color palette requests and other bridge messages (100+ messages per page with many iframes). Scripts that only need to run in the top-level page: - WebView ID and GTK theme detection - Color palette config and color-scheme detection - Main-world bridge (omnibox, OAuth, console capture) - GUI controls This fix dramatically reduces message spam and improves performance.
- commit
- 2db2e408e0285c61ae75e853bc35c9de7eac990c
- author
- bamen · · 2025-10-15 05:14 UTC
- parent
- 2217286
- signature
- unsigned
1 file · +6 -6
M pkg/webkit/user_content.go +6 -6
| @@ -30,7 +30,7 @@ func SetupUserContentManager(view *webkit.WebView, appearanceConfigJSON string, | |||
| 30 | 30 | `, webviewID) | |
| 31 | 31 | ucm.AddScript(webkit.NewUserScript( | |
| 32 | 32 | webviewIDScript, | |
| 33 | - | webkit.UserContentInjectAllFrames, | |
| 33 | + | webkit.UserContentInjectTopFrame, // Only top frame needs the webview ID | |
| 34 | 34 | webkit.UserScriptInjectAtDocumentStart, | |
| 35 | 35 | nil, | |
| 36 | 36 | nil, | |
| @@ -43,7 +43,7 @@ func SetupUserContentManager(view *webkit.WebView, appearanceConfigJSON string, | |||
| 43 | 43 | gtkThemeScript := fmt.Sprintf(`window.__dumber_gtk_prefers_dark = %t;`, prefersDark) | |
| 44 | 44 | ucm.AddScript(webkit.NewUserScript( | |
| 45 | 45 | gtkThemeScript, | |
| 46 | - | webkit.UserContentInjectAllFrames, | |
| 46 | + | webkit.UserContentInjectTopFrame, // Only top frame needs theme preference | |
| 47 | 47 | webkit.UserScriptInjectAtDocumentStart, | |
| 48 | 48 | nil, | |
| 49 | 49 | nil, | |
| @@ -56,7 +56,7 @@ func SetupUserContentManager(view *webkit.WebView, appearanceConfigJSON string, | |||
| 56 | 56 | paletteScript := fmt.Sprintf(`window.__dumber_palette = %s;`, appearanceConfigJSON) | |
| 57 | 57 | ucm.AddScript(webkit.NewUserScript( | |
| 58 | 58 | paletteScript, | |
| 59 | - | webkit.UserContentInjectAllFrames, | |
| 59 | + | webkit.UserContentInjectTopFrame, // Only top frame needs palette config | |
| 60 | 60 | webkit.UserScriptInjectAtDocumentStart, | |
| 61 | 61 | nil, | |
| 62 | 62 | nil, | |
| @@ -68,7 +68,7 @@ func SetupUserContentManager(view *webkit.WebView, appearanceConfigJSON string, | |||
| 68 | 68 | if assets.ColorSchemeScript != "" { | |
| 69 | 69 | ucm.AddScript(webkit.NewUserScript( | |
| 70 | 70 | assets.ColorSchemeScript, | |
| 71 | - | webkit.UserContentInjectAllFrames, | |
| 71 | + | webkit.UserContentInjectTopFrame, // Only top frame needs color scheme detection | |
| 72 | 72 | webkit.UserScriptInjectAtDocumentStart, | |
| 73 | 73 | nil, // whitelist (nil = all) | |
| 74 | 74 | nil, // blacklist (nil = none) | |
| @@ -80,7 +80,7 @@ func SetupUserContentManager(view *webkit.WebView, appearanceConfigJSON string, | |||
| 80 | 80 | if assets.MainWorldScript != "" { | |
| 81 | 81 | ucm.AddScript(webkit.NewUserScript( | |
| 82 | 82 | assets.MainWorldScript, | |
| 83 | - | webkit.UserContentInjectAllFrames, | |
| 83 | + | webkit.UserContentInjectTopFrame, // Only top frame needs bridge/UI functionality | |
| 84 | 84 | webkit.UserScriptInjectAtDocumentStart, | |
| 85 | 85 | nil, | |
| 86 | 86 | nil, | |
| @@ -92,7 +92,7 @@ func SetupUserContentManager(view *webkit.WebView, appearanceConfigJSON string, | |||
| 92 | 92 | if assets.GUIScript != "" { | |
| 93 | 93 | ucm.AddScript(webkit.NewUserScript( | |
| 94 | 94 | assets.GUIScript, | |
| 95 | - | webkit.UserContentInjectAllFrames, | |
| 95 | + | webkit.UserContentInjectTopFrame, // Only top frame needs GUI controls | |
| 96 | 96 | webkit.UserScriptInjectAtDocumentStart, | |
| 97 | 97 | nil, | |
| 98 | 98 | nil, | |
| @@ -30,7 +30,7 @@ func SetupUserContentManager(view *webkit.WebView, appearanceConfigJSON string, | |||||
| 30 | `, webviewID) | 30 | `, webviewID) | ||
| 31 | ucm.AddScript(webkit.NewUserScript( | 31 | ucm.AddScript(webkit.NewUserScript( | ||
| 32 | webviewIDScript, | 32 | webviewIDScript, | ||
| 33 | - | webkit.UserContentInjectAllFrames, | 33 | + | webkit.UserContentInjectTopFrame, // Only top frame needs the webview ID |
| 34 | webkit.UserScriptInjectAtDocumentStart, | 34 | webkit.UserScriptInjectAtDocumentStart, | ||
| 35 | nil, | 35 | nil, | ||
| 36 | nil, | 36 | nil, | ||
| @@ -43,7 +43,7 @@ func SetupUserContentManager(view *webkit.WebView, appearanceConfigJSON string, | |||||
| 43 | gtkThemeScript := fmt.Sprintf(`window.__dumber_gtk_prefers_dark = %t;`, prefersDark) | 43 | gtkThemeScript := fmt.Sprintf(`window.__dumber_gtk_prefers_dark = %t;`, prefersDark) | ||
| 44 | ucm.AddScript(webkit.NewUserScript( | 44 | ucm.AddScript(webkit.NewUserScript( | ||
| 45 | gtkThemeScript, | 45 | gtkThemeScript, | ||
| 46 | - | webkit.UserContentInjectAllFrames, | 46 | + | webkit.UserContentInjectTopFrame, // Only top frame needs theme preference |
| 47 | webkit.UserScriptInjectAtDocumentStart, | 47 | webkit.UserScriptInjectAtDocumentStart, | ||
| 48 | nil, | 48 | nil, | ||
| 49 | nil, | 49 | nil, | ||
| @@ -56,7 +56,7 @@ func SetupUserContentManager(view *webkit.WebView, appearanceConfigJSON string, | |||||
| 56 | paletteScript := fmt.Sprintf(`window.__dumber_palette = %s;`, appearanceConfigJSON) | 56 | paletteScript := fmt.Sprintf(`window.__dumber_palette = %s;`, appearanceConfigJSON) | ||
| 57 | ucm.AddScript(webkit.NewUserScript( | 57 | ucm.AddScript(webkit.NewUserScript( | ||
| 58 | paletteScript, | 58 | paletteScript, | ||
| 59 | - | webkit.UserContentInjectAllFrames, | 59 | + | webkit.UserContentInjectTopFrame, // Only top frame needs palette config |
| 60 | webkit.UserScriptInjectAtDocumentStart, | 60 | webkit.UserScriptInjectAtDocumentStart, | ||
| 61 | nil, | 61 | nil, | ||
| 62 | nil, | 62 | nil, | ||
| @@ -68,7 +68,7 @@ func SetupUserContentManager(view *webkit.WebView, appearanceConfigJSON string, | |||||
| 68 | if assets.ColorSchemeScript != "" { | 68 | if assets.ColorSchemeScript != "" { | ||
| 69 | ucm.AddScript(webkit.NewUserScript( | 69 | ucm.AddScript(webkit.NewUserScript( | ||
| 70 | assets.ColorSchemeScript, | 70 | assets.ColorSchemeScript, | ||
| 71 | - | webkit.UserContentInjectAllFrames, | 71 | + | webkit.UserContentInjectTopFrame, // Only top frame needs color scheme detection |
| 72 | webkit.UserScriptInjectAtDocumentStart, | 72 | webkit.UserScriptInjectAtDocumentStart, | ||
| 73 | nil, // whitelist (nil = all) | 73 | nil, // whitelist (nil = all) | ||
| 74 | nil, // blacklist (nil = none) | 74 | nil, // blacklist (nil = none) | ||
| @@ -80,7 +80,7 @@ func SetupUserContentManager(view *webkit.WebView, appearanceConfigJSON string, | |||||
| 80 | if assets.MainWorldScript != "" { | 80 | if assets.MainWorldScript != "" { | ||
| 81 | ucm.AddScript(webkit.NewUserScript( | 81 | ucm.AddScript(webkit.NewUserScript( | ||
| 82 | assets.MainWorldScript, | 82 | assets.MainWorldScript, | ||
| 83 | - | webkit.UserContentInjectAllFrames, | 83 | + | webkit.UserContentInjectTopFrame, // Only top frame needs bridge/UI functionality |
| 84 | webkit.UserScriptInjectAtDocumentStart, | 84 | webkit.UserScriptInjectAtDocumentStart, | ||
| 85 | nil, | 85 | nil, | ||
| 86 | nil, | 86 | nil, | ||
| @@ -92,7 +92,7 @@ func SetupUserContentManager(view *webkit.WebView, appearanceConfigJSON string, | |||||
| 92 | if assets.GUIScript != "" { | 92 | if assets.GUIScript != "" { | ||
| 93 | ucm.AddScript(webkit.NewUserScript( | 93 | ucm.AddScript(webkit.NewUserScript( | ||
| 94 | assets.GUIScript, | 94 | assets.GUIScript, | ||
| 95 | - | webkit.UserContentInjectAllFrames, | 95 | + | webkit.UserContentInjectTopFrame, // Only top frame needs GUI controls |
| 96 | webkit.UserScriptInjectAtDocumentStart, | 96 | webkit.UserScriptInjectAtDocumentStart, | ||
| 97 | nil, | 97 | nil, | ||
| 98 | nil, | 98 | nil, | ||