سؤال

I have following problem: I am writing an extension for internet explorer. In the process i am traversing the DOM and searching for certain keywords. These keywords shall be highlighted and a javascript funktion should be called when clicked. The traversing and highlighting works. But I am stuck with the onclick event. I tried several different approaches:

 IHTMLAttributeCollection attributes = element.attributes;
        if (attributes != null)
        {
            for (int i = 0; i < attributes.length; i++)
            {
                IHTMLDOMAttribute a = attributes.item(i);

                if (a.nodeName.Equals("onclick"))
                {
                    a.nodeValue = "alert()";
                }
            }
        }


element.setAttribute("onclick", "javascript:alert()");

These two create the right text in the DOM but the functionality is not given.

Another approach was to use the innerHTML property. This one worked but it is is really terrible to use because i can not distinguish between the text and new starting.

It seems like IE does not parse the attributes

I am thankful for any support

cheers

هل كانت مفيدة؟

المحلول

The Incredibly useful answer by Miguel Angelo here does the job How to get started with developing Internet Explorer extensions?. I believe he does exactly what you`re trying to do. His BHO plugin highlights the text and inserts the onclick simple javascript function. Good luck with your plugin!

نصائح أخرى

Instead of inline event handler, use attachEvent or addEventListener(for non IE browsers) which are the JavaScript apis for event registration.

if (element.addEventListener) {  // all browsers except IE before version 9
    element.addEventListener("click", function(){ alert('clicked'); });
}
else if (element.attachEvent) {   // IE before version 9
    element.attachEvent("onclick", function(){ alert('clicked'); });
}

I don't think you can set the 'onclick' handler, nor I don't think you can attachEvent, unless the Interop layer is doing magics I'm not aware of.

The native way to get events from IHTMLElement is to QueryInterface for IConnectionPointContainer (everything in COM that supports events implements this interface) and then use the method FindConnectionPoint. The connection point you are looking for has the disp interface ID: DIID_HTMLElementEvents2. Once you get the connection point, you advise (i.e. call the advise method). You give it a call back object (in COM, it is called "sink"), which implements the disp interface IHTMLElementEvents2 - and one of its method which you should implement is on click.

in C# land you will use extensively the interop services (System.Runtime.InteropServices namespace). First, you need to reference the MSHTML (not sure, but I think it has an already interop assembly. Otherwise, just reference from COM). Take a look at this article for connecton point in COM: http://msdn.microsoft.com/en-us/magazine/cc163361.aspx

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top