OnRequest being called before DocumentComplete

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

OnRequest being called before DocumentComplete
There are OnRequest calls to 3rd party site called before DocumentComplete 
George Kierstein




Posts: 47
Joined: 2013-05-04
Hi,

I'm working on an add-on that blocks requests by injecting javascript which gets called when requests are made. I've gotten all the hooks working but have noticed some cases where requests to 3rd party URLs (not the page being loaded) sneak out before OnDocumentComplete is called.

Is this typical ? What is the timing of javascript executed in the page relative to the OnDocumentComplete being called ?

Thanks
G
Posted 13 May, 2013 16:35:50 Top
George Kierstein




Posts: 47
Joined: 2013-05-04
Any thoughts on how to approach this ?

Best
G
Posted 14 May, 2013 22:10:44 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi George,

Actually it depends on the script being injected into the web page. E.g. the script below is executed right after you inject it.

<script type="text/javascript">
function SayHello() {
alert('Hello');
};
SayHello();
</script>
Posted 15 May, 2013 05:28:28 Top
George Kierstein




Posts: 47
Joined: 2013-05-04
The script is called by the add-on when onRequest is received and performs some logic to decide if the request should be allowed.

Ideally I want to monitor and potentially block/redirect all requests that a page makes as its loading. Since its a passive function that is waiting on page events that the BHO receives there isn't anything to run when its first injected (apart from a call to localStorage).

Given your answer and the relative timing of when these initial requests are made I can always just create a list of them, stop them from happening and wait for the script to inject before dealing with them but it would be great if I could get the script into the page before they occur if possible.
Posted 17 May, 2013 11:59:55 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi George,

In this case you need to use the OnDataAvailable event in order to inject the script before the web page is completely loaded.
Posted 20 May, 2013 09:57:54 Top
George Kierstein




Posts: 47
Joined: 2013-05-04
Hi,

When I set up the callback I get the ADXIEDataAvailableEventArgs parameter. I've looked in the docs but there are no examples on how to use it. When it says that the Data member is a writable stream does this mean that I have to parse this stream and write back to it if I wanted to inject something ?

At this point I just want to ensure that the page has a HTML 5 DocType but adding it if its missing - Is this the right way to go about this or is there an easier way ?
Posted 19 Jun, 2013 12:24:09 Top
George Kierstein




Posts: 47
Joined: 2013-05-04
Huh. I have HandleHttp turned on. I get the callback and the args but when I try to simply print out the contents via:


MemoryStream stream = e.Data;
stream.Position = 0;
var sr = new StreamReader(stream);
string myStr = sr.ReadToEnd();
 MessageBox.Show("Contents=" + myStr);


Its always empty.

Thoughts ?
Posted 19 Jun, 2013 21:33:48 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi George,

Please look at the 'How to modify HTML source code before it is rendered in IE' example:
http://www.add-in-express.com/support/ie-vbnet-csharp.php
Posted 20 Jun, 2013 06:52:50 Top