System.UnauthorizedAccessException when injecting CSS

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

System.UnauthorizedAccessException when injecting CSS
 
SilverStr




Posts: 28
Joined: 2013-03-31
Hey guys,

My Addin is throwing "System.UnauthorizedAccessException" when I try to inject some CSS on DocumentComplete for a website (http://ecomm.dell.com/myaccount/login.aspx) and I think its because of code they have in their own Javascript.

Basically what I am doing is looping through each frame on the page and injecting a new stylesheet with my changes using the following code:


int numFrames = doc.frames.length;

for (int x = 0; x < numFrames; x++)
{
    var frame = HTMLDocument.frames.item(x);
    styleSheet = ((mshtml.HTMLWindow2)frame).document.createStyleSheet("", 0);
    styleSheet.cssText = ".new-style { some_style_stuff; }";
}


It blows when I call createStyleSheet().

Can you tell me how I can properly inject a stylesheet in this sort of condition? I thought maybe NavigateComplete2() would be early enough in the sequence to allow injection, but it doesn't seem to work. Was wondering if you could take a look and give me your recommendations.
Posted 13 Apr, 2013 20:45:55 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Dana,

Please try to use the root document to create your own CSS.
Posted 15 Apr, 2013 05:33:29 Top
SilverStr




Posts: 28
Joined: 2013-03-31
So what is the right way to get the root document inside the iframe? I would have thought having the frame.document would suffice for the injection.

Currently I inject to the main doc and each iframe. Looks like this:


public void InjectHoverStyleSheet( mshtml.HTMLDocument doc)
{
    mshtml.IHTMLStyleSheet styleSheet = doc.createStyleSheet("", 0);
    styleSheet.cssText = ".hover-style { outline: 1px solid #0000DD !important;}";

    int numFrames = doc.frames.length;

    for (int x = 0; x < numFrames; x++)
    {
        var frame = HTMLDocument.frames.item(x);
        styleSheet = ((mshtml.HTMLWindow2)frame).document.createStyleSheet("", 0);
        styleSheet.cssText = ".hover-style { outline: 1px solid #0000DD !important;}";
    }
}


Its throwing the exception in that frame, not on the outside doc.

What am I missing?
Goal is to draw a border around elements across the parent doc or any content in the iframe.
Posted 16 Apr, 2013 18:59:55 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Dana,

Please try the code below:


        private void IEModule_DocumentComplete2(object pDisp, string url, bool rootDocLoaded)
        {
            try
            {
                if (rootDocLoaded)
                {
                    mshtml.IHTMLStyleSheet styleSheet = this.HTMLDocument.createStyleSheet("", 0);
                    styleSheet.cssText = ".hover-style { outline: 1px solid #0000DD !important;}";
                }
                else
                {
                    object[] frameList = ADXIEHelper.GetFrames(this.HTMLDocument);
                    if (frameList.Length > 0)
                    {
                        object frameDoc;
                        mshtml.IHTMLStyleSheet styleSheet;

                        foreach (object frame in frameList)
                        {
                            frameDoc = ADXIEHelper.GetFrameDocument(frame);
                            if (frameDoc is mshtml.IHTMLDocument2)
                            {
                                styleSheet = (frameDoc as mshtml.IHTMLDocument2).createStyleSheet("", 0);
                                styleSheet.cssText = ".hover-style { outline: 1px solid #0000DD !important;}";
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(this, err.Message, this.ModuleName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Posted 17 Apr, 2013 04:57:34 Top