Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is injecting C++ via Javascript into an IFRAME possible?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
qt5.5iframewebkitqwebviewjavascript
2 Posts 1 Posters 2.6k Views
  • 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 Offline
    M Offline
    maximo
    wrote on 11 Nov 2015, 02:38 last edited by
    #1

    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 11 Nov 2015, 03:46
    0
    • M maximo
      11 Nov 2015, 02:38

      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 Offline
      M Offline
      maximo
      wrote on 11 Nov 2015, 03:46 last edited by
      #2

      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
      0

      2/2

      11 Nov 2015, 03:46

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved