以编程方式访问功能位置 [英] Access function location programmatically

查看:107
本文介绍了以编程方式访问功能位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在代码中是否可以访问使用控制台登录功能时Google chrome开发人员工具显示的 [ [[[FunctionLocation]]] 属性?

Is it possible in code to access ["[[FunctionLocation]]"] property that google chrome developer tools show when using console log on a function ?

推荐答案

目前的答案是

您在Inspector中看到的 [[FunctionLocation]] 属性已添加到 V8Debugger :: internalProperties() 在调试器的C ++代码中,使用另一个C ++函数 V8Debugger :: functionLocation() 来收集有关该函数的信息。然后, functionLocation()使用许多V8特定的C ++ API,例如 v8 :: Function :: GetScriptLineNumber() GetScriptColumnNumber() 来查找确切信息。

The [[FunctionLocation]] property you see in Inspector is added in V8Debugger::internalProperties() in the debugger's C++ code, which uses another C++ function V8Debugger::functionLocation() to gather information about the function. functionLocation() then uses a number of V8-specific C++ APIs such as v8::Function::GetScriptLineNumber() and GetScriptColumnNumber() to find out the exact information.

上述所有API仅可用于C ++代码,而不适用于JavaScript代码。换句话说,网页上的JavaScript代码无法直接访问此信息。

All APIs described above are exclusively available to C++ code, not JavaScript code. In other words, JavaScript code on the webpage does not have direct access to this information.

但是,您也许可以使用Chrome扩展程序访问属性。最近,Chrome使用的V8 JavaScript引擎增加了对通过 Chrome DevTools协议访问这些属性的支持。一个>。特别是,您可以通过 获取内部属性。 Runtime.getProperties 调用。此外,它似乎 Chrome扩展程序可以通过< a href = https://developer.chrome.com/extensions/debugger.html rel = noreferrer> chrome.debugger

However, you may be able to get access to the properties using a Chrome extension. More recently, the V8 JavaScript engine used by Chrome has added support to access these properties through the Chrome DevTools Protocol. In particular, you can get the internal properties through the Runtime.getProperties call. Additionally, it seems like Chrome extensions may be able to interact with the DevTools protocol through chrome.debugger.

在Node.js中使用DevTools协议的概念证明,该协议可使用检查器内置模块(穆罕默德在回答中有帮助提及):

A proof of concept for using the DevTools protocol in Node.js, which has direct access to the protocol using the Inspector built-in module (helpfully mentioned by Mohamed in their answer):

global.a = () => { /* test function */ };

const s = new (require('inspector').Session)();
s.connect();

let objectId;
s.post('Runtime.evaluate', { expression: 'a' }, (err, { result }) => {
  objectId = result.objectId;
});
s.post('Runtime.getProperties', { objectId }, (err, { internalProperties }) => {
  console.log(internalProperties);
});

收益率

[
  {
    name: '[[FunctionLocation]]',
    value: {
      type: 'object',
      subtype: 'internal#location',
      value: [Object],
      description: 'Object'
    }
  },
  {
    name: '[[Scopes]]',
    value: {
      type: 'object',
      subtype: 'internal#scopeList',
      className: 'Array',
      description: 'Scopes[2]',
      objectId: '{"injectedScriptId":1,"id":24}'
    }
  }
]

使用Node.js v12.3.1。

with Node.js v12.3.1.

这篇关于以编程方式访问功能位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