【问题标题】:Developing Internet Explorer, browser helper object extensions?开发 Internet Explorer、浏览器助手对象扩展?
【发布时间】:2014-05-22 03:00:20
【问题描述】:

1) 我正在尝试用 C# 制作一个简单的 BHO,就像这里已经回答的那样:https://stackoverflow.com/a/5740004/285594

2) 但不幸的是,他们尝试的次数都少于 IE11,有些成功,有些也失败了

3)按照该答案中提到的所有内容进行操作后,我还购买了官方代码符号,但它根本无法在 IE11 Windows 7 64 位中运行。

您可以下载我准备好的 Visual Studio 2013 版本:其中包含 IE11 的所有源代码和详细信息:

https://www.dropbox.com/s/60kg212vkjb7yud/ClassLibrary2.rar

问。任何人都可以请建议/建议/帮助我如何让这个 BHO 成为一个你好世界?

我也尝试过 codeproject 中的其他示例,但我仍然无法完成工作,尝试了 4 周,我迷路了,请告知我的 ClassLibrary2.rar 中没有突出显示文本的问题“浏览器”?

我完全迷路了,请指教。

编辑:

IEAddon.cs

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;
using mshtml;
using SHDocVw;

namespace InternetExplorerExtension
{
  [ComVisible(true)]
  [ClassInterface(ClassInterfaceType.None)]
  [Guid("D40C654D-7C51-4EB3-95B2-1E23905C2A2D")]
  [ProgId("MyBHO.WordHighlighter")]
  public class WordHighlighterBHO : IObjectWithSite, IOleCommandTarget
  {
    const string DefaultTextToHighlight = "browser";

    IWebBrowser2 browser;
    private object site;

    #region Highlight Text
    void OnDocumentComplete(object pDisp, ref object URL)
    {
      try
      {

        // This will prevent this method being executed more than once.
        if (pDisp != this.site)
          return;

        var document2 = browser.Document as IHTMLDocument2;
        var document3 = browser.Document as IHTMLDocument3;

        var window = document2.parentWindow;
        window.execScript(@"function FncAddedByAddon() { alert('Message added by addon.'); }");

        Queue<IHTMLDOMNode> queue = new Queue<IHTMLDOMNode>();
        foreach (IHTMLDOMNode eachChild in document3.childNodes)
          queue.Enqueue(eachChild);

        while (queue.Count > 0)
        {
          // replacing desired text with a highlighted version of it
          var domNode = queue.Dequeue();

          var textNode = domNode as IHTMLDOMTextNode;
          if (textNode != null)
          {
            if (textNode.data.Contains(TextToHighlight))
            {
              var newText = textNode.data.Replace(TextToHighlight, "<span style='background-color: yellow; cursor: hand;' onclick='javascript:FncAddedByAddon()' title='Click to open script based alert window.'>" + TextToHighlight + "</span>");
              var newNode = document2.createElement("span");
              newNode.innerHTML = newText;
              domNode.replaceNode((IHTMLDOMNode)newNode);
            }
          }
          else
          {
            // adding children to collection
            var x = (IHTMLDOMChildrenCollection)(domNode.childNodes);
            foreach (IHTMLDOMNode eachChild in x)
            {
              if (eachChild is mshtml.IHTMLScriptElement)
                continue;
              if (eachChild is mshtml.IHTMLStyleElement)
                continue;

              queue.Enqueue(eachChild);
            }
          }
        }
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
      }
    }
    #endregion
    #region Load and Save Data
    static string TextToHighlight = DefaultTextToHighlight;
    public static string RegData = "Software\\MyIEExtension";

    [DllImport("ieframe.dll")]
    public static extern int IEGetWriteableHKCU(ref IntPtr phKey);

