Slide 1

Slide 1 text

Modern JavaScript Debugging Ashutosh Sharma Adobe

Slide 2

Slide 2 text

About Me •  Senior Computer Scientist at Adobe •  Web Platform & Authoring Team @zorder deceptiveweb.com

Slide 3

Slide 3 text

Agenda •  The Right Tools •  Modern Debugging Techniques •  Browser Tools •  Debugging HTML5 Features •  Tips and Tricks

Slide 4

Slide 4 text

The Right Tools •  Static code analyzers – JSLint, JSHint •  Browser and built-in tools – Firebug – Chrome Devtools – Web Inspector – Opera Dragonfly – IE Developer Tools DEMOS

Slide 5

Slide 5 text

Modern Debugging Techniques

Slide 6

Slide 6 text

Source Maps •  Debug minified JavaScript files •  Debug compiled-to-JavaScript languages •  Debug CSS pre-processor files •  Syntax //@ sourceMappingURL=library.min.js.map •  Generating a source map –  Closure compiler –  Grunt JSMin SourceMaps plugin –  UglifyJS –  Multi-level source maps (CoffeeScript/TypeScript) DEMO

Slide 7

Slide 7 text

DOM Breakpoints •  Break on DOM mutation & attribute changes DEMO

Slide 8

Slide 8 text

MutationObservers •  Vs DOM Mutation Events (deprecated) •  Syntax var addedNodes = []; var observer = new WebKitMutationObserver(function(mutations) { mutations.forEach(function(mutation) { for(var i = 0; i < mutation.addedNodes.length; i++) addedNodes.push(mutation.addedNodes[i]); }) }); observer.observe(document, { subtree: true, childList: true }); console.log(addedNodes); •  Mutation-Summary library DEMO

Slide 9

Slide 9 text

Object.observe •  Proposal •  Track when an object, or its properties, change •  Syntax Object.observe(obj, callback); DEMO

Slide 10

Slide 10 text

Event Listener Breakpoints •  e.g. load, click, scroll DEMO

Slide 11

Slide 11 text

Event Listener Inspection •  Event listeners on specific DOM nodes DEMO

Slide 12

Slide 12 text

Conditional Breakpoints •  console.log, console.assert, console.trace can be used in breakpoint conditions DEMO

Slide 13

Slide 13 text

Debugging Mobile Applications •  Remote Debugging – Chrome, Firefox – Weinre, Edge Inspect – Aardwolf •  jsconsole.com •  Overrides –  User Agent –  Device Metrics –  Geolocation –  Device Orientation –  Emulate Touch Events

Slide 14

Slide 14 text

Browser Tools

Slide 15

Slide 15 text

Console API •  Firebug, Chrome Devtools •  console. – log, warn, error – group, groupEnd – trace – assert – time, timeEnd DEMO

Slide 16

Slide 16 text

Command Line API •  Firebug, Chrome Devtools •  $(selector), $$(selector), $x(xpath) •  profile, profileEnd •  inspect •  monitorEvents, unmonitorEvents DEMO

Slide 17

Slide 17 text

Chrome Devtools •  Useful Keyboard Shortcuts – Esc Bring the console up – Cmd-O Search file names – Cmd-Shift-O Search for functions (.js) and CSS selectors (.css) – Cmd-Opt-F Search in all source files – Cmd-1-8 / Cmd-[ / Cmd-] Navigate between panels – Ctrl-, / Ctrl-. Previous/Next call frame – Shift-Enter Multiline JavaScript – Cmd-F (Resources Panel) Search in all resources DEMO

Slide 18

Slide 18 text

Firebug •  Log function calls •  Break on cookie changes

Slide 19

Slide 19 text

Edit and Continue (JS) •  Edit JavaScript without a page refresh – External scripts can be modified •  Existing function references are replaced •  Useful for modifying callbacks on-the-fly DEMO

Slide 20

Slide 20 text

Debugging HTML5 Features

Slide 21

Slide 21 text

Debugging HTML5 Features •  Canvas •  Web Workers •  WebSockets •  Offline Data –  LocalStorage –  SessionStorage –  IndexedDB –  Web SQL Database –  FileSystem

Slide 22

Slide 22 text

Canvas DEMO

Slide 23

Slide 23 text

Web Workers DEMO

Slide 24

Slide 24 text

WebSockets DEMO

Slide 25

Slide 25 text

Offline Data – Web Storage DEMO

Slide 26

Slide 26 text

Offline Data – IndexedDB, WebSQL DEMO

Slide 27

Slide 27 text

Offline Data - FileSystem DEMO

Slide 28

Slide 28 text

Tips and Tricks for Effective Debugging

Slide 29

Slide 29 text

jQuery •  Visual Event (bookmarklet) •  Browser extensions – Chrome •  jQuery Debugger •  Always Inject jQuery – Firefox •  FireQuery (Firebug extension) DEMO

Slide 30

Slide 30 text

Miscellaneous •  $0 - $4 •  $_ •  debugger; •  Pretty-print •  Disable cache •  Versioning .js files •  console.log(JSON.stringify(…)) •  Initiating JS code for network requests DEMO

Slide 31

Slide 31 text

Miscellaneous •  Bookmarklets to inject code into pages – DevTools Code Snippets •  DevTools workspaces and local files •  Firebug Lite for IE (compat mode in IE 10) •  Chrome record and playback modes –  open -a "Google Chrome" --args --record-mode –  open -a "Google Chrome" --args --playback-mode •  performance.now() DEMO

Slide 32

Slide 32 text

Demo and Source Code http://deceptiveweb.com/js-debug/

Slide 33

Slide 33 text

References •  JSLint, JSHint –  http://www.jslint.com/ –  http://jshint.com/ •  Mutation-Summary library –  https://code.google.com/p/mutation-summary/ •  Weinre –  http://people.apache.org/~pmuellr/weinre/ •  Adobe Edge Inspect –  http://html.adobe.com/edge/inspect/ •  Aardwolf –  https://github.com/lexandera/Aardwolf

Slide 34

Slide 34 text

References •  JSConsole.com –  http://jsconsole.com/ •  Visual Event 2 –  http://www.sprymedia.co.uk/article/Visual+Event+2 •  jQuery Debugger –  https://chrome.google.com/webstore/detail/jquery-debugger/ dbhhnnnpaeobfddmlalhnehgclcmjimi •  Always Inject jQuery –  https://chrome.google.com/webstore/detail/always-inject-jquery/ neappnndmnboecdfhflpbmfdhkanpaoa •  FireQuery –  https://addons.mozilla.org/en-us/firefox/addon/firequery/

Slide 35

Slide 35 text

Q & A