Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Is injecting C++ via Javascript into an IFRAME possible?

    General and Desktop
    qt5.5 iframe webkit qwebview javascript
    1
    2
    2287
    Loading More Posts
    • 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.
    • M
      maximo last edited by

      From the QWebView's loadFinished() slot, I can reach an IFRAME of a given Webkit page like...

      QWebFrame *iframe = ui->webView->page()->mainFrame()->findFirstElement("iframe").webFrame();
      

      ...and then try to inject a Webkit bridge like so...

      frame->addToJavaScriptWindowObject(QString("cpp"), this);
      

      ...but unfortunately the IFRAME has no clue what the cpp object is, even if I do it inside a jQuery page load with a setTimeout:

      $(document).ready(function(){ setTimeout('testIfCPPLoaded();',100); });
      

      This seems like a limitation of Qt 5.5, or I'm missing the technique. There's no documentation on this for an IFRAME.

      M 1 Reply Last reply Reply Quote 0
      • M
        maximo @maximo last edited by

        I found the solution. Not only can you call the HTML5 postMessage() API to pass messages back and form from/to the IFRAME to the parent document, but Qt's version of WebKit have lax security controls compared to Chrome and do allow you to do the following in the IFRAME's Javascript. The trick is the window.parent.[whatever] -- something that Chrome won't let you do with stuff accessed with file:// (local files).

        $(document).ready(function(){
        
          if (window.parent.cpp) {
            var cpp = window.parent.cpp;
            // do something with C++ here through the cpp object
          }
        
        });
        
        1 Reply Last reply Reply Quote 0
        • First post
          Last post