/// <summary> /// 处理 DocumentComplete 事件. /// </summary> /// <param name="pDisp"> /// pDisp 是一个实现了 InternetExplorer 接口的对象. /// 默认情况下,该对象与 ieInstance 相同, 但是如果页面包含多个框架,每一个框 /// 架会有自己的文件. /// </param> void ieInstance_DocumentComplete(object pDisp, ref object URL) { string url = URL as string; if (string.IsNullOrEmpty(url) || url.Equals("about:blank", StringComparison.OrdinalIgnoreCase)) { return; } InternetExplorer explorer = pDisp as InternetExplorer; // 设置 InternetExplorer 内文件的句柄. if (explorer != null) { NativeMethods.ICustomDoc customDoc = (NativeMethods.ICustomDoc)explorer.Document; customDoc.SetUIHandler(openImageDocHostUIHandler); } }
private void ConfigureBrowser() { if (!this.loadCompleted || this.browserConfigured) { return; } this.browserConfigured = true; this.timer.Tick += (sender, args) => { RefreshBrowser(); }; this.WebBrowser.SetScale(scaleLevel: this.EffectiveScale); this.WebBrowser.SetScrollBarVisibility(isVisible: this.IsMaximized); ////this.WebBrowser.SetSilent(silent: !this.IsNavigationEnabled); this.WebBrowser.SetSilent(silent: true); // Disable context menu NativeMethods.ICustomDoc doc = this.WebBrowser.Document as NativeMethods.ICustomDoc; doc.SetUIHandler(new DocHostUIHandler(this.WebBrowser, isBrowserContextMenuEnabled: false)); // Block opening new windows (test -> navigate to msn.com and click on OUTLOOK button) var eventSink = new WebBrowserEventSink(isNewWindowEnabled: false); eventSink.Connect(this.WebBrowser); this.WebBrowser.MouseDown += delegate(object sender, System.Windows.Input.MouseButtonEventArgs e) { e.Handled = !this.IsNavigationEnabled; }; this.WebBrowser.KeyDown += delegate(object sender, System.Windows.Input.KeyEventArgs e) { e.Handled = !IsNavigationEnabled; }; // Block any further navigation this.WebBrowser.Navigating += delegate(object sender, NavigatingCancelEventArgs args) { var isSafelySimilarUri = string.Equals(args.Uri.Host, baselineUri.Host, StringComparison.OrdinalIgnoreCase) && (string.Equals(args.Uri.AbsolutePath, baselineUri.AbsolutePath, StringComparison.OrdinalIgnoreCase)); var isSafeUri = false; foreach (var safePrefixUri in this.SharedPanelConfiguration.SafeUris) { if (args.Uri.ToString().StartsWith(safePrefixUri.ToString())) { isSafeUri = true; } } // Append safe URIs if navigation is enabled if (this.IsNavigationEnabled || isSafelySimilarUri || isSafeUri) { if (this.IsNavigationEnabled && !isSafelySimilarUri && !isSafeUri) { this.AppendTemporaryAllowedUri(args.Uri); } args.Cancel = false; this.Address.Text = args.Uri.ToString(); } else { this.ErrorMessage.Text = $"Blocked navigation to URI: {args.Uri}"; this.errorMessageUris.Add(args.Uri); args.Cancel = true; } this.ErrorPanel.Visibility = errorMessageUris.Count == 0 || this.IsPreviewMode ? Visibility.Collapsed : Visibility.Visible; SetTimerOnNavigation(args.Uri); }; this.WebBrowser.Navigated += delegate(object sender, NavigationEventArgs args) { try { var uri = ((dynamic)this.WebBrowser.Document).url; SetTimerOnNavigation(new Uri(uri)); } catch (Exception) { } }; }
/// <summary> /// Handle the DocumentComplete event. /// </summary> /// <param name="pDisp"> /// The pDisp is an an object implemented the interface InternetExplorer. /// By default, this object is the same as the ieInstance, but if the page /// contains many frames, each frame has its own document. /// </param> void IeInstance_DocumentComplete(object pDisp, ref object URL) { if (ieInstance == null) { return; } // get the url string url = URL as string; if (string.IsNullOrEmpty(url) || url.Equals(@"about:Tabs", StringComparison.OrdinalIgnoreCase) || url.Equals("about:blank", StringComparison.OrdinalIgnoreCase)) { return; } // http://borderstylo.com/posts/115-browser-wars-2-dot-0-the-plug-in SHDocVw.WebBrowser browser = (SHDocVw.WebBrowser)ieInstance; if (browser.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE) { return; } // Set the handler of the document in InternetExplorer. NativeMethods.ICustomDoc customDoc = (NativeMethods.ICustomDoc)ieInstance.Document; customDoc.SetUIHandler(openImageDocHostUIHandler); // sets the document this.document = (HTMLDocument)ieInstance.Document; try { if (this.document.url.Contains(@"thousandpass") || this.document.url.Contains(@"1000pass.com")) { // Mark the add_on as installed! IHTMLElement div1000pass_add_on = this.document.getElementById("1000pass_add_on"); div1000pass_add_on.className = @"installed"; IHTMLElement div1000pass_add_on_version = this.document.getElementById("1000pass_add_on_version"); div1000pass_add_on_version.innerText = VERSION; // Try to save the token string token = div1000pass_add_on.getAttribute("token").ToString(); if (!String.IsNullOrEmpty(token)) { Utils.WriteToFile(Utils.TokenFileName, token); } foreach (IHTMLElement htmlElement in document.getElementsByTagName("IMG")) { if (htmlElement.className == "remote_site_logo") { IHTMLStyle htmlStyle = (IHTMLStyle)htmlElement.style; htmlStyle.cursor = "pointer"; DHTMLEventHandler Handler = new DHTMLEventHandler((IHTMLDocument2)ieInstance.Document); Handler.Handler += new DHTMLEvent(Logo_OnClick); htmlElement.onclick = Handler; htmlElement.setAttribute("alreadyopened", "false", 0); } } } else { // Must run on a thread to guaranty the page has finished loading (js loading) // http://stackoverflow.com/questions/3514945/running-a-javascript-function-in-an-instance-of-internetexplorer System.Threading.ThreadPool.QueueUserWorkItem((o) => { System.Threading.Thread.Sleep(500); try { Thread aThread = new Thread(bind); aThread.SetApartmentState(ApartmentState.STA); aThread.Start(); } catch (Exception ee) { Utils.l(ee); } }, browser); } } catch (Exception e) { Utils.l(e); } }