From f613095208911cfa6b8609f8b87b4984dc55e69c Mon Sep 17 00:00:00 2001 From: Michal Klocek Date: Tue, 25 Aug 2020 11:37:34 +0200 Subject: Reuse qwebenginescript in qml Reuse core class and adopt api: * add missing setUrlSource to c++ class * fix typing for setRunsOnSubFrames * remove all invokable from setters in qml, we use properties for that anyway. * remove invokable toString , since we have debug stream operator in c++ [ChangeLog] In qml websetttings.runOnSubframes is now websettings.runsOnSubFrames Change-Id: Iba822a7aa6a59940484c972726d710a1b66cb20d Reviewed-by: Allan Sandfeld Jensen --- src/core/api/qwebenginescript.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/core/api/qwebenginescript.cpp') diff --git a/src/core/api/qwebenginescript.cpp b/src/core/api/qwebenginescript.cpp index e56017166..e3917566a 100644 --- a/src/core/api/qwebenginescript.cpp +++ b/src/core/api/qwebenginescript.cpp @@ -41,9 +41,12 @@ #include "user_script.h" #include +#include using QtWebEngineCore::UserScript; +QT_BEGIN_NAMESPACE + /*! \class QWebEngineScript \inmodule QtWebEngineCore @@ -172,6 +175,37 @@ void QWebEngineScript::setName(const QString &scriptName) d->setName(scriptName); } + +QUrl QWebEngineScript::sourceUrl() const +{ + return d->sourceUrl(); +} + +void QWebEngineScript::setSourceUrl(const QUrl &url) +{ + if (url == sourceUrl()) + return; + + d->setSourceUrl(url); + + QFile file; + if (url.scheme().compare(QLatin1String("qrc"), Qt::CaseInsensitive) == 0) { + if (url.authority().isEmpty()) + file.setFileName(QLatin1Char(':') + url.path()); + return; + } else { + file.setFileName(url.toLocalFile()); + } + + if (!file.open(QIODevice::ReadOnly)) { + qWarning() << "Can't open user script " << url; + return; + } + + QString source = QString::fromUtf8(file.readAll()); + setSourceCode(source); +} + /*! Returns the source of the script. */ @@ -291,3 +325,5 @@ QDebug operator<<(QDebug d, const QWebEngineScript &script) return d.space(); } #endif + +QT_END_NAMESPACE -- cgit v1.2.1