Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose onJsDialog #428

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions include/QCefView.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ class QCEFVIEW_EXPORT QCefView : public QWidget
};
Q_ENUM(CefWindowOpenDisposition)

/// <summary>
/// Represents the CEF javascript dialog type
/// </summary>
enum CefJsDialogType
{
CefJsDialogTypeAlert = 0,
CefJsDialogTypeConfirm,
CefJsDialogTypePrompt
};
Q_ENUM(CefJsDialogType)

public:
/// <summary>
/// Constructs a QCefView instance
Expand Down Expand Up @@ -269,6 +280,26 @@ class QCEFVIEW_EXPORT QCefView : public QWidget
/// <returns>True to enable; otherwise false</returns>
bool isDragAndDropEnabled() const;

/// <summary>
/// Handles custom JavaScript dialogs
/// </summary>
/// <param name="originUrl">The URL of the page that triggered the dialog</param>
/// <param name="dialogType">The type of the JavaScript dialog (alert, confirm, prompt)</param>
/// <param name="messageText">The message to be displayed in the dialog</param>
/// <param name="defaultPromptText">The default text for the prompt dialog</param>
/// <param name="suppressMessage">Set to true to suppress the dialog</param>
/// <param name="callbackResult">A pair containing the result of the dialog and the user input</param>
/// <remarks>
/// This function is called when a JavaScript dialog is triggered. It allows for custom handling of the dialog,
/// including modifying the message, suppressing the dialog, and providing a custom result.
/// </remarks>
bool onJsDialog(const QString& originUrl,
QCefView::CefJsDialogType dialogType,
const QString& messageText,
const QString& defaultPromptText,
bool& suppressMessage,
QPair<bool, QString>& callbackResult);

signals:
/// <summary>
/// Gets called on loading state changed
Expand Down
36 changes: 36 additions & 0 deletions src/QCefView.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <QCefView.h>

#pragma region qt_headers
#include <QInputDialog>
#include <QMessageBox>
#include <QPainter>
#include <QPoint>
#include <QResizeEvent>
Expand Down Expand Up @@ -327,6 +329,40 @@ QCefView::onRequestCloseFromWeb()
return true;
}

bool
QCefView::onJsDialog(const QString& originUrl,
QCefView::CefJsDialogType dialogType,
const QString& messageText,
const QString& defaultPromptText,
bool& suppressMessage,
QPair<bool, QString>& callbackResult)
{
suppressMessage = false;

QMessageBox::StandardButton button = QMessageBox::NoButton;
if (dialogType == QCefView::CefJsDialogType::CefJsDialogTypeAlert) {
button = QMessageBox::warning(this, "Alert", messageText);
callbackResult.first = true;
callbackResult.second = "";
} else if (dialogType == QCefView::CefJsDialogType::CefJsDialogTypeConfirm) {
button = QMessageBox::question(this, "Confirm", messageText, QMessageBox::Yes | QMessageBox::No);
callbackResult.first = button == QMessageBox::Yes;
callbackResult.second = "";
} else if (dialogType == QCefView::CefJsDialogType::CefJsDialogTypePrompt) {
bool ok = false;
QString text = QInputDialog::getText(this, "Prompt", messageText, QLineEdit::Normal, defaultPromptText, &ok);
if (ok) {
callbackResult.first = true;
callbackResult.second = text;
} else {
callbackResult.first = false;
callbackResult.second = "";
}
}

return true;
}

void
QCefView::leaveEvent(QEvent* event)
{
Expand Down
8 changes: 8 additions & 0 deletions src/details/CCefClientDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ class CCefClientDelegate
#endif
CefRefPtr<CefFileDialogCallback> callback) override;

virtual bool OnJSDialog(CefRefPtr<CefBrowser> browser,
const CefString& origin_url,
CefJSDialogHandler::JSDialogType dialog_type,
const CefString& message_text,
const CefString& default_prompt_text,
CefRefPtr<CefJSDialogCallback> callback,
bool& suppress_message) override;

// DisplayHandler
virtual void addressChanged(CefRefPtr<CefBrowser>& browser, int64_t frameId, const std::string& url) override;
virtual void titleChanged(CefRefPtr<CefBrowser>& browser, const std::string& title) override;
Expand Down
37 changes: 37 additions & 0 deletions src/details/CCefClientDelegate_JSDialogHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "CCefClientDelegate.h"

#include <QDebug>
#include <QSharedPointer>
#include <QThread>

#include "QCefViewPrivate.h"
#include "utils/CommonUtils.h"
#include "utils/ValueConvertor.h"

bool
CCefClientDelegate::OnJSDialog(CefRefPtr<CefBrowser> browser,
const CefString& origin_url,
CefJSDialogHandler::JSDialogType dialog_type,
const CefString& message_text,
const CefString& default_prompt_text,
CefRefPtr<CefJSDialogCallback> callback,
bool& suppress_message)

{
bool ret = false;

QString originUrl = QString::fromStdString(origin_url.ToString());
QString messageText = QString::fromStdString(message_text.ToString());
QString defaultPromptText = QString::fromStdString(default_prompt_text.ToString());

Qt::ConnectionType c =
pCefViewPrivate_->q_ptr->thread() == QThread::currentThread() ? Qt::DirectConnection : Qt::BlockingQueuedConnection;
QMetaObject::invokeMethod(
pCefViewPrivate_,
[&]() {
ret = pCefViewPrivate_->onJsDialog(
originUrl, dialog_type, messageText, defaultPromptText, callback, suppress_message);
},
c);
return ret;
}
26 changes: 26 additions & 0 deletions src/details/QCefViewPrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,32 @@ QCefViewPrivate::onCefContextMenuDismissed()
osr.contextMenuCallback_ = nullptr;
}

bool
QCefViewPrivate::onJsDialog(const QString& origin_url,
int dialog_type,
const QString& message_text,
const QString& default_prompt_text,
CefRefPtr<CefJSDialogCallback> callback,
bool& suppress_message)
{
Q_Q(QCefView);

bool ret = false;

QPair<bool, QString> callbackResult;

ret = q->onJsDialog(origin_url,
(QCefView::CefJsDialogType)dialog_type,
message_text,
default_prompt_text,
suppress_message,
callbackResult);

callback->Continue(callbackResult.first, callbackResult.second.toStdString());

return ret;
}

void
QCefViewPrivate::onFileDialog(CefBrowserHost::FileDialogMode mode,
const CefString& title,
Expand Down
7 changes: 7 additions & 0 deletions src/details/QCefViewPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ public slots:

void onContextMenuDestroyed(QObject* obj);

bool onJsDialog(const QString& origin_url,
int dialog_type,
const QString& message_text,
const QString& default_prompt_text,
CefRefPtr<CefJSDialogCallback> callback,
bool& suppress_message);

signals:
void updateOsrFrame();

Expand Down
Loading