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



  • 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.



  • 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
      }
    
    });
    


Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.