Bug 142656

Summary: Web Inspector: Expose a way for developer provided object previews
Product: WebKit Reporter: Joseph Pecoraro <joepeck>
Component: Web InspectorAssignee: Joseph Pecoraro <joepeck>
Status: NEW ---    
Severity: Normal CC: 0.cynics-squints, graouts, inspector-bugzilla-changes, jonowells, webkit-bug-importer, ysuzuki
Priority: P2 Keywords: InRadar
Version: 528+ (Nightly build)   
Hardware: All   
OS: All   

Description Joseph Pecoraro 2015-03-12 23:44:04 PDT
* SUMMARY
Expose a way for developer provided object previews.

* IDEA (nvasilyev)

The idea here is that instead of just showing the first 5 properties of an object in Object Previews, we could show more useful previews based on what the developer of the object may deem most useful to expose.

For example an object might display poorly:

  js> obj
  ▶︎ MyObject {_root: Object, __owner: undefined, __hash: undefined...}

We can provide a hook for the developers to provide better previews. For example, we could check if the object has a Symbol.preview getter in the prototype chain, and if so use that object in the prototype chain

For example:

  MyObject.prototype = {
    toJSON() { ... },
    get [Symbol.preview]() { return this.toJSON(); },
  };

Would produce:

  js> obj
  ▶︎ MyObject {x: 2, y: 42, color: "red"}


* NOTES
- "Symbol.preview" is probably bad, since that is a built-in object.
- "console.preview" is likely better, after all this is for the "console" / debugger
Comment 1 Radar WebKit Bug Importer 2015-03-12 23:44:42 PDT
<rdar://problem/20149654>
Comment 2 Timothy Hatcher 2016-04-05 13:36:14 PDT
Chrome seems to do this with a global map hanging off window (window.devtoolsFormatters). Yuck.

https://docs.google.com/document/d/1FTascZXT9cxfetuPRT2eXPQKXui4nWFivUnS_335T3U/preview
Comment 3 Salam 2023-08-19 13:05:22 PDT
Hi,

Now that both Google Chrome[1] and Mozilla Firefox[2] implemented this feature, is it possible for WebKit/Safari to do the same so that developers (myself included) who use Safari as their primary browser for development can benefit from it as well? The community of developers that can benefit from this feature seem not so insignificant as evidenced by the backlash when the Google Chrome team made an announcement of removing this feature from Google Chrome[3].

Thanks.

[1] https://docs.google.com/document/d/1FTascZXT9cxfetuPRT2eXPQKXui4nWFivUnS_335T3U/
[2] https://fxdx.dev/firefox-devtools-custom-object-formatters/
[3] https://twitter.com/ChromeDevTools/status/1263421789671239681