Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Problem/Bug]: variable being redeclared when a preload script loads in an iframe in google.com #4929

Open
pushkin- opened this issue Nov 15, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@pushkin-
Copy link

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

  1. download the WinForms WebView2 sample app
  2. 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;
		}
	}
  1. start the app, open devtools and run window.open("https://google.com")
  2. open devtools for the google window
  3. search "erg"
  4. 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

@pushkin- pushkin- added the bug Something isn't working label Nov 15, 2024
@pushkin- pushkin- changed the title [Problem/Bug]: [Problem/Bug]: variable being redeclared when a preload script loads in an iframe in google.com Nov 15, 2024
@sandeepchads
Copy link

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.

@pushkin-
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants