Skip to content Skip to sidebar Skip to footer

Javascript: Retrieve File And Line Location Of Function During Runtime

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

Solution 1:

  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.

Post a Comment for "Javascript: Retrieve File And Line Location Of Function During Runtime"