[Problem/Bug]: variable being redeclared when a preload script loads in an iframe in google.com
pushkin- opened this issue · 2 comments
What happened?
Maybe this is more an issue with the google site, but I couldn't reproduce this in my Electron app when I have it inject a preload script into iframes.
In WebView2, if I set a preload script that declares a variable, when I open a window to google.com and search for things that result in iframes being loaded (e.g. "erg"), I get crashes like this:
Uncaught SyntaxError: Identifier 'aVar' has already been declared
Importance
Moderate. My app's user experience is affected, but still usable.
Runtime Channel
Stable release (WebView2 Runtime)
Runtime Version
130.0.3849.80
SDK Version
1.0.2535.41
Framework
Winforms
Operating System
Windows 11
OS Version
22631.4317
Repro steps
- download the WinForms WebView2 sample app
- intercept the newwindowrequested event:
in WebView2Control_CoreWebView2InitializationCompleted
, add this.webView2Control.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
in that handler add:
private async void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
{
e.Handled = true;
using (e.GetDeferral())
{
Form form = new Form();
WebView2 webView = new WebView2();
await webView.EnsureCoreWebView2Async();
form.Controls.Add(webView);
await webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("let aVar = 0; console.log(aVar);");
form.Visible = true;
webView.Dock = DockStyle.Fill;
e.NewWindow = webView.CoreWebView2;
}
}
- start the app, open devtools and run
window.open("https://google.com")
- open devtools for the google window
- search "erg"
- see the errors in devtools
Interestingly, replacing let
with var
makes it work. const
fails just like let
.
Repros in Edge Browser
No, issue does not reproduce in the corresponding Edge version
Regression
No, this never worked
Last working version (if regression)
No response
The issue you're encountering with the Uncaught SyntaxError: Identifier 'aVar' has already been declared error is likely due to the way JavaScript handles variable declarations with let and const. These declarations are block-scoped, which means they are only accessible within the block of code (e.g., within a pair of curly braces {}) where they are defined.
Because let and const are block-scoped, you cannot redeclare the same variable within the same block. If you try to do so, you'll get a SyntaxError. Can you please check whether this is causing the issue that you reported?
This can cause conflicts when iframes are loaded because each iframe might try to declare the same variable name within the same scope, leading to errors.
@sandeepchads I'm only declaring the variable once though, at the top of the file. Even renaming the variable doesn't fix it; I thought maybe this third-party iframe was declarating the same variable, but that doesn't seem to be the case. (is that what you meant by your last sentence?)
And again, I don't seem to have this issue with Electron for whatever reason.