Commit b3d23af2 authored by Laurent Montel's avatar Laurent Montel 😁

Allow to load script

parent 5af4dc7c
......@@ -22,6 +22,7 @@
#include <QWebEngineProfile>
#include <QWebEngineScript>
#include <QWebEngineScriptCollection>
#include <QDebug>
using namespace WebEngineViewer;
......@@ -52,9 +53,10 @@ void WebEngineManageScript::addScript(QWebEngineProfile *profile, const QString
script.setName(scriptName);
script.setInjectionPoint(injectionPoint);
script.setRunsOnSubFrames(true);
script.setWorldId(QWebEngineScript::ApplicationWorld);
script.setWorldId(QWebEngineScript::MainWorld);
}
script.setSourceCode(source);
profile->scripts()->insert(script);
qDebug()<<" insert script:::::::::::::::::::::"<<script.isNull() << "scriptName"<<scriptName ;
}
}
......@@ -18,6 +18,7 @@
*/
#include "webengineview.h"
#include "webenginemanagescript.h"
#include "../config-webengineviewer.h"
#include <QEvent>
#include <QKeyEvent>
......@@ -43,6 +44,7 @@ public:
QString mJquery;
qreal mSavedRelativePosition;
QWidget *mCurrentWidget;
WebEngineManageScript *mManagerScript;
};
WebEngineView::WebEngineView(bool useJQuery, QWidget *parent)
......@@ -50,6 +52,7 @@ WebEngineView::WebEngineView(bool useJQuery, QWidget *parent)
d(new WebEngineViewer::WebEngineViewPrivate)
{
installEventFilter(this);
d->mManagerScript = new WebEngineManageScript(this);
if (useJQuery) {
QFile file;
file.setFileName(QStringLiteral(":/data/jquery.min.js"));
......@@ -57,7 +60,8 @@ WebEngineView::WebEngineView(bool useJQuery, QWidget *parent)
d->mJquery = QString::fromUtf8(file.readAll());
d->mJquery.append(QStringLiteral("\nvar qt = { 'jQuery': jQuery.noConflict(true) };"));
file.close();
connect(this, &WebEngineView::loadFinished, this, &WebEngineView::slotLoadFinished);
//connect(this, &WebEngineView::loadFinished, this, &WebEngineView::slotLoadFinished);
d->mManagerScript->addScript(page()->profile(), d->mJquery, QStringLiteral("jquery"), QWebEngineScript::DocumentCreation);
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
......@@ -70,6 +74,11 @@ WebEngineView::~WebEngineView()
delete d;
}
void WebEngineView::addScript(const QString &source, const QString &scriptName, QWebEngineScript::InjectionPoint injectionPoint)
{
d->mManagerScript->addScript(page()->profile(), source, scriptName, injectionPoint);
}
void WebEngineView::forwardWheelEvent(QWheelEvent *event)
{
Q_UNUSED(event);
......
......@@ -21,6 +21,7 @@
#define WEBENGINEVIEW_H
#include <QWebEngineView>
#include <QWebEngineScript>
#include "webengineviewer_export.h"
class QWebEngineDownloadItem;
namespace WebEngineViewer
......@@ -40,6 +41,7 @@ public:
bool hasPrintPreviewSupport() const;
void saveHtml(QWebEngineDownloadItem *download);
void addScript(const QString &source, const QString &scriptName, QWebEngineScript::InjectionPoint injectionPoint);
protected:
bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
QWebEngineView *createWindow(QWebEnginePage::WebWindowType type) Q_DECL_OVERRIDE;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment