开发IE浏览器,浏览器帮助对象扩展? [英] Developing Internet Explorer, browser helper object extensions?

查看:475
本文介绍了开发IE浏览器,浏览器帮助对象扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



1)我试图做一个简单的BHO在C#中喜欢这里已经回答了: http://stackoverflow.com/a/5740004/285594



2),但遗憾的是他们都试图小于IE11,一些使工作和一些没有太



<如该答复中提到以下一切后,p> 3),我还购买了官方代码的迹象,但它根本不是在IE11的Windows 7 64位工作。



您可以我下载的Visual Studio 2013版本的准备:包括所有为IE11的源代码和细节:



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



Q.任何人都可以请告知/建议/帮助我怎样才能让这个BHO之一的hello world?



我也试过其他的样品从CodeProject,但是仍然没有人,我是能够使工作然而,试图自4周后,我失去了,请大家指教是什么错在我的ClassLibrary2.rar未高亮显示了文本浏览器?



我完全失去了,请大家指教。



编辑:



IEAddon.cs

 使用系统; 
使用System.Collections.Generic;使用System.Runtime.InteropServices
;使用System.Windows.Forms的
;
使用的Microsoft.Win32;
使用MSHTML;
使用SHDOCVW;

命名空间InternetExplorerExtension
{
[标记有ComVisible特性(真)]
[ClassInterface(ClassInterfaceType.None)
[的Guid(D40C654D-7C51-4EB3 -95B2-1E23905C2A2D)]
[的ProgId(MyBHO.WordHighlighter)]
公共类WordHighlighterBHO:接口IObjectWithSite,IOleCommandTarget
{
常量字符串DefaultTextToHighlight =浏览器;

的IWebBrowser2浏览器;
私有对象的网站;

#地区的高亮文本
无效OnDocumentComplete(对象pDisp,参考目标URL)
{

{

/ /这将防止执行该方法一次以上。
如果(pDisp = this.site!)
的回报;

VAR书2 = browser.Document为的IHTMLDocument2;
变种文件3 = browser.Document为IHTMLDocument3;

变种窗口= document2.parentWindow;
window.execScript(@功能FncAddedByAddon(){警报('留言者添加插件');});

问答LT; IHTMLDOMNode>队列=新的问答LT; IHTMLDOMNode>();
的foreach(IHTMLDOMNode eachChild在document3.childNodes)
queue.Enqueue(eachChild);

,而(queue.Count大于0)
{
//用它的一个突出显示的版本替换所需的文本
VARれ= queue.Dequeue();

VAR textNode =れ为IHTMLDOMTextNode;
如果(textNode!= NULL)
{
如果(textNode.data.Contains(TextToHighlight))
{
VAR newText = textNode.data.Replace(TextToHighlight ,<跨度风格=背景色:黄色;光标:手;'的onclick =JavaScript的:FncAddedByAddon()称号='点击打开基于脚本的警报窗口>中+ TextToHighlight +< /跨度>中);
VAR newNode = document2.createElement(跨度);
newNode.innerHTML = newText;
domNode.replaceNode((IHTMLDOMNode)newNode);
}
}
,否则
{
//将孩子集合
变种X =(IHTMLDOMChildrenCollection)(domNode.childNodes);
的foreach(IHTMLDOMNode eachChild在X)
{
如果(eachChild是mshtml.IHTMLScriptElement)
继续;
如果(eachChild是mshtml.IHTMLStyleElement)
继续;

queue.Enqueue(eachChild);
}
}
}
}
赶上(异常前)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#地区的加载和保存数据
静态字符串TextToHighlight = DefaultTextToHighlight;
公共静态字符串中心数据,=Software\\MyIEExtension;

函数[DllImport(ieframe.dll)]
公共静态外部INT IEGetWriteableHKCU(参考文献的IntPtr phKey);

私有静态无效SaveOptions()
{
//在IE 7,8,9,(台式机)10个标签页,在保护模式
//禁止运行写入HKLM,HKCU。
//必须问IE为可写注册表部分指针
//这将是这样HKU / S-1-7 *** /软件/ AppDataLow /
//在地铁IE 10人模式下,在运行选项卡增强保护模式
//其中BHO是不允许运行,除在边缘情况。
//看到http://blogs.msdn.com/b/ieinternals/archive/2012/03/23/understanding-ie10-enhanced-protected-mode-network-security-addons-cookies-metro-desktop的.aspx
的IntPtr phKey =新的IntPtr();
VAR答案= IEGetWriteableHKCU(REF phKey);
的RegistryKey writeable_registry = RegistryKey.FromHandle(
新Microsoft.Win32.SafeHandles.SafeRegistryHandle(phKey,真)
);
的RegistryKey的RegistryKey = writeable_registry.OpenSubKey(注册中心数据,真实);

如果(的RegistryKey == NULL)
的RegistryKey = writeable_registry.CreateSubKey(注册中心数据);
registryKey.SetValue(数据,TextToHighlight);

writeable_registry.Close();
}
私有静态无效LoadOptions()
{
//在IE 7,8,9,(台式机)10个标签页,在保护模式
//运行哪些禁止写入HKLM,HKCU。
//必须问IE为可写注册表部分指针
//这将是这样HKU / S-1-7 *** /软件/ AppDataLow /
//在地铁IE 10人模式下,在运行选项卡增强保护模式
//其中BHO是不允许运行,除在边缘情况。
//看到http://blogs.msdn.com/b/ieinternals/archive/2012/03/23/understanding-ie10-enhanced-protected-mode-network-security-addons-cookies-metro-desktop的.aspx
的IntPtr phKey =新的IntPtr();
VAR答案= IEGetWriteableHKCU(REF phKey);
的RegistryKey writeable_registry = RegistryKey.FromHandle(
新Microsoft.Win32.SafeHandles.SafeRegistryHandle(phKey,真)
);
的RegistryKey的RegistryKey = writeable_registry.OpenSubKey(注册中心数据,真实);

如果(的RegistryKey == NULL)
的RegistryKey = writeable_registry.CreateSubKey(注册中心数据);
registryKey.SetValue(数据,TextToHighlight);

如果(的RegistryKey == NULL)
{
TextToHighlight = DefaultTextToHighlight;
}
,否则
{
TextToHighlight =(字符串)registryKey.GetValue(数据);
}
writeable_registry.Close();
}
#endregion

[的Guid(6D5140C1-7436-11CE-8034-00AA006009FA)]
[InterfaceType(1)]
公接口的IServiceProvider
{
INT QueryService的(REF的Guid guidService,裁判的Guid RIID,出的IntPtr ppvObject);接口IObjectWithSite
INT IObjectWithSite.SetSite(对象的网站)
{
this.site =网站
}

#地区的实施情况;

如果(!网站= NULL)
{
LoadOptions();

VAR serviceProv =(的IServiceProvider)this.site;
VAR guidIWebBrowserApp = Marshal.GenerateGuidForType(typeof运算(IWebBrowserApp)); //新的GUID(0002DF05-0000-0000-C000-000000000046);
VAR guidIWebBrowser2 = Marshal.GenerateGuidForType(typeof运算(的IWebBrowser2)); //新的GUID(D30C1661-CDAF-11D0-8A3E-00C04FC9E26E);
的IntPtr IntPtr的;
serviceProv.QueryService(REF guidIWebBrowserApp,裁判guidIWebBrowser2,出的IntPtr);

=浏览器(的IWebBrowser2)Marshal.GetObjectForIUnknown(IntPtr的);

((DWebBrowserEvents2_Event)浏览器).DocumentComplete + =
新DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
}
,否则
{
((DWebBrowserEvents2_Event)浏览器).DocumentComplete - =
新DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
浏览器= NULL;
}
返回0;
}
INT IObjectWithSite.GetSite(REF的Guid GUID,出的IntPtr ppvSite)
{
IntPtr的朋克= Marshal.GetIUnknownForObject(浏览器);
INT HR = Marshal.QueryInterface(朋克,楼盘GUID,出ppvSite);
Marshal.Release(朋克);
返回小时;
} $ B IOleCommandTarget
INT IOleCommandTarget.QueryStatus(IntPtr的pguidCmdGroup,UINT cCmds,裁判OLECMD prgCmds,IntPtr的pCmdText)
的$ B #endregion
#地区实施{
返回0;
}
INT IOleCommandTarget.Exec(IntPtr的pguidCmdGroup,UINT nCmdID,UINT nCmdexecopt,IntPtr的pvaIn,IntPtr的pvaOut)
{

{
//访问在命令栏中的文件。
变种文件= browser.Document为的IHTMLDocument2;
变种窗口= document.parentWindow;
VAR的结果= window.execScript(@警报('现在将允许配置来突出显示文本......'););

变种形式=新HighlighterOptionsForm();
form.InputText = TextToHighlight;
如果(form.ShowDialog()= DialogResult.Cancel!)
{
TextToHighlight = form.InputText;
SaveOptions();
}
}
赶上(异常前)
{
MessageBox.Show(ex.Message);
}

返回0;
}
#endregion

#地区与regasm
公共静态字符串RegBHO =Software\\Microsoft\\Windows\\CurrentVersion注册\\Explorer\\Browser辅助对象;
公共静态字符串RegCmd =Software\\Microsoft\\Internet Explorer\\Extensions

[ComRegisterFunction]
公共静态无效RegisterBHO(type类型)
{
字符串GUID = type.GUID.ToString(B);

// BHO
{
的RegistryKey的RegistryKey = Registry.LocalMachine.OpenSubKey(RegBHO,真);
如果(的RegistryKey == NULL)
的RegistryKey = Registry.LocalMachine.CreateSubKey(RegBHO);
的RegistryKey键= registryKey.OpenSubKey(GUID);
如果(键== NULL)
键= registryKey.CreateSubKey(GUID);
key.SetValue(好,1);
registryKey.Close();
key.Close();
}

//命令
{
的RegistryKey的RegistryKey = Registry.LocalMachine.OpenSubKey(RegCmd,真);
如果(的RegistryKey == NULL)
的RegistryKey = Registry.LocalMachine.CreateSubKey(RegCmd);
的RegistryKey键= registryKey.OpenSubKey(GUID);
如果(键== NULL)
键= registryKey.CreateSubKey(GUID);
key.SetValue(ButtonText,荧光笔选项);
key.SetValue(CLSID,{1FBA04EE-3024-11d2-8F1F-0000F87ABD16});
key.SetValue(ClsidExtension,GUID)
key.SetValue(图标,);
key.SetValue(HotIcon,);
key.SetValue(默认可见,是);
key.SetValue(MenuText,&放大器;荧光笔选项);
key.SetValue(工具提示,荧光笔选项);
//key.SetValue(\"KeyPath,无);
registryKey.Close();
key.Close();
}
}

[ComUnregisterFunction]
公共静态无效UnregisterBHO(type类型)
{
字符串GUID = type.GUID.ToString (B);
// BHO
{
的RegistryKey的RegistryKey = Registry.LocalMachine.OpenSubKey(RegBHO,真);
如果(的RegistryKey!= NULL)
registryKey.DeleteSubKey(GUID,FALSE);
}
//命令
{
的RegistryKey的RegistryKey = Registry.LocalMachine.OpenSubKey(RegCmd,真);
如果(的RegistryKey!= NULL)
registryKey.DeleteSubKey(GUID,FALSE);
}
}
#endregion
}
}


解决方案

我试图做同样的 - 我刚刚注意到,在构建日志有错误




未能将装配到缓存:尝试安装一个程序集
没有强大的名称




所以我已经添加* .SNK并强调工作(使用IE11到,64),但'重头戏选项菜单项不工作



<一个HREF =https://www.dropbox.com/s/artu9pvbep8jkz2/IEExtension.7z相对=nofollow> IEExtension例如


1) I am trying to make a simple BHO in C# like here already answered: http://stackoverflow.com/a/5740004/285594

2) But unfortunately they all tried less then IE11, where some made it work and some failed too

3) after following everything as mentioned in that answer, i also purchased official code sign but it simply does not working in IE11 Windows 7 64-bit.

You can download my prepared version of Visual studio 2013: which includes all the source code and details for IE11:

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

Q. Can anyone please advise/suggest/help how can i make one hello world of this BHO?

I have also tried others sample from codeproject, but still none of them i was able to make work yet, trying since 4 weeks, i am lost, please advise what is wrong in my ClassLibrary2.rar which is not hilighting the text "browser"?

I am completely lost, please advise.

EDIT:

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
  }
}

解决方案

i'm trying to do the same - and i've just noticed that in build log there was an error

Failure adding assembly to the cache: Attempt to install an assembly without a strong name

so i've added *.snk and highlighting worked (using ie11 to, x64), but 'highlight options' menu item isn't working

IEExtension example

这篇关于开发IE浏览器,浏览器帮助对象扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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