我正在尝试从 Qt 5 WebView 网页获取控制台日志输出,但我不知道该怎么做。也许外面的一些人可以帮助我?
我试图启用应该在您右键单击网页时显示的网络检查器,但是当我这样做时没有任何反应。我通过将环境变量 QTWEBKIT_INSPECTOR_SERVER 设置为 1111 设置了一个检查器端口(在 1111 上)。我可以得到一个页面,上面有这个:
Inspectable web views
LOG TEST [http://MY_LAN_IP:8880/logtest.html]
但是当我点击链接时,我得到了这个错误:
WebSocket connection to 'ws://localhost:1111/devtools/page/1' failed: Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received
inspector.js:341 Event {clipboardData: undefined, path: NodeList[0], cancelBubble: false, returnValue: true, srcElement: WebSocket…}
View.js:363 Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.
出于演示目的,我在本地 Web 服务器上有一个网页,如下所示:
<!DOCTYPE html>
<html>
<head>
<title>LOG TEST</title>
</head>
<body>
<h1>Logging to console...</h1>
<script>
setInterval(function() {
console.log("This is a log message");
console.error("This is an error message");
}, 1000);
</script>
</body>
</html>
QML 文件如下所示:
import QtQuick 2.2
import QtQuick.Window 2.1
import QtWebKit 3.0
import QtWebKit.experimental 1.0
Window {
visible: true
width: 360
height: 360
WebView {
url: "http://MY_LAN_IP:8880/logtest.html"
anchors.fill: parent
experimental.preferences.developerExtrasEnabled: true
experimental.preferences.navigatorQtObjectEnabled: false
}
}