/// <summary> /// Prompt the user for some script and register it to execute whenever a new page loads. /// </summary> public void AddInitializeScript() { TextInputDialog dialog = new TextInputDialog( "Add Initialize Script", "Initialization Script:", "Enter the JavaScript code to run as the initialization script that " + "runs before any script in the HTML document.", // This example script stops child frames from opening new windows. Because // the initialization script runs before any script in the HTML document, we // can trust the results of our checks on window.parent and window.top. "if (window.parent !== window.top) {\r\n" + " delete window.open;\r\n" + "}", false); if (dialog.ShowDialog() == true) { _webView2.AddScriptToExecuteOnDocumentCreated( dialog.Input, (args) => { _lastInitializeScriptId = args.Id; MessageBox.Show(_lastInitializeScriptId, "AddScriptToExecuteOnDocumentCreated Id", MessageBoxButton.OK); }); } }