1
mirror of https://invent.kde.org/network/falkon.git synced 2024-09-23 02:32:10 +02:00
falkonOfficial/src/lib/webengine/webinspector.cpp
David Rosca 6857270057 WebInspector: Fix searching with Ctrl+F in inspector
Don't eat key events so inspector's search function can be used.
2015-10-04 19:44:25 +02:00

102 lines
3.1 KiB
C++

/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "webinspector.h"
#include "mainapplication.h"
#include "networkmanager.h"
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QNetworkReply>
QList<QWebEngineView*> WebInspector::s_views;
WebInspector::WebInspector(QWidget *parent)
: QWebEngineView(parent)
{
setAttribute(Qt::WA_DeleteOnClose);
setObjectName(QSL("web-inspector"));
setMinimumHeight(80);
registerView(this);
connect(page(), &QWebEnginePage::windowCloseRequested, this, &WebInspector::deleteLater);
connect(page(), &QWebEnginePage::loadFinished, this, &WebInspector::updateCloseButton);
}
WebInspector::~WebInspector()
{
unregisterView(this);
}
void WebInspector::setView(QWebEngineView *view)
{
QUrl inspectorUrl = QUrl(QSL("http://localhost:%1").arg(WEBINSPECTOR_PORT));
int index = s_views.indexOf(view);
QNetworkReply *reply = mApp->networkManager()->get(QNetworkRequest(inspectorUrl.resolved(QUrl("json/list"))));
connect(reply, &QNetworkReply::finished, this, [=]() {
QJsonArray clients = QJsonDocument::fromJson(reply->readAll()).array();
QUrl pageUrl;
if (clients.size() > index) {
QJsonObject object = clients.at(index).toObject();
pageUrl = inspectorUrl.resolved(QUrl(object.value(QSL("devtoolsFrontendUrl")).toString()));
}
load(pageUrl);
pushView(this);
show();
});
}
void WebInspector::pushView(QWebEngineView *view)
{
s_views.removeOne(view);
s_views.prepend(view);
}
void WebInspector::registerView(QWebEngineView *view)
{
s_views.prepend(view);
}
void WebInspector::unregisterView(QWebEngineView *view)
{
s_views.removeOne(view);
}
void WebInspector::updateCloseButton()
{
page()->runJavaScript(QL1S("var button = document.getElementsByClassName('toolbar-close-button-item')[0];"
"button.style.display = 'inline-block';"
"button.addEventListener('click', function() {"
" window.close();"
"})"));
}
void WebInspector::keyPressEvent(QKeyEvent *event)
{
Q_UNUSED(event)
// Stop propagation
}
void WebInspector::keyReleaseEvent(QKeyEvent *event)
{
Q_UNUSED(event)
// Stop propagation
}