问题
I am trying to get the file and line location of a function I saved in an object. If I am logging the object to the Chrome Dev Tools I see this:
Can I somehow from inside the code access [[FunctionLocation]]
? Or is this something the Chrome Dev Tools add to the object? If so, is it possible to retrieve the function location when I am developing a Chrome Dev Tools extension or a Chrome extension?
回答1:
You still cannot access the internal properties through JavaScript.
The information is exposed through the Chrome DevTools Protocol, as an InternalPropertyDescriptor. This is observable through for example Node.js:
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); });
yields
[ { 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}' } } ]
with Node.js v12.3.1.
Chrome extensions may interact with the Chrome DevTools protocol through chrome.debugger. I am not familiar with how DevTools extensions work, but you may find the Integrating with DevTools page to be insightful.
来源:https://stackoverflow.com/questions/56066523/javascript-retrieve-file-and-line-location-of-function-during-runtime