    private static void SaveOptions()
    {
      // In IE 7,8,9,(desktop)10 tabs run in Protected Mode
      // which prohibits writes to HKLM, HKCU.
      // Must ask IE for "Writable" registry section pointer
      // which will be something like HKU/S-1-7***/Software/AppDataLow/
      // In "metro" IE 10 mode, tabs run in "Enhanced Protected Mode"
      // where BHOs are not allowed to run, except in edge cases.
      // see http://blogs.msdn.com/b/ieinternals/archive/2012/03/23/understanding-ie10-enhanced-protected-mode-network-security-addons-cookies-metro-desktop.aspx
      IntPtr phKey = new IntPtr();
      var answer = IEGetWriteableHKCU(ref phKey);
      RegistryKey writeable_registry = RegistryKey.FromHandle(
          new Microsoft.Win32.SafeHandles.SafeRegistryHandle(phKey, true)
      );
      RegistryKey registryKey = writeable_registry.OpenSubKey(RegData, true);

      if (registryKey == null)
        registryKey = writeable_registry.CreateSubKey(RegData);
      registryKey.SetValue("Data", TextToHighlight);

      writeable_registry.Close();
    }
    private static void LoadOptions()
    {
      // In IE 7,8,9,(desktop)10 tabs run in Protected Mode
      // which prohibits writes to HKLM, HKCU.
      // Must ask IE for "Writable" registry section pointer
      // which will be something like HKU/S-1-7***/Software/AppDataLow/
      // In "metro" IE 10 mode, tabs run in "Enhanced Protected Mode"
      // where BHOs are not allowed to run, except in edge cases.
      // see http://blogs.msdn.com/b/ieinternals/archive/2012/03/23/understanding-ie10-enhanced-protected-mode-network-security-addons-cookies-metro-desktop.aspx
      IntPtr phKey = new IntPtr();
      var answer = IEGetWriteableHKCU(ref phKey);
      RegistryKey writeable_registry = RegistryKey.FromHandle(
          new Microsoft.Win32.SafeHandles.SafeRegistryHandle(phKey, true)
      );
      RegistryKey registryKey = writeable_registry.OpenSubKey(RegData, true);

      if (registryKey == null)
        registryKey = writeable_registry.CreateSubKey(RegData);
      registryKey.SetValue("Data", TextToHighlight);

      if (registryKey == null)
      {
        TextToHighlight = DefaultTextToHighlight;
      }
      else
      {
        TextToHighlight = (string)registryKey.GetValue("Data");
      }
      writeable_registry.Close();
    }
    #endregion

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

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

      if (site != null)
      {
        LoadOptions();

        var serviceProv = (IServiceProvider)this.site;
        var guidIWebBrowserApp = Marshal.GenerateGuidForType(typeof(IWebBrowserApp)); // new Guid("0002DF05-0000-0000-C000-000000000046");
        var guidIWebBrowser2 = Marshal.GenerateGuidForType(typeof(IWebBrowser2)); // new Guid("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E");
        IntPtr intPtr;
        serviceProv.QueryService(ref guidIWebBrowserApp, ref guidIWebBrowser2, out intPtr);

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

        ((DWebBrowserEvents2_Event)browser).DocumentComplete +=
            new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
      }
      else
      {
        ((DWebBrowserEvents2_Event)browser).DocumentComplete -=
            new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
        browser = null;
      }
      return 0;
    }
    int IObjectWithSite.GetSite(ref Guid guid, out IntPtr ppvSite)
    {
      IntPtr punk = Marshal.GetIUnknownForObject(browser);
      int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
      Marshal.Release(punk);
      return hr;
    }
    #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)
    {
      try
      {
        // Accessing the document from the command-bar.
        var document = browser.Document as IHTMLDocument2;
        var window = document.parentWindow;
        var result = window.execScript(@"alert('You will now be allowed to configure the text to highlight...');");

        var form = new HighlighterOptionsForm();
        form.InputText = TextToHighlight;
        if (form.ShowDialog() != DialogResult.Cancel)
        {
          TextToHighlight = form.InputText;
          SaveOptions();
        }
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
      }

      return 0;
    }
    #endregion

    #region Registering with regasm
    public static string RegBHO = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";
    public static string RegCmd = "Software\\Microsoft\\Internet Explorer\\Extensions";

    [ComRegisterFunction]
    public static void RegisterBHO(Type type)
    {
      string guid = type.GUID.ToString("B");

      // BHO
      {
        RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(RegBHO, true);
        if (registryKey == null)
          registryKey = Registry.LocalMachine.CreateSubKey(RegBHO);
        RegistryKey key = registryKey.OpenSubKey(guid);
        if (key == null)
          key = registryKey.CreateSubKey(guid);
        key.SetValue("Alright", 1);
        registryKey.Close();
        key.Close();
      }

      // Command
      {
        RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(RegCmd, true);
        if (registryKey == null)
          registryKey = Registry.LocalMachine.CreateSubKey(RegCmd);
        RegistryKey key = registryKey.OpenSubKey(guid);
        if (key == null)
          key = registryKey.CreateSubKey(guid);
        key.SetValue("ButtonText", "Highlighter options");
        key.SetValue("CLSID", "{1FBA04EE-3024-11d2-8F1F-0000F87ABD16}");
        key.SetValue("ClsidExtension", guid);
        key.SetValue("Icon", "");
        key.SetValue("HotIcon", "");
        key.SetValue("Default Visible", "Yes");
        key.SetValue("MenuText", "&Highlighter options");
        key.SetValue("ToolTip", "Highlighter options");
        //key.SetValue("KeyPath", "no");
        registryKey.Close();
        key.Close();
      }
    }

