0

I've scoured the web for examples of a Browser Helper object that executes JS and have gotten this far but the JS doesn't seem to execute. Code is below and I've also run regasm.exe codebase/ on the dll but nothing seems to happen when I load the extension in the browser - any suggestions?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SHDocVw;
using mshtml;
using System.IO;
using Microsoft.Win32;
using System.Runtime.InteropServices;

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

        [PreserveSig]
        int GetSite(ref Guid guid, out IntPtr ppvSite);
    }
    [
        ComVisible(true),
        Guid("2159CB25-EF9A-54C1-B43C-E30D1A4A8277"),
        ClassInterface(ClassInterfaceType.None)
    ]

    public class BHO : IObjectWithSite
    {
        private WebBrowser webBrowser;
        public const string BHO_REGISTRY_KEY_NAME = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";

        public int SetSite(object site)
        {
            if (site != null)
            {
                webBrowser = (WebBrowser)site;
                webBrowser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
            }
            else
            {
                webBrowser.DocumentComplete -= new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
                webBrowser = null;
            }
            return 0;
        }

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

        public void OnDocumentComplete(object pDisp, ref object URL)
        {
            HTMLDocument document = (HTMLDocument)webBrowser.Document;
            IHTMLElement head = (IHTMLElement)((IHTMLElementCollection)document.all.tags("head")).item(null, 0);
            IHTMLScriptElement adaptiveScript = (IHTMLScriptElement)document.createElement("script");
            adaptiveScript.type = @"text/javascript";
            adaptiveScript.text = "alert('hi');";
            ((HTMLHeadElement)head).appendChild((IHTMLDOMNode)adaptiveScript);
        }

        [ComRegisterFunction]
        public static void RegisterBHO(Type type)
        {
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHO_REGISTRY_KEY_NAME, true);
            if (registryKey == null) registryKey = Registry.LocalMachine.CreateSubKey(BHO_REGISTRY_KEY_NAME);

            string guid = type.GUID.ToString("B");
            RegistryKey ourKey = registryKey.OpenSubKey(guid, true);
            if (ourKey == null) ourKey = registryKey.CreateSubKey(guid);
            ourKey.SetValue("NoExplorer", 1, RegistryValueKind.DWord);

            registryKey.Close();
            ourKey.Close();
        }

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

}

I've mainly been trying to follow this tutorial but would appreciate any other more recent examples! https://www.codeproject.com/Articles/149258/Inject-HTML-and-JavaScript-into-an-existing-page-w

Maybe something with the GUIDs is off?

Thanks in advance!

McD
  • 173
  • 2
  • 8
  • 1
    I followed the tutorial and the add-on seems incompatible with IE 11: https://i.stack.imgur.com/dMxXh.png. You could check the info of your add-on in IE. You could also try to disable Enhanced Protected Mode to see if the add-on can work. Besides, I found [a more recent example](https://stackoverflow.com/questions/46003656/cannot-get-bho-working-in-64-bit/46204713#46204713) which you could refer to. – Yu Zhou Jan 21 '20 at 08:38
  • ActiveX control support was [severely restricted in IE11](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/samples/hh968248(v=vs.85)?redirectedfrom=MSDN#using-plug-ins-as-a-last-resort), in order to move people away from ActiveX controls and BHOs. You have to add an `x-ua-compatible` meta tag to the page, which only works in IE for the desktop. (They don't work at all in Edge.) These days, you're expected to develop browser addons as [Chrome extensions](https://learn.microsoft.com/en-us/microsoft-edge/extensions/guides/creating-an-extension). – Lance Leonard Jan 22 '20 at 22:38
  • Thank you both! @YuZhou that newer example (and fiddling around with the arcane IE11 settings) worked! – McD Jan 29 '20 at 17:30
  • 1
    It seems that you have solved the issue. You could also put your solution as an answer and mark it as an accepted answer after 48 hrs, when it is available to mark. It can help other community members in future in similar kind of issues. Thanks for your understanding. – Yu Zhou Jan 31 '20 at 07:48

0 Answers0