Search code examples
javascriptgoogle-chromedevtools

Copy object with function as a property in DevTools


When copying an object with function as a property in the chrome console, the property is lost at all. How to use copy() in DevTools and copy a whole property without losing anything?


Solution

  • The only way I see about copying json with function property to clipboard

    const isArrowFn = (fn) => 
      (typeof fn === 'function') &&
      !/^(?:(?:\/\*[^(?:\*\/)]*\*\/\s*)|(?:\/\/[^\r\n]*))*\s*(?:(?:(?:async\s(?:(?:\/\*[^(?:\*\/)]*\*\/\s*)|(?:\/\/[^\r\n]*))*\s*)?function|class)(?:\s|(?:(?:\/\*[^(?:\*\/)]*\*\/\s*)|(?:\/\/[^\r\n]*))*)|(?:[_$\w][\w0-9_$]*\s*(?:\/\*[^(?:\*\/)]*\*\/\s*)*\s*\())/.test(fn.toString());
    
    
    copy(
      JSON.parse(
        JSON.stringify(obj, (key, val) => {
          if (typeof val === "function")
            return !isArrowFn(val)
              ? val.toString().substring(
                  val.toString().indexOf("{") + 1,
                  val.toString().lastIndexOf("}")
                )
              : val.toString();
          return val;
        })
      )
    );