[Interest] [Qtwebengine] Inject QWebChannel and/or JQuery into QWebEnginePage
Aleksey Yermakov
jp.kuraisu at gmail.com
Mon Mar 21 15:49:22 CET 2016
Hi,
You don't have to inject QWebChannel.js every time if you use
QWebEngineScriptCollection in your QWebEngineProfile. Here is a sample code
from my project:
const char s_qWebChannelAdditionalScript[] = "new
QWebChannel(qt.webChannelTransport, function(channel) {"
" window.exported_object =
channel.objects.exported_object;"
"});";
QWebEngineProfile *WebEngineView::prepareProfile()
{
QWebEngineProfile *profile = new QWebEngineProfile("Profile", this);
...
// Preparing qwebchannel.js for injection
QFile qWebChannelJsFile(":/qtwebchannel/qwebchannel.js");
if(! qWebChannelJsFile.open(QIODevice::ReadOnly)) {
MY_ERROR("Failed to load qwebchannel.js with error: " +
qWebChannelJsFile.errorString());
} else {
QByteArray qWebChannelJs = qWebChannelJsFile.readAll();
qWebChannelJs.append(QString(s_qWebChannelAdditionalScript));
QWebEngineScript script;
script.setSourceCode(qWebChannelJs);
script.setName("qwebchannel.js");
script.setWorldId(QWebEngineScript::MainWorld);
script.setInjectionPoint(QWebEngineScript::DocumentCreation);
script.setRunsOnSubFrames(false);
profile->scripts()->insert(script);
}
return profile;
}
Don't forget to create your QWebEnginePage with this profile instead of
default one.
Cheers,
Aleksey
On Sun, Mar 20, 2016 at 9:30 PM, NoRulez <norulez at me.com> wrote:
> Hello,
>
> Did I have to inject QWebChannel and for example JQuery every time before
> or after I call setUrl()/load() to QWebEngineView or only once?
>
> Because the jquery event for loading a site is only called once and if
> loaded then it has the previous url:
>
> $(window).load(function () {
> alert('page is loaded');
> alert(window.location.href);
> });
>
> Thanks in advance
>
> Best Regards
>
> _______________________________________________
> QtWebEngine mailing list
> QtWebEngine at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/qtwebengine
>
--
Aleksey Yermakov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160321/c37d790a/attachment.html>
More information about the Interest
mailing list