aboutsummaryrefslogtreecommitdiff
path: root/src/widgets
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-07-27 20:21:07 +0530
committerAkshay Nair <phenax5@gmail.com>2025-07-27 20:27:42 +0530
commit8604765c3660dbea9cae6996d5750bfb985e6751 (patch)
tree3a90f68dd07f8eb6a9c92d1f322162ac3b345383 /src/widgets
parentc95ee83c24b79e64a91a6d3a55addcb6e7ebd11b (diff)
downloadnull-browser-8604765c3660dbea9cae6996d5750bfb985e6751.tar.gz
null-browser-8604765c3660dbea9cae6996d5750bfb985e6751.zip
Add web.view.run_js + change rpc js api
Diffstat (limited to 'src/widgets')
-rw-r--r--src/widgets/BrowserWindow.cpp4
-rw-r--r--src/widgets/BrowserWindow.hpp1
-rw-r--r--src/widgets/Decorations.cpp6
-rw-r--r--src/widgets/Decorations.hpp1
-rw-r--r--src/widgets/EdgeDecoration.cpp9
-rw-r--r--src/widgets/EdgeDecoration.hpp1
-rw-r--r--src/widgets/IWebViewMediator.hpp1
-rw-r--r--src/widgets/WebView.cpp29
-rw-r--r--src/widgets/WebView.hpp2
-rw-r--r--src/widgets/WebViewStack.cpp10
-rw-r--r--src/widgets/WebViewStack.hpp1
11 files changed, 51 insertions, 14 deletions
diff --git a/src/widgets/BrowserWindow.cpp b/src/widgets/BrowserWindow.cpp
index 5d78360..1971b9c 100644
--- a/src/widgets/BrowserWindow.cpp
+++ b/src/widgets/BrowserWindow.cpp
@@ -93,6 +93,10 @@ void BrowserWindow::set_html(const QString &html, WebViewId webview_id) {
get_webview_mediator(webview_id)->set_html(html, webview_id);
}
+void BrowserWindow::run_javascript(const QString &js_code, WebViewId webview_id) {
+ get_webview_mediator(webview_id)->run_javascript(js_code, webview_id);
+}
+
void BrowserWindow::expose_rpc_function(const QString &name, const RpcFunc &action,
WebViewId webview_id) {
get_webview_mediator(webview_id)->expose_rpc_function(name, action, webview_id);
diff --git a/src/widgets/BrowserWindow.hpp b/src/widgets/BrowserWindow.hpp
index 3dc9e69..2ea7b88 100644
--- a/src/widgets/BrowserWindow.hpp
+++ b/src/widgets/BrowserWindow.hpp
@@ -38,6 +38,7 @@ public:
bool has_webview(WebViewId webview_id);
void open_url(const QUrl &url, OpenType open_type, WebViewId webview_id);
void set_html(const QString &html, WebViewId webview_id);
+ void run_javascript(const QString &js_code, WebViewId webview_id);
void expose_rpc_function(const QString &name, const RpcFunc &action, WebViewId webview_id);
bool on_window_key_event(QKeyEvent *event);
diff --git a/src/widgets/Decorations.cpp b/src/widgets/Decorations.cpp
index 7886442..dc644d5 100644
--- a/src/widgets/Decorations.cpp
+++ b/src/widgets/Decorations.cpp
@@ -84,6 +84,12 @@ void Decorations::set_html(const QString &html, WebViewId view_id) {
decoration.value()->set_html(html);
}
+void Decorations::run_javascript(const QString &js_code, WebViewId view_id) {
+ auto decoration = get_decoration_widget_by_view_id(view_id);
+ if (decoration.has_value())
+ decoration.value()->run_javascript(js_code);
+}
+
std::optional<WebViewId> Decorations::get_view_id(DecorationType type) {
auto decoration = get_decoration_widget_type(type);
return decoration.has_value() ? decoration.value()->get_view_id() : std::nullopt;
diff --git a/src/widgets/Decorations.hpp b/src/widgets/Decorations.hpp
index 9788359..97e0415 100644
--- a/src/widgets/Decorations.hpp
+++ b/src/widgets/Decorations.hpp
@@ -29,6 +29,7 @@ public:
bool has_webview(WebViewId view_id) override;
void open_url(const QUrl &url, OpenType open_type, WebViewId view_id) override;
void set_html(const QString &html, WebViewId view_id) override;
+ void run_javascript(const QString &js_code, WebViewId webview_id) override;
void expose_rpc_function(const QString &name, const RpcFunc &action,
WebViewId webview_id) override;
diff --git a/src/widgets/EdgeDecoration.cpp b/src/widgets/EdgeDecoration.cpp
index 7351552..bb114bf 100644
--- a/src/widgets/EdgeDecoration.cpp
+++ b/src/widgets/EdgeDecoration.cpp
@@ -11,7 +11,8 @@
QString default_html_layout = R"HTML(
<script>
- window.__nullbrowser ||= (() => {
+ window._nullbrowser ||= {};
+ window._nullbrowser.rpc ||= (() => {
const invoke = (action, opts = {}) => {
const urlParams = new URLSearchParams(opts);
const url = `nullrpc://${action}?${urlParams.toString()}`;
@@ -93,6 +94,12 @@ void EdgeDecoration::set_url(const QUrl &url) {
webview.value()->setUrl(url);
}
+void EdgeDecoration::run_javascript(const QString &js_code) {
+ if (!webview.has_value())
+ return;
+ webview.value()->run_javascript(js_code);
+}
+
std::optional<WebViewId> EdgeDecoration::get_view_id() {
return webview.has_value() ? std::make_optional(webview.value()->get_id()) : std::nullopt;
}
diff --git a/src/widgets/EdgeDecoration.hpp b/src/widgets/EdgeDecoration.hpp
index 3c90cfb..2c92de7 100644
--- a/src/widgets/EdgeDecoration.hpp
+++ b/src/widgets/EdgeDecoration.hpp
@@ -21,6 +21,7 @@ public:
void set_html(const QString &content);
void set_enabled(bool enabled_value);
void set_url(const QUrl &url);
+ void run_javascript(const QString &js_code);
std::optional<WebViewId> get_view_id();
void expose_rpc_function(const QString &name, const RpcFunc &action);
diff --git a/src/widgets/IWebViewMediator.hpp b/src/widgets/IWebViewMediator.hpp
index b9af36f..9c64e53 100644
--- a/src/widgets/IWebViewMediator.hpp
+++ b/src/widgets/IWebViewMediator.hpp
@@ -12,4 +12,5 @@ public:
virtual void set_html(const QString &html, WebViewId webview_id) = 0;
virtual void expose_rpc_function(const QString &name, const RpcFunc &action,
WebViewId webview_id) = 0;
+ virtual void run_javascript(const QString &js_code, WebViewId webview_id) = 0;
};
diff --git a/src/widgets/WebView.cpp b/src/widgets/WebView.cpp
index 2941e8d..91615a4 100644
--- a/src/widgets/WebView.cpp
+++ b/src/widgets/WebView.cpp
@@ -28,25 +28,28 @@ void WebView::open_devtools() {
}
void WebView::scroll_increment(int deltax, int deltay) {
- auto code = QString(R"JS((() => {
- const $el = document.scrollingElement;
- $el.scrollTo($el.scrollLeft + %1, $el.scrollTop + %2);
- })())JS")
- .arg(deltax)
- .arg(deltay);
-
- page()->runJavaScript(code);
+ // clang-format off
+ run_javascript(
+ QString(R"JS(
+ (() => {
+ const $el = document.scrollingElement;
+ $el.scrollTo($el.scrollLeft + %1, $el.scrollTop + %2);
+ })()
+ )JS").arg(deltax).arg(deltay)
+ );
+ // clang-format on
}
void WebView::scroll_to_top() {
- auto code = QString(R"JS(document.scrollingElement.scrollTo(0, 0))JS");
- page()->runJavaScript(code);
+ run_javascript(R"JS(
+ document.scrollingElement.scrollTo(0, 0)
+ )JS");
}
void WebView::scroll_to_bottom() {
- auto code = QString(
- R"JS(document.scrollingElement.scrollTo(0, document.scrollingElement.scrollHeight))JS");
- page()->runJavaScript(code);
+ run_javascript(R"JS(
+ document.scrollingElement.scrollTo(0, document.scrollingElement.scrollHeight)
+ )JS");
}
void WebView::enable_rpc_api() {
diff --git a/src/widgets/WebView.hpp b/src/widgets/WebView.hpp
index 7a767d3..36a252d 100644
--- a/src/widgets/WebView.hpp
+++ b/src/widgets/WebView.hpp
@@ -1,6 +1,7 @@
#pragma once
#include <QMainWindow>
+#include <QWebEnginePage>
#include <QWebEngineUrlRequestInterceptor>
#include <QWebEngineUrlRequestJob>
#include <QWebEngineUrlSchemeHandler>
@@ -31,6 +32,7 @@ public:
void enable_rpc_api();
void expose_rpc_function(const QString &name, const RpcFunc &action);
+ DELEGATE(page(), runJavaScript, run_javascript)
DEFINE_GETTER(get_id, id)
private:
diff --git a/src/widgets/WebViewStack.cpp b/src/widgets/WebViewStack.cpp
index 0ed2399..37532ff 100644
--- a/src/widgets/WebViewStack.cpp
+++ b/src/widgets/WebViewStack.cpp
@@ -342,6 +342,16 @@ void WebViewStack::set_html(const QString &html, WebViewId webview_id) {
webview->setHtml(html);
}
+void WebViewStack::run_javascript(const QString &js_code, WebViewId webview_id) {
+ auto *webview = get_webview(webview_id);
+ if (webview == nullptr) {
+ qDebug() << "Webview does not exist";
+ return;
+ }
+
+ webview->run_javascript(js_code);
+}
+
void WebViewStack::expose_rpc_function(const QString &name, const RpcFunc & /* unused */,
WebViewId /* unused */) {
qDebug() << "expose_rpc_function: NOT IMPLEMENTED" << name;
diff --git a/src/widgets/WebViewStack.hpp b/src/widgets/WebViewStack.hpp
index 27a1181..5a88266 100644
--- a/src/widgets/WebViewStack.hpp
+++ b/src/widgets/WebViewStack.hpp
@@ -62,6 +62,7 @@ public slots:
void scroll_to_top(WebViewId webview_id);
void scroll_to_bottom(WebViewId webview_id);
void set_html(const QString &html, WebViewId webview_id = 0) override;
+ void run_javascript(const QString &js_code, WebViewId webview_id) override;
void expose_rpc_function(const QString &name, const RpcFunc &action,
WebViewId webview_id) override;