IE打开新标签页about:空白而不是网页 [英] IE opening new tab about:blank instead of Webpage

查看:79
本文介绍了IE打开新标签页about:空白而不是网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#开发IE的BHO,通过检查onBeforeNavigateEvent中的URL来控制IE的导航流程.

I am developing a BHO for IE in C# that controls the navigation flow of IE by checking the URL in onBeforeNavigateEvent.

一切正常,除了在新标签页中打开链接后,某些标签页以About:Blank(空白)打开.

Everything works fine except that when a link is opened in a new tab then some of the tabs opened as About:Blank.

我已经检查了日志,还调试了BHO,没有抛出异常.但是,在About:Blank BHO已启动的情况下,将调用SetSite和GetSite方法,但不会触发导航事件.

I have checked the logs and also debugged the BHO no exception is thrown. However in the case of About:Blank BHO is initiated SetSite and GetSite methods are called but navigation event is not fired.

当在新标签页中快速打开链接时,也会发生这种情况.

Also it happens when links are opened in new tab rapidly.

出于测试目的,我禁用了该插件,并且IE正常运行,即没有About:空白页面.

For testing purpose I disabled the addon and IE worked fine i.e. no About:Blank page.

BHO的加载时间为0.1s,导航时间为0.5s

Loading time of BHO is 0.1s and Navigation time is 0.5s

那么,可能是什么问题?

So, what could be the issue ?

我遵循的建立此BHO的来源是此处

The source I followed to build this BHO is here

当前环境:IE 11,Windows 10

Current Environment: IE 11, Windows 10

Interop.cs

Interop.cs

using System;
using System.Runtime.InteropServices;

namespace IE_BHO
{
    [
        ComImport(),
        ComVisible(true),
        InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
        Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")
    ]
    interface IObjectWithSite
    {
        [PreserveSig]
        int SetSite([In, MarshalAs(UnmanagedType.IUnknown)] object site);

        [PreserveSig]
        int GetSite(ref Guid guid, out IntPtr ppvSite);
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct OLECMDTEXT
    {
        public uint cmdtextf;
        public uint cwActual;
        public uint cwBuf;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
        public char rgwz;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct OLECMD
    {
        public uint cmdID;
        public uint cmdf;
    }

    [ComImport(), ComVisible(true),
    Guid("B722BCCB-4E68-101B-A2BC-00AA00404770"),
    InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleCommandTarget
    {

        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int QueryStatus(
            [In] IntPtr pguidCmdGroup,
            [In, MarshalAs(UnmanagedType.U4)] uint cCmds,
            [In, Out, MarshalAs(UnmanagedType.Struct)] ref OLECMD prgCmds,
            //This parameter must be IntPtr, as it can be null
            [In, Out] IntPtr pCmdText);

        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int Exec(
            //[In] ref Guid pguidCmdGroup,
            //have to be IntPtr, since null values are unacceptable
            //and null is used as default group!
            [In] IntPtr pguidCmdGroup,
            [In, MarshalAs(UnmanagedType.U4)] uint nCmdID,
            [In, MarshalAs(UnmanagedType.U4)] uint nCmdexecopt,
            [In] IntPtr pvaIn,
            [In, Out] IntPtr pvaOut);
    }

    [Guid("6D5140C1-7436-11CE-8034-00AA006009FA")]
    [InterfaceType(1)]
    public interface IServiceProvider
    {
        int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject);
    }

    [
        ComVisible(true),
        Guid("4C1D2E51-018B-4A7C-8A07-618452573E42"),
        InterfaceType(ComInterfaceType.InterfaceIsDual)
    ]
    public interface IExtension
    {
        [DispId(1)]
        string ActivateEndPoint(string licenseKey, string userAgent);
    }
}

BHO.cs

using Microsoft.Win32;
using SHDocVw;
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Expando;
using System.Security.Permissions;
using System.Threading.Tasks;
using mshtml;
using System.Reflection;
using System.Diagnostics;

namespace IE_BHO
{
    [
        SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode),
        ComVisible(true),
        Guid("BDCB9FDA-8370-40D9-96C9-9D4B4C25C0D8"),
        ClassInterface(ClassInterfaceType.None),
        ProgId("myExtension"),
        ComDefaultInterface(typeof(IExtension))
    ]
    public class BHO : IObjectWithSite, IOleCommandTarget, IExtension
    {
        object _site;
        IWebBrowser2 _webBrowser2;

