Browse Source

add JavaScript bridge for QSPExecString

Sonnix 5 years ago
parent
commit
9b745f8f65
6 changed files with 75 additions and 4 deletions
  1. 5 3
      Qqsp.pro
  2. 6 0
      qspwebbox.cpp
  3. 4 0
      qspwebbox.h
  4. 41 0
      qspwebchannel.cpp
  5. 16 0
      qspwebchannel.h
  6. 3 1
      qspwebengineurlschemehandler.cpp

+ 5 - 3
Qqsp.pro

@@ -149,15 +149,17 @@ CONFIG += enable-webbox
 enable-webbox {
   DEFINES += _WEBBOX
   DEFINES += _WEBBOX_COMMON
-  QT += webengine webenginewidgets
+  QT += webengine webenginewidgets webchannel
   SOURCES += qspwebbox.cpp \
     qspwebengineurlrequestinterceptor.cpp \
     qspwebengineurlschemehandler.cpp \
-    qspexecwebengineurlschemehandler.cpp
+    qspexecwebengineurlschemehandler.cpp \
+    qspwebchannel.cpp
   HEADERS += qspwebbox.h \
     qspwebengineurlrequestinterceptor.h \
     qspwebengineurlschemehandler.h \
-    qspexecwebengineurlschemehandler.h
+    qspexecwebengineurlschemehandler.h \
+    qspwebchannel.h
 }
 
 #CONFIG += enable-webbox-webkit

+ 6 - 0
qspwebbox.cpp

@@ -38,6 +38,9 @@ QspWebBox::QspWebBox(QWidget *parent) : QWebEngineView(parent)
     profile->installUrlSchemeHandler(QByteArray("exec"), qeweush);
     QWebEnginePage * page = new QWebEnginePage(profile, this);
     setPage(page);
+    QWebChannel *channel = new QWebChannel(page);
+    channel->registerObject(QStringLiteral("qsp"), &qspJS);
+    page->setWebChannel(channel);
     connect(qeweush, SIGNAL(qspLinkClicked(QUrl)), this, SLOT(OnQspLinkClicked(QUrl)));
 }
 
@@ -126,6 +129,9 @@ void QspWebBox::RefreshUI(bool isScroll)
         QEventLoop loop;
         connect(page(), SIGNAL(loadFinished(bool)), &loop, SLOT(quit()));
         page()->load(QUrl("qsp:/"));
+        QWebChannel *channel = new QWebChannel(newpage);
+        channel->registerObject(QStringLiteral("qsp"), &qspJS);
+        newpage->setWebChannel(channel);
         loop.exec();
     }
     page()->triggerAction(QWebEnginePage::ReloadAndBypassCache);

+ 4 - 0
qspwebbox.h

@@ -8,8 +8,10 @@
 #include <QColor>
 #include <QVariant>
 #include <QUrl>
+#include <QWebChannel>
 
 #include "qspwebengineurlschemehandler.h"
+#include "qspwebchannel.h"
 
 namespace Ui {
 class QspWebBox;
@@ -67,6 +69,8 @@ private:
     int m_sizeType;
     QspWebEngineUrlSchemeHandler *qweush;
     QWebEngineProfile *profile;
+    QWebChannel *channel;
+    QspWebChannel qspJS;
 
 signals:
     void qspLinkClicked(QUrl url);

+ 41 - 0
qspwebchannel.cpp

@@ -0,0 +1,41 @@
+#include "qspwebchannel.h"
+
+#include <QMessageBox>
+
+#include <qsp_default.h>
+#include "callbacks_gui.h"
+#include "comtools.h"
+
+QspWebChannel::QspWebChannel(QObject *parent) : QObject(parent)
+{
+
+}
+
+void QspWebChannel::ExecString(const QString &string)
+{
+    if (!QSPExecString(qspStringFromQString(string), QSP_TRUE))
+        ShowError();
+}
+
+void QspWebChannel::ShowError()
+{
+    QString errorMessage;
+    QSP_CHAR *loc;
+    int code, actIndex, line;
+    QSPGetLastErrorData(&code, &loc, &actIndex, &line);
+    QString desc = QSPTools::qspStrToQt(QSPGetErrorDesc(code));
+    if (loc)
+        errorMessage = QString("Location: %1\nArea: %2\nLine: %3\nCode: %4\nDesc: %5")
+                .arg(QSPTools::qspStrToQt(loc))
+                .arg(actIndex < 0 ? QString("on visit") : QString("on action"))
+                .arg(line)
+                .arg(code)
+                .arg(desc);
+    else
+        errorMessage = QString("Code: %1\nDesc: %2")
+                .arg(code)
+                .arg(desc);
+    QMessageBox dialog(QMessageBox::Critical, tr("Error"), errorMessage, QMessageBox::Ok);
+    dialog.exec();
+    QSPCallBacks::RefreshInt(QSP_FALSE);
+}

+ 16 - 0
qspwebchannel.h

@@ -0,0 +1,16 @@
+#ifndef QSPWEBCHANNEL_H
+#define QSPWEBCHANNEL_H
+
+#include <QObject>
+
+class QspWebChannel : public QObject
+{
+    Q_OBJECT
+public:
+    explicit QspWebChannel(QObject *parent = nullptr);
+    Q_INVOKABLE void ExecString(const QString &string);
+private:
+    void ShowError();
+};
+
+#endif // QSPWEBCHANNEL_H

+ 3 - 1
qspwebengineurlschemehandler.cpp

@@ -45,7 +45,9 @@ void QspWebEngineUrlSchemeHandler::requestStarted(QWebEngineUrlRequestJob *reque
                 replystr.append(QString("}\na:link {\ncolor: %1;\n").arg(m_linkColor.name()));
             replystr.append("}\n</style>\n");
             if(m_isCustomCSS)
-                replystr.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"custom.css\">");
+                replystr.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"custom.css\">\n");
+            replystr.append("<script src=\"qrc:///qtwebchannel/qwebchannel.js\"></script>\n");
+            replystr.append("<script type=\"text/javascript\"> \nvar qsp; \ndocument.addEventListener(\"DOMContentLoaded\", function () { \nnew QWebChannel(qt.webChannelTransport, function(channel) { \nqsp = channel.objects.qsp; \n}); \n});\n</script>\n");
             replystr.append(m_head);
             replystr.append("</head>\n<body>\n");
             replystr.append(m_text);