QWebEnginePage javascript injection on redirect inconsistent?
-
Hi,
i try to swap / replace JavaScript sources dependent on the URL i'am about to approach. There is always a default script for any page, but this gets replaced / swapped if there exists a special script for an url.
To Achieve this i emit a signal on acceptNavigationRequest in a QWebEnginePage implementation which triggers a slot to replace the javascript in QWebEnginePage scripts. However if i get redirects from a website, the scripts in QWebEnginePage appear to be inconsistent with what the QWebEnginePage is actually injecting.
// Pseudocode which wraps my usecase togehter.
bool WebEnginePage::acceptNavigationRequest(const QUrl& qurlIn, QWebEnginePage::NavigationType type, bool isMainFrame) {
...
if(isMainFrame){
emit signalNavigateTo(qurlIn);
}
...
}// Child of QWebEnginePage
void WebEnginePage::updateScriptsSlot(const Qurl& url){
QString name("script");
for (QWebEngineScript foundScript : scripts().find(name)) {
qDebug() << name << foundScript // correct name and scriptsource
bool success = scripts()->remove(foundScript);
qDebug() << success // Always 1 correct name and scriptsource
}// function to get the correct scriptSource by url. I take parts of the URL to determine the correct scriptsource. if the url has nothing special, the default script will be returned.
const QString& scriptSource = getScriptSource(url);qDebug() << name << scriptSource; // correct name and scriptsource
QWebEngineScript script;
script.setName(name);
script.setSourceCode(scriptSource);
script.setInjectionPoint(QWebEngineScript::Deferred);
script.setWorldId(QWebEngineScript::MainWorld);
scripts().insert(script);
}Lets say i browse a startpage and the QWebEnginePage gets a default script added to its scripts. When i browse to another page with multiple redirects the signal gets emitted multiple times.
The slot does nothing more than removeing an existing script by name and adding a new one by the same name. This can be a default or special script dependent on the url.
However, landing on the final site i always see both scripts, the default and the special script injected via javascript log. If i reload the QWebEnginePage, the special script gets injected correctly without the default.My asumption is, that somewhere during redirects the default script gets injected and sits there until Deferred. Any ideas, why this is happening? In this case, shouldn't a redirect likely flush "waiting" scripts?
I've already reworked my code with the use of Greasemonkey to make special scripts include their respective url and exclude all urls in my default script and add them to scripts() on initialisation.
Still i would like to know what went wrong with my previous approach.I appreciate any information.