0

I'm working on a cross-platform webview library that uses webkit (macos/linux) and mshtml (windows). I am now trying to give users a callback that would allow them to "inject" custom javascript code into the page once it is loaded (e.g. to wrap console.log, to pass initial data to the web page and so on).

My current problem is with MSHTML. If I try evaluating javascript right after I call Navigate2 - I get errors, e.g. GetIDsOfNames method on script object fails, which is understandable because it's probably too early. I try to listen to other events, such as DISPID_DOCUMENTCOMPLETE or DISPID_NAVIGATECOMPLETE2, but that arrive too late (e.g. after the scripts on the page are executed, although before window.onload).

I wonder if there is any way to get notified when the page is loaded and DOM is parsed and scripts can be evaluated, but before they are actually evaluated?

zserge
  • 2,212
  • 2
  • 31
  • 40
  • 2
    The assumption that the DOM loads then script is executed isn't correct, for example inline scripts will execute during parsing of their – Alex K. Sep 11 '17 at 09:59
  • @AlexK You're right. Ideally, I would like to be able to inject JS code before any of the inline scripts are executed. Webkit has a signal when window object is cleared. It happens before any script is evaluated and is a perfect hook to prepare the JS environment. – zserge Sep 11 '17 at 10:31
  • 1
    Have you tried listening to [`DownloadComplete`](https://msdn.microsoft.com/en-us/library/aa768331(v=vs.85).aspx) event? I believe this is fired before the parsing stage. – zett42 Sep 11 '17 at 11:36
  • @zett42 Thanks for the suggestion. But still, the first DownloadComplete event arrives too soon, so that `GetIDsOfNames` fail. There is another one (I only have a single html file, so that must be some favicon download) and that arrives too late, after `NavigateComplete2`. Thank you all, but so far it looks to me that there is no perfect solution. – zserge Sep 12 '17 at 15:25

0 Answers0