Details
Description
I have a JavaScript which should be injected in every website loaded in a WebEngineView.
Therefore I create a QWebEngineProfile where I add my Script.
The JS stops working if I do the following steps i.e. in the demo browser example:
- Load any page on which there is a link with target="_blank".
- Click on this link, a new tab opens. The injected JS is working in both tabs.
- Close the new opened tab. The old tab gets active again. JS still working.
- Click inside the old active tab on any other link.
- After the new page loaded, the JS is not injected any more (or broken).
The expected behavior is that the JS is injected and executed inside the new loaded page.
I was able to reproduce this behavior in the "simple browser" example. With the following changes:
BrowserWindow *Browser::createWindow(bool offTheRecord)
{{{}}
QWebEngineProfile *testProfile = new QWebEngineProfile("TestJavaScriptStore");
QWebEngineScript script;
script.setName("test.js");
script.setSourceCode("alert('test');");
script.setWorldId(QWebEngineScript::ApplicationWorld);
script.setInjectionPoint(QWebEngineScript::DocumentCreation);
script.setRunsOnSubFrames(true);
testProfile->scripts()->insert(script);
auto profile = offTheRecord ? &m_otrProfile : testProfile;
auto mainWindow = new BrowserWindow(this, profile);
m_windows.append(mainWindow);
QObject::connect(mainWindow, &QObject::destroyed, [this, mainWindow]() {
m_windows.removeOne(mainWindow);
});
mainWindow->show();
return mainWindow;
}