[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[no subject]
From: |
Gavin D. Smith |
Date: |
Wed, 30 Nov 2022 13:36:35 -0500 (EST) |
branch: old/qt-info
commit b04e952948a168ad215b72e8cf46260bc8dd991c
Author: Gavin Smith <gavinsmith0123@gmail.com>
AuthorDate: Tue Apr 9 00:28:08 2019 +0100
Inject info.js into viewed pages.
---
js/docbrowser/infopath.c | 13 ++--
js/docbrowser/mainwindow.cpp | 152 +++++++++++++++++++++++++++++++++++--------
js/docbrowser/mainwindow.h | 10 ++-
js/docbrowser/qwebchannel.js | 2 -
4 files changed, 140 insertions(+), 37 deletions(-)
diff --git a/js/docbrowser/infopath.c b/js/docbrowser/infopath.c
index 289b4e46fc..9d4415f3b9 100644
--- a/js/docbrowser/infopath.c
+++ b/js/docbrowser/infopath.c
@@ -19,7 +19,10 @@ locate_manual (const char *manual)
{
datadir = getenv ("QTINFO_DATADIR");
if (!datadir)
- return 0;
+ {
+ exit (1); /* probably wrong for C++ */
+ return 0;
+ }
datadir_len = strlen (datadir);
}
@@ -32,9 +35,9 @@ locate_manual (const char *manual)
}
closedir (d);
- char *s = malloc (datadir_len + strlen ("/examples/")
+ char *s = malloc (datadir_len + strlen ("/test/")
+ strlen (manual) + 1);
- sprintf (s, "%s/examples/%s", datadir, manual);
+ sprintf (s, "%s/test/%s", datadir, manual);
d = opendir (s);
if (!d)
@@ -46,9 +49,9 @@ locate_manual (const char *manual)
fprintf (stderr, "success so far\n");
free (s);
- s = malloc (datadir_len + strlen ("/examples/")
+ s = malloc (datadir_len + strlen ("/test/")
+ strlen (manual) + strlen ("/index.html") + 1);
- sprintf (s, "%s/examples/%s/index.html", datadir, manual);
+ sprintf (s, "%s/test/%s/index.html", datadir, manual);
struct stat dummy;
if (stat (s, &dummy) == -1)
diff --git a/js/docbrowser/mainwindow.cpp b/js/docbrowser/mainwindow.cpp
index 434447a3a5..756c8be7ac 100644
--- a/js/docbrowser/mainwindow.cpp
+++ b/js/docbrowser/mainwindow.cpp
@@ -11,21 +11,48 @@
#include <QWebEngineScript>
#include <QWebEngineScriptCollection>
#include <QFile>
+#include <QtDebug>
+#include <QWebChannel>
+#include <QWebEngineProfile>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
-
connect(ui->actionQuit, &QAction::triggered, this, &MainWindow::quit);
+ this->datadir = getenv ("QTINFO_DATADIR");
+ if (!this->datadir)
+ QCoreApplication::quit();
+
+ setup_channel ();
+
+ auto *profile = new QWebEngineProfile(this);
+ setup_profile(profile);
+ auto *page = new QWebEnginePage(profile, this);
+
+ ui->webEngineView->setPage(page);
+
+ connect(ui->webEngineView->page(), &QWebEnginePage::loadFinished,
+ this, &MainWindow::inject_qwebchannel);
+
+ ui->webEngineView->page()->load(QUrl(QString("file:") + this->datadir +
+ "/test/hello/index.html"));
+}
+
+void
+MainWindow::setup_channel()
+{
auto *server = new QWebSocketServer
(QStringLiteral("QWebChannel Standalone Server"),
QWebSocketServer::NonSecureMode,
this);
-// we shouldn't hardcode a number here. what if the program is run twice at
the same time?
+ // we shouldn't hardcode a number here. what if the program is run twice
+ // at the same time?
+ // can use 0 to pick a port
+
if (!server->listen(QHostAddress::LocalHost, 12345)) {
qFatal("Failed to open web socket server.");
}
@@ -40,44 +67,113 @@ MainWindow::MainWindow(QWidget *parent) :
this->core = new Core(ui, this);
channel->registerObject(QStringLiteral("core"), core);
+}
- connect(ui->webEngineView->page(), &QWebEnginePage::loadFinished, this,
&MainWindow::inject_qwebchannel);
+/* Load info.js and qwebchannel.js into the current page. */
+void
+MainWindow::inject_qwebchannel(bool finished_ok)
+{
+ if (!finished_ok || ui->webEngineView->url() == QUrl("about:blank"))
+ return;
- this->datadir = getenv ("QTINFO_DATADIR");
- if (!this->datadir)
- QCoreApplication::quit();
+ qDebug() << "injecting into page" << ui->webEngineView->url();
#define QWEBCHANNEL_JS "qwebchannel.js"
- QFile file;
- file.setFileName (QString(this->datadir)
- + "/docbrowser/" + QWEBCHANNEL_JS);
- file.open(QIODevice::ReadOnly);
- QByteArray b = file.readAll();
- this->qwebchannel_js = QString(b);
+ if (qwebchannel_js.isNull()) {
+ QFile file;
+ file.setFileName (QString(this->datadir)
+ + "/docbrowser/" + QWEBCHANNEL_JS);
+ file.open(QIODevice::ReadOnly);
+ QByteArray b = file.readAll();
+ qwebchannel_js = QString(b);
+ }
+
+ auto *page = ui->webEngineView->page();
-#define MANUAL "hello-html"
- core->load_manual (MANUAL);
+ /* Run the code, and only after that has finished, run the init
+ function. Qt uses an asynchronous callback system for this. Check if
+ wc_init is defined because this slot is activated even for
"about:blank",
+ the default page. */
+ page->runJavaScript (qwebchannel_js, [this, page](const QVariant&) {
+ page->runJavaScript(
+ "if (typeof wc_init == 'function') { wc_init(); }",
+ 0 );
+ });
}
-/* Load qwebchannel.js into the current page. */
void
-MainWindow::inject_qwebchannel(bool finished_ok)
+MainWindow::setup_profile(QWebEngineProfile *profile)
{
- if (!finished_ok)
- return;
+ /* First load the data from disk */
- /* Run the code, and only after that has finished, run the init
- function. Qt uses an asynchronous callback system for this. Check if
- init is defined because this slot is activated even for "about:blank",
- the default page. */
+#define INFO_JS "info.js"
- ui->webEngineView->page()->runJavaScript (qwebchannel_js,
- [this](const QVariant&) {
- this->ui->webEngineView->page()->runJavaScript(
- "if (typeof wc_init == 'function') { wc_init(); }",
- 0 );
- });
+ if (info_js.isNull()) {
+ QFile file;
+ file.setFileName (QString(this->datadir)
+ + "/" INFO_JS);
+ file.open(QIODevice::ReadOnly);
+ QByteArray b = file.readAll();
+ info_js = QString(b);
+ }
+
+#define MODERNIZR_JS "modernizr.js"
+
+ if (modernizr_js.isNull()) {
+ QFile file;
+ file.setFileName (QString(this->datadir)
+ + "/" MODERNIZR_JS);
+ file.open(QIODevice::ReadOnly);
+ QByteArray b = file.readAll();
+ modernizr_js = QString(b);
+ }
+
+#define INFO_CSS "info.css"
+
+ if (info_css.isNull()) {
+ QFile file;
+ file.setFileName (QString(this->datadir)
+ + "/" INFO_CSS);
+ file.open(QIODevice::ReadOnly);
+ QByteArray b = file.readAll();
+ info_css = QString(b);
+ }
+
+ /* Set up JavaScript to load info.css. This relies on there being no
+ single quotes or backslashes in info.css. The simplified() call
+ is needed to fit the CSS in a single line of JavaScript. */
+ QString insert_css = QString::fromLatin1("(function () {"
+ "var css = document.createElement('style');\n"
+ "css.type = 'text/css';\n"
+ "css.innerText = '%1';\n"
+ "document.head.appendChild(css);\n"
+ "})()").arg(info_css.simplified());
+
+ /* This needs to run after the <head> element is accessible, but before
+ the DOMContentLoaded event handlers in info.js fire. */
+ QWebEngineScript s;
+ s.setSourceCode(insert_css);
+ s.setInjectionPoint(QWebEngineScript::DocumentReady);
+ s.setWorldId(QWebEngineScript::MainWorld);
+ profile->scripts()->insert(s);
+
+ QWebEngineScript s2;
+ s2.setSourceCode(modernizr_js);
+ s2.setInjectionPoint(QWebEngineScript::DocumentCreation);
+ s2.setWorldId(QWebEngineScript::MainWorld);
+ profile->scripts()->insert(s2);
+
+ QWebEngineScript s3;
+ s3.setSourceCode(info_js);
+ s3.setInjectionPoint(QWebEngineScript::DocumentCreation);
+ s3.setWorldId(QWebEngineScript::MainWorld);
+ profile->scripts()->insert(s3);
+
+ /* We need the files to be loaded in a particular order.
+ Using QWebEngineProfile appears to work. Calling runJavaScript
+ after the page is loaded doesn't work this is too late for
+ DOMContentLoaded event handlers in info.js to fire. */
}
diff --git a/js/docbrowser/mainwindow.h b/js/docbrowser/mainwindow.h
index 83da54d08c..9f146a1e85 100644
--- a/js/docbrowser/mainwindow.h
+++ b/js/docbrowser/mainwindow.h
@@ -5,13 +5,13 @@
#include "core.h"
#include <QMainWindow>
-#include <QWebChannel>
-#include <QtWebSockets/QWebSocketServer>
+#include <QWebEngineProfile>
namespace Ui {
class MainWindow;
}
+
class MainWindow : public QMainWindow
{
Q_OBJECT
@@ -30,10 +30,16 @@ private:
Core *core;
QString qwebchannel_js;
+ QString info_js;
+ QString modernizr_js;
+ QString info_css;
+
char *datadir;
void quit();
void inject_qwebchannel(bool ok);
+ void setup_profile(QWebEngineProfile *profile);
+ void setup_channel();
};
#endif // MAINWINDOW_H
diff --git a/js/docbrowser/qwebchannel.js b/js/docbrowser/qwebchannel.js
index fc93f0e7c4..8ebfbb1c94 100644
--- a/js/docbrowser/qwebchannel.js
+++ b/js/docbrowser/qwebchannel.js
@@ -51,8 +51,6 @@
"use strict";
-console.log ("I am happy");
-
var QWebChannelMessageTypes = {
signal: 1,
propertyUpdate: 2,
- [no subject], (continued)
- [no subject], Gavin D. Smith, 2022/11/30
- [no subject], Gavin D. Smith, 2022/11/30
- [no subject], Gavin D. Smith, 2022/11/30
- [no subject], Gavin D. Smith, 2022/11/30
- [no subject], Gavin D. Smith, 2022/11/30
- [no subject], Gavin D. Smith, 2022/11/30
- [no subject], Gavin D. Smith, 2022/11/30
- [no subject], Gavin D. Smith, 2022/11/30
- [no subject], Gavin D. Smith, 2022/11/30
- [no subject], Gavin D. Smith, 2022/11/30
- [no subject],
Gavin D. Smith <=
- [no subject], Gavin D. Smith, 2022/11/30
- [no subject], Gavin D. Smith, 2022/11/30
- [no subject], Gavin D. Smith, 2022/11/30
- [no subject], Gavin D. Smith, 2022/11/30
- [no subject], Gavin D. Smith, 2022/11/30
- [no subject], Gavin D. Smith, 2022/11/30
- [no subject], Gavin D. Smith, 2022/11/30
- [no subject], Gavin D. Smith, 2022/11/30
- [no subject], Gavin D. Smith, 2022/11/30
- [no subject], Gavin D. Smith, 2022/11/30