Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to inject javascript into webbrowser control

How to inject javascript into webbrowser control

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestionc++javascripthardware
6 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alexander Fedorov
    wrote on last edited by
    #1

    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?

    L 1 Reply Last reply
    0
    • A Alexander Fedorov

      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?

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      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

      A 1 Reply Last reply
      0
      • L led mike

        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

        A Offline
        A Offline
        Alexander Fedorov
        wrote on last edited by
        #3

        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.

        L 1 Reply Last reply
        0
        • A Alexander Fedorov

          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.

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          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

          A 1 Reply Last reply
          0
          • L 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

            A Offline
            A Offline
            Alexander Fedorov
            wrote on last edited by
            #5

            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?

            L 1 Reply Last reply
            0
            • A Alexander Fedorov

              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?

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups