mirror of
https://invent.kde.org/network/falkon.git
synced 2024-11-13 10:32:11 +01:00
Using WebPage and ExternalJsObject for communication with FxA Login page
Now Firefox Login page is opened in a WebPage object and the object used for communication on the webchannel is registered using ExternalJsObject::registerExtraObject.
This commit is contained in:
parent
4fda04bc70
commit
c1e49ec8a4
|
@ -1,11 +1,6 @@
|
||||||
new QWebChannel(qt.webChannelTransport, function(channel) {
|
|
||||||
communicator = channel.objects.communicator;
|
|
||||||
window.comm = communicator;
|
|
||||||
});
|
|
||||||
|
|
||||||
window.addEventListener('WebChannelMessageToChrome', function(event) {
|
window.addEventListener('WebChannelMessageToChrome', function(event) {
|
||||||
let e = {type: event.type, detail: event.detail};
|
let e = {type: event.type, detail: event.detail};
|
||||||
window.comm.receiveJSON(e);
|
external.extra.communicator.receiveJSON(e);
|
||||||
});
|
});
|
||||||
|
|
||||||
function sendMessage(response) {
|
function sendMessage(response) {
|
||||||
|
|
|
@ -16,6 +16,8 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
* ============================================================ */
|
* ============================================================ */
|
||||||
#include "fxalogin.h"
|
#include "fxalogin.h"
|
||||||
|
#include "javascript/externaljsobject.h"
|
||||||
|
#include "webpage.h"
|
||||||
|
|
||||||
#include <QWebEnginePage>
|
#include <QWebEnginePage>
|
||||||
#include <QWebEngineScript>
|
#include <QWebEngineScript>
|
||||||
|
@ -28,43 +30,32 @@
|
||||||
FxALoginPage::FxALoginPage(QWidget* parent)
|
FxALoginPage::FxALoginPage(QWidget* parent)
|
||||||
: QWebEngineView(parent)
|
: QWebEngineView(parent)
|
||||||
{
|
{
|
||||||
m_page = new QWebEnginePage(this);
|
m_communicator = new MessageReceiver(this);
|
||||||
m_channel = new QWebChannel(m_page);
|
ExternalJsObject::registerExtraObject(QString("communicator"), m_communicator);
|
||||||
m_page->setWebChannel(m_channel);
|
connect(m_communicator, &MessageReceiver::signalMessageReceived,
|
||||||
|
this, &FxALoginPage::slotMessageReceived);
|
||||||
|
|
||||||
|
m_page = new WebPage(this);
|
||||||
m_page->load(FxALoginUrl);
|
m_page->load(FxALoginUrl);
|
||||||
setPage(m_page);
|
setPage(m_page);
|
||||||
connect(m_page, &QWebEnginePage::loadFinished, this, &FxALoginPage::pageLoadFinished);
|
connect(m_page, &WebPage::loadFinished, this, &FxALoginPage::pageLoadFinished);
|
||||||
}
|
}
|
||||||
|
|
||||||
FxALoginPage::~FxALoginPage()
|
FxALoginPage::~FxALoginPage()
|
||||||
{
|
{
|
||||||
delete m_communicator;
|
delete m_communicator;
|
||||||
delete m_channel;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FxALoginPage::pageLoadFinished(bool pageLoaded)
|
void FxALoginPage::pageLoadFinished(bool pageLoaded)
|
||||||
{
|
{
|
||||||
if (pageLoaded) {
|
if (pageLoaded) {
|
||||||
QFile apiFile(":/qtwebchannel/qwebchannel.js");
|
|
||||||
if (!apiFile.open(QIODevice::ReadOnly)) {
|
|
||||||
qWarning() << "Couldn't load Qt's Webchannel API!";
|
|
||||||
}
|
|
||||||
QString apiScript = QString::fromUtf8(apiFile.readAll());
|
|
||||||
apiFile.close();
|
|
||||||
m_page->runJavaScript(apiScript);
|
|
||||||
|
|
||||||
m_communicator = new MessageReceiver(this);
|
|
||||||
connect(m_communicator, &MessageReceiver::signalMessageReceived,
|
|
||||||
this, &FxALoginPage::slotMessageReceived);
|
|
||||||
m_channel->registerObject(QString("communicator"), m_communicator);
|
|
||||||
|
|
||||||
QFile scriptFile(":/data/inject.js");
|
QFile scriptFile(":/data/inject.js");
|
||||||
if (!scriptFile.open(QIODevice::ReadOnly)) {
|
if (!scriptFile.open(QIODevice::ReadOnly)) {
|
||||||
qWarning() << "Couldn't load JavaScript file to inject.";
|
qWarning() << "Couldn't load JavaScript file to inject.";
|
||||||
}
|
}
|
||||||
QString injectScript = QString::fromUtf8(scriptFile.readAll());
|
QString injectScript = QString::fromUtf8(scriptFile.readAll());
|
||||||
scriptFile.close();
|
scriptFile.close();
|
||||||
m_page->runJavaScript(injectScript);
|
m_page->runJavaScript(injectScript, WebPage::SafeJsWorld);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,7 +106,7 @@ void FxALoginPage::sendMessage(QJsonObject msg)
|
||||||
qDebug() << "<<< Sending to server:\n " << stringMsg;
|
qDebug() << "<<< Sending to server:\n " << stringMsg;
|
||||||
|
|
||||||
QString srcCode = "sendMessage(" + stringMsg + ");";
|
QString srcCode = "sendMessage(" + stringMsg + ");";
|
||||||
m_page->runJavaScript(srcCode);
|
m_page->runJavaScript(srcCode, WebPage::SafeJsWorld);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,7 @@ class QWebEngineScript;
|
||||||
class QWebEnginePage;
|
class QWebEnginePage;
|
||||||
class QJsonObject;
|
class QJsonObject;
|
||||||
class MessageReceiver;
|
class MessageReceiver;
|
||||||
|
class WebPage;
|
||||||
|
|
||||||
class FxALoginPage : public QWebEngineView
|
class FxALoginPage : public QWebEngineView
|
||||||
{
|
{
|
||||||
|
@ -43,8 +44,7 @@ private:
|
||||||
void parseMessage(QJsonObject *msg);
|
void parseMessage(QJsonObject *msg);
|
||||||
void sendMessage(QJsonObject msg);
|
void sendMessage(QJsonObject msg);
|
||||||
|
|
||||||
QWebEnginePage *m_page;
|
WebPage *m_page;
|
||||||
QWebChannel *m_channel;
|
|
||||||
MessageReceiver *m_communicator;
|
MessageReceiver *m_communicator;
|
||||||
|
|
||||||
const QUrl FxALoginUrl = QUrl("https://accounts.firefox.com/signin?service=sync&context=fx_desktop_v3");
|
const QUrl FxALoginUrl = QUrl("https://accounts.firefox.com/signin?service=sync&context=fx_desktop_v3");
|
||||||
|
@ -60,10 +60,10 @@ public:
|
||||||
~MessageReceiver();
|
~MessageReceiver();
|
||||||
QJsonObject *getMessage();
|
QJsonObject *getMessage();
|
||||||
|
|
||||||
public slots:
|
public Q_SLOTS:
|
||||||
void receiveJSON(const QVariantMap &data);
|
void receiveJSON(const QVariantMap &data);
|
||||||
|
|
||||||
signals:
|
Q_SIGNALS:
|
||||||
void signalMessageReceived();
|
void signalMessageReceived();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user