Cannot inject the script

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

Cannot inject the script
 
Eugene Mednikov




Posts: 2
Joined: 2012-06-30
There is the code that I use to test IE button:

private void adxieCommandItem1_OnClick(object sender, object htmlDoc)
{
MessageBox.Show("test");

var head = HTMLDocument.getElementsByTagName("head").item(null, 0) as IHTMLElement;
if (head == null)
MessageBox.Show("noe head");
else
{
var head2 = head as HTMLHeadElementClass;
if (head2 == null)
MessageBox.Show("no head 2");
else
{
var el = HTMLDocument.createElement("script") as IHTMLScriptElement;
el.src = @"//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js";
head2.appendChild((IHTMLDOMNode)el);
}
}
}

What happens is that after clicking at the button at any page, I get "test", then "no head 2". Thus I cannot inject script into the page.

I've used standard template with Command item.

Please assist.
Posted 30 Jun, 2012 14:21:06 Top
Eugene Astafiev


Add-in Express team


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

Thank you for providing me with a snippet of code.

var head2 = head as HTMLHeadElementClass;


Please try to cast the head object to the IHTMLHeaderElement interface instead. Does it help?

Also please take a look at the HowTo: get the actual type name behind System.__ComObject with Visual C# or VB.NET article on our technical blog.
.NET, Extended MAPI, Windows Phone & RT
Posted 02 Jul, 2012 06:16:53 Top
Eugene Mednikov




Posts: 2
Joined: 2012-06-30
1. type of the head variable is mshtml.HTMLHeadElementClass

2. var head2 = (HTMLHeadElementClass)head produces following exception:
Unable to cast COM object of type 'mshtml.HTMLHeadElementClass' to class type 'mshtml.HTMLHeadElementClass'. Instances of types that represent COM components cannot be cast to different types that represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

3. var head2 = (IHTMLHeaderElement)head produces following exception:
Unable to cast COM object of type 'mshtml.HTMLHeadElementClass' to interface type 'mshtml.IHTMLHeaderElement'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{3050F1F6-98B5-11CF-BB82-00AA00BDCE0B}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Posted 02 Jul, 2012 16:57:15 Top
Eugene Astafiev


Add-in Express team


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

Unfortunately I wasn't able to reproduce the issue on my PC with Internet Explorer 9 installed. I used the following code for testing:

private void adxieCommandItem1_OnClick(object sender, object htmlDoc)
{
    MessageBox.Show("test");

    var head = HTMLDocument.getElementsByTagName("head").item(null, 0) as mshtml.IHTMLElement;
    if (head == null)
       MessageBox.Show("noe head");
    else
    {
        var head2 = head as mshtml.HTMLHeadElement;
        // var head2 = head as mshtml.IHTMLHeadElement; - works too
        if (head2 == null)
           MessageBox.Show("no head 2");
        else
        {   
           var el = HTMLDocument.createElement("script") as mshtml.IHTMLScriptElement;
           el.src = @"//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js";
           head2.appendChild((mshtml.IHTMLDOMNode)el);
        }
    } 
}


Could you please prepare a sample add-on project which can reproduce the issue and then send it to the support e-mail address (see readme.txt)?
.NET, Extended MAPI, Windows Phone & RT
Posted 04 Jul, 2012 10:38:10 Top