From 02e213920cbd45d82f01774fa33b63827a32ec8a Mon Sep 17 00:00:00 2001 From: ShaopengLin Date: Mon, 23 Sep 2024 10:59:03 -0400 Subject: [PATCH] Introduce QWebEngine Javascript Infrastructure Setup for header anchor injection --- kiwix-desktop.pro | 3 ++- resources/js.qrc | 5 +++++ resources/js/headerAnchor.js | 0 src/kprofile.cpp | 22 ++++++++++++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 resources/js.qrc create mode 100644 resources/js/headerAnchor.js diff --git a/kiwix-desktop.pro b/kiwix-desktop.pro index cf37176..1c6ea24 100644 --- a/kiwix-desktop.pro +++ b/kiwix-desktop.pro @@ -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 diff --git a/resources/js.qrc b/resources/js.qrc new file mode 100644 index 0000000..8391239 --- /dev/null +++ b/resources/js.qrc @@ -0,0 +1,5 @@ + + + js/headerAnchor.js + + diff --git a/resources/js/headerAnchor.js b/resources/js/headerAnchor.js new file mode 100644 index 0000000..e69de29 diff --git a/src/kprofile.cpp b/src/kprofile.cpp index 6659ebb..cefe6f7 100644 --- a/src/kprofile.cpp +++ b/src/kprofile.cpp @@ -4,6 +4,26 @@ #include #include #include +#include +#include + +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)