    [ComUnregisterFunction]
    public static void UnregisterBHO(Type type)
    {
      string guid = type.GUID.ToString("B");
      // BHO
      {
        RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(RegBHO, true);
        if (registryKey != null)
          registryKey.DeleteSubKey(guid, false);
      }
      // Command
      {
        RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(RegCmd, true);
        if (registryKey != null)
          registryKey.DeleteSubKey(guid, false);
      }
    }
    #endregion
  }
}

【问题讨论】:

  • 不确定它是否有效,但我发现了这个:add-in-express.com/programming-internet-explorer/…
  • 请看我的编辑。您可以在哪里看到显示我们的 BHO 在 IE11 中的屏幕截图,但页面没有显示任何包含“浏览器”的文本亮点,这是我的问题。
  • 也许不是让我们尝试从截取的图像中读取代码,或者告诉我们下载它,您可以在这里发布您的代码,有人可以帮助您?
  • 请查看我的编辑部分 IEAddon.cs 的代码(如果有人需要,整个项目也可以在该下载链接中获得)
  • 正如您在代码中看到的那样,当页面加载时它的mentioend 并且在页面中我们有一个文本“浏览器”,它将用黄色包裹它并显示一个手形图标,这不会发生,正如你在图片中看到的那样,BHO 是在 IE11 中加载的

标签: c# c++ internet-explorer atl browser-extension


【解决方案1】:

我正在尝试做同样的事情 - 我刚刚注意到在构建日志中有一个错误

将程序集添加到缓存失败:尝试安装程序集 没有一个强大的名字

所以我添加了 *.snk 并突出显示(使用 ie11 到 x64),但“突出显示选项”菜单项不起作用

IEExtension example

【讨论】:

  • 重建后,“突出显示选项”似乎也可以正常工作
  • 哇-你是怎么做到的,请分享让我们一起做你在聊天吗?
  • 只是尝试向程序集添加签名-项目选项->签名选项卡->选中签名程序集->在选择框中选择新建->取消选中使用密码保护-仅此而已。然后只是 rebild - gacutil 现在可以正常工作(检查构建输出 - 它应该说 Assembly 已成功添加到缓存)
  • 我在 Visual Studio 2013 > 项目属性 > 签名选项卡 > 复选标记为选择的密钥文件的程序集签名 > 未选择密码选项。并构建/编译。我可以在 IE11 中看到 BHO,但它还没有工作,页面中根本没有高亮任何文本。请指教。
  • IEExtension - 这在我的机器上工作。你应该更加小心命名空间和程序集名称
【解决方案2】:

虽然你的 IE11 运行在 64 位 Windows 上,但默认的 IE 实例是 32 位版本。需要开启增强保护模式,IE11才能在64位模式下运行。

另一个技巧是 32 位 IE,你必须注册 32 位扩展,反之亦然 64 位。我的建议如下:

  • 确保您的 IE11 模式是 32 位或 64 位
  • 只注册 32bit 或 64bit 扩展,如果两者都注册,扩展也不能工作。您必须仔细检查您的注册表以删除一个不必要的

【讨论】:

    【解决方案3】:

    我热烈地向您推荐 Pavel Zolnikov 2002 年发表的这篇文章!

    http://www.codeproject.com/Articles/2219/Extending-Explorer-with-Band-Objects-using-NET-and

    它基于 Band 对象的使用,使用 .Net 2.0 编译。 正如您将在帖子 cmets 中看到的那样,它在 IE 11 以及 Windows 7 和 Windows 10 上运行良好。 提供了教程源代码,并且可以使用 Visual Studio 2013 很好地打开和编译。 享受吧!

    【讨论】:

    • 谢谢,请不要引用链接到链接。请制作一个完整的工作项目并将步骤 1、2、3 放入,以便它可以帮助所有其他研究人员。从一个链接到另一个链接非常混乱。
    猜你喜欢
    • 1970-01-01
    • 2017-04-18
    • 2013-07-11
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多