        #region Implementation of IObjectWithSite
        int IObjectWithSite.SetSite(object site)
        {
            _site = site;

            if (site != null)
            {
                var serviceProv = (IServiceProvider)_site;
                var guidIWebBrowserApp = Marshal.GenerateGuidForType(typeof(IWebBrowserApp));
                var guidIWebBrowser2 = Marshal.GenerateGuidForType(typeof(IWebBrowser2));

                IntPtr intPtr;
                serviceProv.QueryService(ref guidIWebBrowserApp, ref guidIWebBrowser2, out intPtr);

                _webBrowser2 = (IWebBrowser2)Marshal.GetObjectForIUnknown(intPtr);

                ((DWebBrowserEvents2_Event)_webBrowser2).BeforeNavigate2 += OnBeforeNavigate2;
                ((DWebBrowserEvents2_Event)_webBrowser2).BeforeScriptExecute += S2_BeforeScriptExecute;
                ((DWebBrowserEvents2_Event)_webBrowser2).DownloadComplete += BHO_DownloadComplete;                  
            }
            else
            {
                ((DWebBrowserEvents2_Event)_webBrowser2).BeforeNavigate2 -= OnBeforeNavigate2;
                ((DWebBrowserEvents2_Event)_webBrowser2).BeforeScriptExecute -= S2_BeforeScriptExecute;
                ((DWebBrowserEvents2_Event)_webBrowser2).DownloadComplete -= BHO_DownloadComplete;

                _webBrowser2 = null;
            }                

            return 0;
        }

        int IObjectWithSite.GetSite(ref Guid guid, out IntPtr ppvSite)
        {
            IntPtr punk = Marshal.GetIUnknownForObject(_webBrowser2);
            int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
            Marshal.Release(punk);
            return hr;
        }

        public void OnBeforeNavigate2(object pDisp, ref object URL, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
        {
            try
            {
                UrlObj dto = UrlManager.CheckUrl(URL.ToString());

                if (dto.IsInList)
                {
                     Cancel = true;
                     _webBrowser2.Navigate2("www.google.com");
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void S2_BeforeScriptExecute(object pDispWindow)
        {
            ExposeMethodstoJS();
        }

        private void BHO_DownloadComplete()
        {
            ExposeMethodstoJS();
        }

        private void ExposeMethodstoJS(string calledBy)
        {
            try
            {
                HTMLDocument doc = _webBrowser.Document as HTMLDocument;

                if (doc != null)
                {
                    IHTMLWindow2 tmpWindow = doc.parentWindow;
                    dynamic window = tmpWindow;
                    IExpando windowEx = (IExpando)window;
                    PropertyInfo p = windowEx.AddProperty("myExtension");
                    p.SetValue(windowEx, this);
                }
            }
            catch (Exception ex)
            {
            }
        }
        #endregion


        #region Implementation of IOleCommandTarget
        int IOleCommandTarget.QueryStatus(IntPtr pguidCmdGroup, uint cCmds, ref OLECMD prgCmds, IntPtr pCmdText)
        {
            return 0;
        }

        int IOleCommandTarget.Exec(IntPtr pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            return 0;
        }
        #endregion

        #region Implementation of IExtension 
        string IExtension.GetDetails()
        {
            return "Methods Exposed";
        }
        #endregion


        #region COMRegistration
        [ComRegisterFunction]
        public static void RegisterBHO(Type type)
        {
            RegistryKey registryKey = Registry.LocalMachine.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Browser Helper Objects");
            RegistryKey guidKey = registryKey.CreateSubKey(type.GUID.ToString("B"));

            registryKey.Close();
            guidKey.Close();
        }

        [ComUnregisterFunction]
        public static void UnregisterBHO(Type type)
        {
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Browser Helper Objects", true);

            string guid = type.GUID.ToString("B");

            if (registryKey != null)
            {
                registryKey.DeleteSubKey(guid, false);
            }
        }
        #endregion COMRegistration
    }
}

推荐答案

您正在使用哪个版本的IE和OS?

Which version of IE and OS are you using?

我认为出现此问题是因为几乎同时打开并导航了新标签页.有关详细信息,您可以参考

I think the issue occurs because the new tab is opened and navigated at almost the same time. For the detailed information, you could refer to this link.

您还可以尝试以下文章中的解决方案:为Internet Explorer安装最新的累积安全更新.

You could also try the resolution in the article: Install the most recent cumulative security update for Internet Explorer.

这篇关于IE打开新标签页about:空白而不是网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