QWebChannel.js does not work with Chromium's existing functionality



  • How to achieve before the page is loaded on the registration of objects or methods, similar to Chromium registered in the method OnContextCreated.

    virtual void OnContextCreated(CefRefPtr<ClientAppRenderer> app,
                                CefRefPtr<CefBrowser> browser,
                                CefRefPtr<CefFrame> frame,
                                CefRefPtr<CefV8Context> context) OVERRIDE {
     CefRefPtr<CefV8Value> object = context->GetGlobal();
     CefRefPtr<CefV8Handler> handler = new V8Handler();
    // Bind test functions.
    object->SetValue(kGetPerfTests,
                     CefV8Value::CreateFunction(kGetPerfTests, handler),
                     V8_PROPERTY_ATTRIBUTE_READONLY);
    object->SetValue(kRunPerfTest,
                     CefV8Value::CreateFunction(kRunPerfTest, handler),
                     V8_PROPERTY_ATTRIBUTE_READONLY);
    object->SetValue(kPerfTestReturnValue,
                     CefV8Value::CreateFunction(kPerfTestReturnValue, handler),
                     V8_PROPERTY_ATTRIBUTE_READONLY);
     }
    

    The current method of using QWebChannel.js no way to ensure that the first injection, if the page load on the need to communicate with C + + can not be achieved, you must wait for QWebChannel.js loaded and new QWebChannel object after the implementation. Like the following page content can not be achieved

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    </head>
    <body>
       <script type="text/javascript">
    	window.invoke("testCallbackFunc", "test");	
       </script>
    </body>
    </html>~~~~


  • @elliyu I have already solved it


  • Lifetime Qt Champion

    Glad you found out !

    Would you mind sharing your solution ? That would help other people hitting the same situation.



  • Re: How to inject QWebChannel.js before the page loads

      QByteArray ccJSFunc = "function invokeQT(funcName, params, funcID) { \n"
                          "try{\n"
                          "qt.webChannelTransport.send(JSON.stringify({\n"
                          "type:6,\n"
                          "object:'ctInvoke',\n"
                          "id:6,\n"
                          "method:6,\n"
                          "args:[funcName, params, funcID]\n"
                          "}))\n"
                          "}catch(e){console.warn('invokeQT call failed', e)}\n}\n"
                          "window.external = window.external || {}\n"
                          "window.external.invoke = window.external.invoke || invokeQT\n";
      QWebEngineScript scriptCC;
      scriptCC.setSourceCode(ccJSFunc);
    scriptCC.setWorldId(QWebEngineScript::MainWorld);
    scriptCC.setName("cccustom.js");
    scriptCC.setInjectionPoint(QWebEngineScript::DocumentCreation);
    scriptCC.setRunsOnSubFrames(true);
    

    The same way qwebchannel.js, js can be inserted above this.


Log in to reply
 

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