2011-10-29 12:24:12 +02:00
|
|
|
/* ============================================================
|
2017-08-25 17:11:29 +02:00
|
|
|
* Falkon - Qt web browser
|
2017-01-21 17:59:54 +01:00
|
|
|
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
|
2011-10-29 12:24:12 +02:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
* ============================================================ */
|
2014-03-31 14:54:08 +02:00
|
|
|
#include "webinspector.h"
|
2015-08-30 14:44:00 +02:00
|
|
|
#include "mainapplication.h"
|
2015-09-29 11:45:39 +02:00
|
|
|
#include "networkmanager.h"
|
2017-01-21 17:59:54 +01:00
|
|
|
#include "settings.h"
|
2011-10-14 20:14:57 +02:00
|
|
|
|
2015-08-30 14:44:00 +02:00
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QNetworkReply>
|
2015-01-27 11:01:52 +01:00
|
|
|
|
2015-08-30 14:44:00 +02:00
|
|
|
QList<QWebEngineView*> WebInspector::s_views;
|
2011-10-14 20:14:57 +02:00
|
|
|
|
2015-08-30 14:44:00 +02:00
|
|
|
WebInspector::WebInspector(QWidget *parent)
|
|
|
|
: QWebEngineView(parent)
|
2015-10-15 17:19:16 +02:00
|
|
|
, m_view(Q_NULLPTR)
|
2014-03-13 12:47:01 +01:00
|
|
|
{
|
2015-09-29 23:22:33 +02:00
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
2014-03-31 17:46:59 +02:00
|
|
|
setObjectName(QSL("web-inspector"));
|
|
|
|
setMinimumHeight(80);
|
2015-08-30 14:44:00 +02:00
|
|
|
|
2017-01-21 17:59:54 +01:00
|
|
|
m_height = Settings().value(QSL("Web-Inspector/height"), 80).toInt();
|
2017-01-22 14:50:15 +01:00
|
|
|
m_windowSize = Settings().value(QSL("Web-Inspector/windowSize"), QSize(640, 480)).toSize();
|
2017-01-21 17:59:54 +01:00
|
|
|
|
2015-08-30 14:44:00 +02:00
|
|
|
registerView(this);
|
|
|
|
|
|
|
|
connect(page(), &QWebEnginePage::windowCloseRequested, this, &WebInspector::deleteLater);
|
2015-10-15 17:19:16 +02:00
|
|
|
connect(page(), &QWebEnginePage::loadFinished, this, &WebInspector::loadFinished);
|
2014-03-13 12:47:01 +01:00
|
|
|
}
|
|
|
|
|
2015-08-30 14:44:00 +02:00
|
|
|
WebInspector::~WebInspector()
|
2012-03-16 16:57:24 +01:00
|
|
|
{
|
2015-08-30 14:44:00 +02:00
|
|
|
unregisterView(this);
|
2017-01-21 17:59:54 +01:00
|
|
|
|
2017-01-22 14:50:15 +01:00
|
|
|
if (isWindow()) {
|
|
|
|
Settings().setValue(QSL("Web-Inspector/windowSize"), size());
|
|
|
|
} else {
|
|
|
|
Settings().setValue(QSL("Web-Inspector/height"), height());
|
|
|
|
}
|
2011-10-14 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
2015-08-30 14:44:00 +02:00
|
|
|
void WebInspector::setView(QWebEngineView *view)
|
2014-04-01 16:34:47 +02:00
|
|
|
{
|
2015-10-15 17:19:16 +02:00
|
|
|
m_view = view;
|
2016-02-20 15:07:36 +01:00
|
|
|
Q_ASSERT(isEnabled());
|
2015-10-15 17:19:16 +02:00
|
|
|
|
2016-02-20 15:07:36 +01:00
|
|
|
int port = qEnvironmentVariableIntValue("QTWEBENGINE_REMOTE_DEBUGGING");
|
|
|
|
QUrl inspectorUrl = QUrl(QSL("http://localhost:%1").arg(port));
|
2015-10-15 17:19:16 +02:00
|
|
|
int index = s_views.indexOf(m_view);
|
2014-04-01 16:34:47 +02:00
|
|
|
|
2015-08-30 14:44:00 +02:00
|
|
|
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();
|
|
|
|
});
|
2014-04-01 16:34:47 +02:00
|
|
|
}
|
|
|
|
|
2015-10-15 17:19:16 +02:00
|
|
|
void WebInspector::inspectElement()
|
|
|
|
{
|
|
|
|
m_inspectElement = true;
|
|
|
|
}
|
|
|
|
|
2016-02-20 15:07:36 +01:00
|
|
|
bool WebInspector::isEnabled()
|
|
|
|
{
|
|
|
|
return qEnvironmentVariableIsSet("QTWEBENGINE_REMOTE_DEBUGGING");
|
|
|
|
}
|
|
|
|
|
2015-08-30 14:44:00 +02:00
|
|
|
void WebInspector::pushView(QWebEngineView *view)
|
2011-10-14 20:14:57 +02:00
|
|
|
{
|
2015-08-30 14:44:00 +02:00
|
|
|
s_views.removeOne(view);
|
|
|
|
s_views.prepend(view);
|
2011-10-14 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
2015-08-30 14:44:00 +02:00
|
|
|
void WebInspector::registerView(QWebEngineView *view)
|
2011-10-14 20:14:57 +02:00
|
|
|
{
|
2015-08-30 14:44:00 +02:00
|
|
|
s_views.prepend(view);
|
|
|
|
}
|
2014-03-31 17:46:59 +02:00
|
|
|
|
2015-08-30 14:44:00 +02:00
|
|
|
void WebInspector::unregisterView(QWebEngineView *view)
|
|
|
|
{
|
|
|
|
s_views.removeOne(view);
|
2011-10-14 20:14:57 +02:00
|
|
|
}
|
2015-01-27 11:01:52 +01:00
|
|
|
|
2015-10-15 17:19:16 +02:00
|
|
|
void WebInspector::loadFinished()
|
2015-08-30 14:44:00 +02:00
|
|
|
{
|
2015-10-15 17:21:51 +02:00
|
|
|
// Show close button only when docked
|
|
|
|
if (!isWindow()) {
|
|
|
|
page()->runJavaScript(QL1S("var toolbar = document.getElementsByClassName('inspector-view-toolbar')[1];"
|
|
|
|
"var button = document.createElement('button');"
|
|
|
|
"button.style.width = '22px';"
|
|
|
|
"button.style.height = '22px';"
|
|
|
|
"button.style.border = 'none';"
|
|
|
|
"button.style.cursor = 'pointer';"
|
|
|
|
"button.style.background = 'url(qrc:html/close.png) no-repeat';"
|
|
|
|
"button.style.backgroundPosition = 'center center';"
|
|
|
|
"button.addEventListener('click', function() {"
|
|
|
|
" window.close();"
|
|
|
|
"});"
|
|
|
|
"toolbar.appendChild(button);"));
|
|
|
|
}
|
2015-10-15 17:19:16 +02:00
|
|
|
|
|
|
|
// Inspect element
|
|
|
|
if (m_inspectElement) {
|
|
|
|
m_view->triggerPageAction(QWebEnginePage::InspectElement);
|
|
|
|
m_inspectElement = false;
|
|
|
|
}
|
2015-08-30 14:44:00 +02:00
|
|
|
}
|
2015-10-04 19:44:25 +02:00
|
|
|
|
2017-01-21 17:59:54 +01:00
|
|
|
QSize WebInspector::sizeHint() const
|
|
|
|
{
|
2017-01-22 14:50:15 +01:00
|
|
|
if (isWindow()) {
|
|
|
|
return m_windowSize;
|
|
|
|
}
|
2017-01-21 17:59:54 +01:00
|
|
|
QSize s = QWebEngineView::sizeHint();
|
|
|
|
s.setHeight(m_height);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-10-04 19:44:25 +02:00
|
|
|
void WebInspector::keyPressEvent(QKeyEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event)
|
|
|
|
// Stop propagation
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebInspector::keyReleaseEvent(QKeyEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event)
|
|
|
|
// Stop propagation
|
|
|
|
}
|