Properly detecting the top-level Window / Document when injecting scripts

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

Properly detecting the top-level Window / Document when injecting scripts
Trying to inject a script for gmail, but the script is injected like 5+ times 
Robert Apostolico




Posts: 70
Joined: 2013-09-06
Support Team,

I've followed through most of your examples for injected both CSS and Javascript into web pages.

I'm property detecting that the domain/url is the one we want to work with.
I'm using IEModule HandleHTTPNegotiation and HandleDocumentUICommands set to True.

In the IEModule_OnDataAvailable event, I'm doing this:


            if (!IsTargetDomain(e.Url)) return;
            /////////////////////////////////////

            ////  if this is NOT the top-level main html document, get out of here
            try {
                if ( HTMLDocument.parentWindow.top != HTMLDocument.parentWindow.self ) 
                    return;
            }
            catch (Exception exWindowFail)  {
                System.Diagnostics.Debug.Print (exWindowFail.ToString() );
            }


The concept of window.top !=== window.self >> get out >> came from Safari/Chrome/Opera where it works just fine, and only injects the script once into the primary document.

HOW IS THIS ACCOMPLISHED USING ADXIE ??

Please advise.

REF: Gmail is a site that uses frames/iframes so many documents are pulled down when Gmail loads.

THANK YOU!
BApostolico
Posted 16 May, 2014 15:42:29 Top
Sergey Grischenko


Add-in Express team


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

The current document (HTMLDocument) is not accessible in the OnDataAvailable event. I would advise you to use the DocumentComplete event instead.
Posted 19 May, 2014 05:24:35 Top
Robert Apostolico




Posts: 70
Joined: 2013-09-06
Sergey,

Please check with Andrei with regard to this:
http://www.add-in-express.com/forum/read.php?FID=10&TID=12375

I'm trying to solve the same problem, in different ways from both these posts.

I need to find the difference between when/why I'd hook into the OnDataAvailable event vs the DocumentComplete event and how, if I use the DocumentComplete event to inject code after the browser has rendered the page, how do I get my injected code to execute?

THANK YOU!
BApostolico
Posted 19 May, 2014 08:02:13 Top
Sergey Grischenko


Add-in Express team


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

If you need to change the HTML source code of a web page before the page is rendered in IE, you need to use OnDataAvailable (e.g. you can change javascript or urls of some iframes, images and so on). But if you want to access the HTMLDocument object, you need to use DocumentComplete or DocumentComplite2 events. If you need to inject the code into the main document (not iframe), you can use DocumentComplite2. In this case 'rootDocLoaded' will be true.
To run injected script you can use 'execScript' method of IE.

HTMLDocument.parentWindow.execScript("MyFunction();");
Posted 19 May, 2014 08:57:11 Top