Skip to content

Instantly share code, notes, and snippets.

@brcontainer
Last active March 13, 2016 03:06
Show Gist options
  • Save brcontainer/98016ee3655cfa535e8e to your computer and use it in GitHub Desktop.
Save brcontainer/98016ee3655cfa535e8e to your computer and use it in GitHub Desktop.
Inject script in QWebFrame
QObject::connect(ui->webView, SIGNAL(loadFinished(bool)), this, SLOT(loadedWebView(bool)));
...
void Form::loadedWebView(const bool a) {
Q_UNUSED(a);
QString scriptContent = "function TEST() { alert(1); }";
QString injectionJS = "(function() { var newdom = document.createElement('script');";
injectionJS.append("newdom.innerHTML = String((");
injectionJS.append(scriptContent);
injectionJS.append("));");
injectionJS.append("document.body.appendChild(newdom)");
injectionJS.append("})();");
ui->webView->page()->mainFrame()->evaluateJavaScript(injectionJS);
ui->webView->page()->mainFrame()->evaluateJavaScript("TEST(1);");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment