How to inject javascript into webbrowser control
-
Hello, I have a dialog based MFC application with embedded webbrowser control. I need to load an url and then inject my javascript when page is loaded. I am able to do this by using insertAdjacentHTML function or pasteHTML (with regions) and injecting deferred script. But the problem is when this injected script is trying to do document.write into the page, it seems to erase all content of the document. What could be a problem there? Also, injected script does not work if I inject script only, it is always necessary to add some "garbade" before script, some extra characters. This is my source code:
long rs = m_web01.get_ReadyState(); if (4 == rs) { MSHTML::IHTMLDocument2Ptr spDoc2 = m_web01.get_Document(); MSHTML::IHTMLBodyElementPtr spBody = spDoc2->body; MSHTML::IHTMLElementPtr spBodElem = spBody; spBodElem->insertAdjacentHTML(_bstr_t("beforeEnd"), _bstr_t(strInjection)); }
Also, there is another problem, sometimes webbrowser just hangs when trying to download a page, for example google finance homepage page will hang it almost for sure. There is an article in KB saying that there is a problem with setting focus if control is embedded into the formview, but I could not find an universal solution, my control embedded into property page, so how do I handle this? -
Hello, I have a dialog based MFC application with embedded webbrowser control. I need to load an url and then inject my javascript when page is loaded. I am able to do this by using insertAdjacentHTML function or pasteHTML (with regions) and injecting deferred script. But the problem is when this injected script is trying to do document.write into the page, it seems to erase all content of the document. What could be a problem there? Also, injected script does not work if I inject script only, it is always necessary to add some "garbade" before script, some extra characters. This is my source code:
long rs = m_web01.get_ReadyState(); if (4 == rs) { MSHTML::IHTMLDocument2Ptr spDoc2 = m_web01.get_Document(); MSHTML::IHTMLBodyElementPtr spBody = spDoc2->body; MSHTML::IHTMLElementPtr spBodElem = spBody; spBodElem->insertAdjacentHTML(_bstr_t("beforeEnd"), _bstr_t(strInjection)); }
Also, there is another problem, sometimes webbrowser just hangs when trying to download a page, for example google finance homepage page will hang it almost for sure. There is an article in KB saying that there is a problem with setting focus if control is embedded into the formview, but I could not find an universal solution, my control embedded into property page, so how do I handle this? -
Alexander Fedorov wrote:
when this injected script is trying to do document.write into the page, it seems to erase all content of the document.
Isn't that what document.write does?
led mike
No, I dont think so. This page will say "Hello world!" not just "world!":
<html> <body> Hello <script type="text/javascript"> document.write('world!'); </script> </body> </html>
But if I inject similar code into control, it will just say "world!". This happens because script is deferred, but this is what I am trying to do, I need script to be able to execute but not to erase previous content. -
No, I dont think so. This page will say "Hello world!" not just "world!":
<html> <body> Hello <script type="text/javascript"> document.write('world!'); </script> </body> </html>
But if I inject similar code into control, it will just say "world!". This happens because script is deferred, but this is what I am trying to do, I need script to be able to execute but not to erase previous content.Alexander Fedorov wrote:
No, I dont think so. This page will say "Hello world!" not just "world!":
Of course because the script is inline and run at that point when the browser is loading the page but that's not what you are doing because you are injecting the script after the page is finished loading. Your scenario is more like this script which will only display "world"
<head>
<title>Untitled Page</title>
<script type="text/javascript">
function loadPage()
{
document.write(" world");
}
</script>
</head>
<body onload="loadPage();">
hello
</body>
</html>led mike
-
Alexander Fedorov wrote:
No, I dont think so. This page will say "Hello world!" not just "world!":
Of course because the script is inline and run at that point when the browser is loading the page but that's not what you are doing because you are injecting the script after the page is finished loading. Your scenario is more like this script which will only display "world"
<head>
<title>Untitled Page</title>
<script type="text/javascript">
function loadPage()
{
document.write(" world");
}
</script>
</head>
<body onload="loadPage();">
hello
</body>
</html>led mike
Right, now I get it, but how do I inject something that can write into the page without erasing it? Should I override some event like DocumentComplete? Can I make loaded page to get "busy" again and inject something then?
-
Right, now I get it, but how do I inject something that can write into the page without erasing it? Should I override some event like DocumentComplete? Can I make loaded page to get "busy" again and inject something then?
Alexander Fedorov wrote:
but how do I inject something that can write into the page without erasing it?
Well you already have the inject part working correct? So you need to figure out how to modify the HTML using javascript without using document.write. AFAIK you would use the HTML DOM[^] to do that. So for example say you had a page with span element in it and you wanted to change the text in the span to say "Code Project". You would get the reference to span element using the HTML DOM and then:
theSpan.innerText = "Code Project";
something like that for IE but all browsers are not equal. Of course how you get the reference to the span element depends on the page you are working with.led mike