2

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:

enter image description here

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?

Johannes Klauß
  • 10,676
  • 16
  • 68
  • 122
  • 1
    [This should better be able to help answer your question](https://stackoverflow.com/questions/41146373/access-function-location-programmatically), but it appears to be no. – Brett Fuller May 09 '19 at 21:17
  • Looks like you asked a two part question and rather than start another thread the second part of your questions was not answered by the above link and is what I am curious about as well. 1. Can you access it in dev tools from the console? The link says no, mind you that was a couple years ago. 2. Are we able to access that from within an extension script? I have searched the APIs but cannot find it referenced and am developing an extension and I would like to find functions locations as part of the data points I am exposing. – mike-pz Aug 07 '19 at 13:11

1 Answers1

2
  1. You still cannot access the internal properties through JavaScript.

  2. 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.

Timothy Gu
  • 3,727
  • 28
  • 35