我想阅读 Runtime.consoleAPICalled 或 Log.entryAdded,但我不知道如何实现这些回调
以下假设(根据您的问题措辞)您能够在对 Web 调试器端点开放的流上发送和接收调试协议消息。
在您发送调试协议消息Runtime.enable
和Log.enable
消息后,Runtime.consoleAPICalled
您Log.entryAdded
要查找的“事件”由您在同一调试通道上收到的消息表示。
您可能需要通过检查接收到的事件消息中的字段来将控制台事件消息与执行上下文(在Runtime.enable
响应中看到)进行匹配。executionContextId
日志事件不与任何单个执行上下文相关联。所有这些“事件”消息都会有Id=0
,这有助于识别它们是“事件”消息而不是响应消息。
以下是从 Chrome 收到的一些示例消息(格式为 JSON,具有任意字段顺序):
控制台 API 事件消息:
{
"method": "Runtime.consoleAPICalled",
"params":
{
"type": "warning",
"args": [
{ "type": "string",
"value": "Google Maps JavaScript API warning: NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keys"
}],
"executionContextId": 1,
"timestamp": 1618949706735.553,
"stackTrace":
{
"callFrames": [
{
"functionName": "TA.j",
"scriptId": "206",
"url": "https://maps.googleapis.com/maps-api-v3/api/js/44/10/util.js",
"lineNumber": 228,
"columnNumber": 26
} ]
}
}
},
"id": 0
}
记录事件消息:
{
"method":"Log.entryAdded",
"params":
{
"entry":
{
"source":"javascript",
"level":"warning",
"text":"The deviceorientation events are blocked by permissions policy. See https://github.com/w3c/webappsec-permissions-policy/blob/master/features.md#sensor-features",
"timestamp":1.6189509536801208e+12
}
},
"id": 0
}