documentation

This commit is contained in:
2026-03-16 20:04:30 +01:00
parent a7257a3492
commit 7c3b780ae9
14 changed files with 2424 additions and 583 deletions

View File

@@ -52,9 +52,9 @@ int main(int argc, char *argv[])
}
if (i == 6) {
rkt_data_t *r = rkt_webview_call_js(wv1, "document.body.innerHTML = '<h1>Hi!</h1>'; return document.body.innerHTML;");
printf("rkt_js_result: %d: %s\n", r->data.js_result.result, r->data.js_result.value);
rkt_webview_free_data(r);
//rkt_data_t *r = rkt_webview_call_js(wv1, "document.body.innerHTML = '<h1>Hi!</h1>'; return document.body.innerHTML;");
//printf("rkt_js_result: %d: %s\n", r->data.js_result.result, r->data.js_result.value);
//rkt_webview_free_data(r);
}
if (i == 7) {
@@ -75,7 +75,7 @@ int main(int argc, char *argv[])
rkt_webview_run_js(wv1, buf);
}
if (i == 24) {
if (i == 15) {
rkt_webview_close(wv2);
}
i += 1;

View File

@@ -352,23 +352,10 @@ void Rktwebview_qt::processCommand(Command *cmd)
}
}
void Rktwebview_qt::processJsEventQueues()
{
QList<rktwebview_t> wvs = _views.keys();
int i, N;
for(i = 0, N = wvs.length(); i < N; i++) {
int wv = wvs[i];
if (_views.contains(wv)) {
WebviewWindow *win = _views[wv];
win->processJsEvents();
}
}
}
void Rktwebview_qt::removeView(int id)
{
if (_views.contains(id)) {
WebviewWindow *win = _views[id];
//WebviewWindow *win = _views[id];
_views.remove(id);
_view_js_callbacks.remove(id);
}
@@ -394,9 +381,14 @@ rkt_wv_context_t Rktwebview_qt::newContext(const char *boilerplate_js, const cha
QString name = QString::asprintf("profile-%d", _context_counter);
QString code = "if (window.rkt_event_queue === undefined) { window.rkt_event_queue = []; }\n"
"window.rkt_evt_frame_el = null;\n"
"window.rkt_evt_frame_win = null;\n"
"window.rkt_send_event = function(obj) {\n"
" console.log('Sending event: ' + obj);\n"
" //console.log('Sending event: ' + obj);\n"
" window.rkt_event_queue.push(obj);\n"
" if (window.rkt_evt_frame_el) {\n"
" window.rkt_evt_frame_win.print();\n"
" }\n"
"};\n"
"window.rkt_get_events = function() {\n"
" let q = window.rkt_event_queue;\n"
@@ -404,6 +396,22 @@ rkt_wv_context_t Rktwebview_qt::newContext(const char *boilerplate_js, const cha
" let json_q = JSON.stringify(q);\n"
" return json_q;\n"
"};\n"
"// add hidden hover element to body if necessary\n"
"setInterval(function () {\n"
" if (window.rkt_evt_frame_el === null || window.rkt_evt_frame_el === undefined) {\n"
" window.rkt_evt_frame_el = document.createElement('iframe');\n"
" window.rkt_evt_frame_el.style.display = 'none';\n"
" window.rkt_evt_frame_el.setAttribute('id', 'rkt-evt-frame');\n"
" window.rkt_evt_frame_el.setAttribute('name', 'rkt-evt-frame');\n"
" document.body.append(window.rkt_evt_frame_el);\n"
" window.rkt_evt_frame_win = window.rkt_evt_frame_el.contentWindow;\n"
" } else {"
" if (window.rkt_event_queue.length > 0) {\n"
" window.rkt_evt_frame_win.print();\n"
" }\n"
" }\n"
"},\n"
"10);\n"
"";
QList<QWebEngineScript> scripts;
@@ -767,24 +775,14 @@ void Rktwebview_qt::onPageLoad(rktwebview_t w)
void Rktwebview_qt::pageLoaded(rktwebview_t w, bool ok)
{
/*runJs(w,
"if (window.rkt_event_queue === undefined) { window.rkt_event_queue = []; }\n"
"window.rkt_send_event = function(obj) {\n"
" console.log('Sending event: ' + obj);\n"
" window.rkt_event_queue.push(obj);\n"
"};\n"
"window.rkt_get_events = function() {\n"
" let q = window.rkt_event_queue;\n"
" window.rkt_event_queue = [];\n"
" let json_q = JSON.stringify(q);\n"
" return json_q;\n"
"};\n"
);
*/
if (!ok) {
// Inject code of the profile to this page
WebviewWindow *win = _views[w];
if (win->navigationEventSent()) {
return;
}
QWebEngineProfile *p = win->profile();
QWebEngineScriptCollection *col = p->scripts();
QList<QWebEngineScript> l = col->toList();
@@ -906,9 +904,6 @@ Rktwebview_qt::Rktwebview_qt(Rktwebview_qt **handler) :
_evt_loop_depth = 0;
_app = new QApplication(_argc, _argv);
connect(&_process_events, &QTimer::timeout, this, &Rktwebview_qt::processJsEventQueues);
_process_events.start(5);
// See Qt 6.10 remark at doEvents.
//connect(&_evt_loop_timer, &QTimer::timeout, this, &Rktwebview_qt::stopEventloop);

View File

@@ -47,9 +47,6 @@ private:
void runJs(rktwebview_t wv, const char *js);
result_t doWindow(rktwebview_t w, int cmd, result_t on_error);
public slots:
void processJsEventQueues();
private slots:
void stopEventloop();

View File

@@ -52,6 +52,7 @@ WebviewWindow::WebviewWindow(QWebEngineProfile *profile, WebviewWindow *parent)
_resized = 0;
_profile = profile;
_navigation_event_sent = false;
if (parent != nullptr) {
setWindowModality(Qt::WindowModality::WindowModal);
@@ -67,11 +68,14 @@ void WebviewWindow::navigationRequested(QWebEngineNavigationRequest &req)
QWebEngineNavigationRequest::NavigationType t = req.navigationType();
if (t == QWebEngineNavigationRequest::NavigationType::TypedNavigation ||
t == QWebEngineNavigationRequest::RedirectNavigation) {
_navigation_event_sent = false;
req.accept();
} else {
EventContainer e("navigation-request");
e["url"] = req.url().toString();
QString u = req.url().toString();
e["url"] = u;
QString type;
switch (req.navigationType()) {
@@ -94,6 +98,8 @@ void WebviewWindow::navigationRequested(QWebEngineNavigationRequest &req)
e["type"] = type;
_container->triggerEvent(_view->id(), e);
_navigation_event_sent = true;
req.reject();
}
}
@@ -208,6 +214,11 @@ void WebviewWindow::setOUToken(const QString &token)
_ou_token = token;
}
bool WebviewWindow::navigationEventSent()
{
return _navigation_event_sent;
}
void WebviewWindow::addView(WebViewQt *v, Rktwebview_qt *c)
{
_container = c;
@@ -216,36 +227,6 @@ void WebviewWindow::addView(WebViewQt *v, Rktwebview_qt *c)
QWebEnginePage *page = _view->page();
/*
if (_profile == nullptr) {
page = _view->page();
} else {
page = new QWebEnginePage(_profile, this);
_view->setPage(page);
}
// Inject event handling code for the javascript side
QWebEngineScriptCollection &col = page->scripts();
QWebEngineScript evt_script;
evt_script.setInjectionPoint(QWebEngineScript::DocumentReady);
evt_script.setName("rkt_webview_event_handling");
evt_script.setSourceCode(
"window.rkt_event_queue = [];\n"
"window.rkt_send_event = function(obj) {\n"
" console.log('Sending event: ' + obj);\n"
" window.rkt_event_queue.push(obj);\n"
"};\n"
"window.rkt_get_events = function() {\n"
" let q = window.rkt_event_queue;\n"
" window.rkt_event_queue = [];\n"
" let json_q = JSON.stringify(q);\n"
" return json_q;\n"
"};\n"
);
evt_script.setWorldId(QWebEngineScript::ApplicationWorld);
//col.insert(evt_script);
*/
connect(page, &QWebEnginePage::loadFinished, this, [this](bool ok) {
_container->pageLoaded(_view->id(), ok);
});
@@ -255,6 +236,12 @@ void WebviewWindow::addView(WebViewQt *v, Rktwebview_qt *c)
connect(page, &QWebEnginePage::navigationRequested, this, &WebviewWindow::navigationRequested);
connect(page, &QWebEnginePage::certificateError, this, &WebviewWindow::handleCertificate);
connect(page, &QWebEnginePage::windowCloseRequested, this, &WebviewWindow::closeView);
connect(page, &QWebEnginePage::linkHovered, this, &WebviewWindow::linkHovered);
connect(page, &QWebEnginePage::printRequestedByFrame, this, &WebviewWindow::evtRequested);
}
WebViewQt *WebviewWindow::view()
@@ -306,6 +293,21 @@ void WebviewWindow::openDevTools()
});
}
void WebviewWindow::linkHovered(const QUrl &u)
{
QString s = u.toString();
EventContainer hover_event("link-hovered");
hover_event["url"] = s;
_container->triggerEvent(_view->id(), hover_event);
}
void WebviewWindow::evtRequested(QWebEngineFrame frame)
{
if (frame.name() == "rkt-evt-frame") {
processJsEvents();
}
}
void WebviewWindow::moveEvent(QMoveEvent *event)
{

View File

@@ -6,6 +6,7 @@
#include <QWebEngineCertificateError>
#include <QWebEngineProfile>
#include <QWebEngineNavigationRequest>
#include <QWebEngineFrame>
class WebViewQt;
class Rktwebview_qt;
@@ -37,24 +38,28 @@ private:
QWebEngineProfile *_profile;
bool _navigation_event_sent;
private slots:
void handleCertificate(const QWebEngineCertificateError &certificateError);
void navigationRequested(QWebEngineNavigationRequest &req);
public slots:
void processJsEvents();
void closeView();
protected:
void closeEvent(QCloseEvent *evt);
public:
void closeView();
bool windowCreated();
int moveCount();
int resizeCount();
void setOUToken(const QString &token);
bool navigationEventSent();
public:
void addView(WebViewQt *v, Rktwebview_qt *c);
WebViewQt *view();
@@ -70,6 +75,8 @@ public:
private slots:
void triggerResize();
void triggerMove();
void linkHovered(const QUrl &u);
void evtRequested(QWebEngineFrame frame);
public:
QWebEngineProfile *profile();