Having trouble getting the source element in IE

Add-in Express™ Support Service
That's what is more important than anything else

Having trouble getting the source element in IE
 
Chris LoVerme




Posts: 4
Joined: 2008-09-03
Hi,

I'm having trouble locating the source element from the webpage that I'm trying to interact with. What i'm trying to do is paste text from the clipboard into *any* text box on a webpage from a custom context menu. The example code below only works on pages where elements have been identified as input and text fields. I've noticed some webpages don't always explicitly call them out. My research has turned up a way to grab the source element with a "srcElement" or "GetElementFromPoint" but I don't know enough about C# to use them correctly witht he add-in. Any help would be appreciated. Also, is there another way to force the "paste" command to fire?





mshtml.IHTMLElementCollection elementCollection = HTMLDocument.getElementsByTagName("input"); 
for (int i = 0; i > elementCollection.length; i++) 
{ 
    mshtml.IHTMLInputElement inputElement = elementCollection.item(i, i) as mshtml.IHTMLInputElement; 
    if (inputElement!=null && inputElement.type == "text") 
    { 
        if ((bool)inputElement.status == true) 
        { 
             inputElement.value = "test"; 
        } 
        else 
        { 
             inputElement.value = "test..."; 
        } 
        Marshal.ReleaseComObject(inputElement); 
     } 
} 
if (elementCollection != null) Marshal.ReleaseComObject(elementCollection);


Posted 27 Nov, 2008 17:20:31 Top
Eugene Astafiev


Add-in Express team


Posts: 8540
Joined: 2007-07-25
Hello Chris,

My research has turned up a way to grab the source element with a "srcElement" or "GetElementFromPoint" but I don't know enough about C# to use them correctly witht he add-in.


Where did you find the GetElementFromPoint function?
.NET, Extended MAPI, Windows Phone & RT
Posted 28 Nov, 2008 05:07:31 Top
Chris LoVerme




Posts: 4
Joined: 2008-09-03
Hi Eugene,

I located it on MSDN at http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementfrompoint(VS.80).aspx

There's also an example in vb and c#. This might be barking up the wrong tree though, but I figured it was worth a look.



private void Document_Click(Object sender, HtmlElementEventArgs e)
{
    if (webBrowser1.Document != null)
    {
        HtmlElement elem = webBrowser1.Document.GetElementFromPoint(webBrowser1.Document.Window.Position);
        elem.ScrollIntoView(true);
    }
}
Posted 28 Nov, 2008 13:13:46 Top
Eugene Astafiev


Add-in Express team


Posts: 8540
Joined: 2007-07-25
Hi Chris,

Please note that you found the HtmlDocument class in the System.Windows.Forms (in System.Windows.Forms.dll) namespace.

Please try using the following code:


mshtml.IHTMLInputElement inputElement = elementCollection.item(i, i) as mshtml.IHTMLInputElement;
if (inputElement!=null)
{
    if ((bool)inputElement.status == true)
    {
        inputElement.value = "test";
    }
    else
    {
        inputElement.value = "test...";
    }
    Marshal.ReleaseComObject(inputElement);
}
.NET, Extended MAPI, Windows Phone & RT
Posted 29 Nov, 2008 07:43:36 Top