Introduce QWebEngine Javascript Infrastructure

Setup for header anchor injection
This commit is contained in:
ShaopengLin 2024-09-23 10:59:03 -04:00 committed by Kelson
parent b1f02ec89c
commit 02e213920c
4 changed files with 29 additions and 1 deletions

View File

@ -232,6 +232,7 @@ RESOURCES += \
resources/translations.qrc \
resources/contentmanager.qrc \
resources/settingsmanager.qrc \
resources/style.qrc
resources/style.qrc \
resources/js.qrc
RC_ICONS = resources/icons/kiwix/app_icon.ico

5
resources/js.qrc Normal file
View File

@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>js/headerAnchor.js</file>
</qresource>
</RCC>

View File

View File

@ -4,6 +4,26 @@
#include <QFileDialog>
#include <QMessageBox>
#include <QWebEngineSettings>
#include <QWebEngineScript>
#include <QWebEngineScriptCollection>
namespace
{
QWebEngineScript getScript(QString filename,
QWebEngineScript::InjectionPoint point = QWebEngineScript::DocumentReady)
{
QWebEngineScript script;
script.setInjectionPoint(point);
script.setWorldId(QWebEngineScript::UserWorld);
QFile scriptFile(filename);
scriptFile.open(QIODevice::ReadOnly);
script.setSourceCode(scriptFile.readAll());
return script;
}
}
QString askForSaveFilePath(const QString& suggestedName)
{
@ -36,6 +56,8 @@ KProfile::KProfile(QObject *parent) :
#else // Qt 5.13 and later
setUrlRequestInterceptor(new ExternalReqInterceptor(this));
#endif
scripts()->insert(getScript(":/js/headerAnchor.js"));
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)