|
1 | 1 | # 8. Debugging |
2 | 2 |
|
3 | | -Nobody is perfect, and we all make mistakes. |
4 | | -Some of them are easy to spot and can easily caught by our editor, Typescript or ESLint. Some bugs are real evil and hard to find. |
| 3 | +Bugs happen — some are simple and can be caught by your editor, TypeScript, or ESLint; others are trickier. The browser developer tools are your best friend for tracking them down. Press `F12` to open the tools; they may look intimidating at first, but you'll get comfortable quickly. |
5 | 4 |
|
6 | | -Our browser will help us: hit `F12` to open the developer tools. |
7 | | -If you see it the first time it might look overwhelming. Don't be afraid you'll soon master it. |
| 5 | +The `Elements` (Firefox: `Inspector`) panel helps when the problem is layout or HTML. Here we focus on the `Console` and `Sources` (Firefox: `Debugger`) tabs for debugging code. |
8 | 6 |
|
9 | | -'Elements' (FF:'inspector') is helpful if something is wrong in your html-code/design or if you want playing with styles. |
10 | | -Here we will focus on the tabs 'console' and 'sources' (FF:'debugger') because that's for code. |
| 7 | +## Console |
11 | 8 |
|
12 | | -## Tab - Console |
| 9 | +We already used the console in the examples: `console.log("CountPortals " + VERSION);`. IITC produces a bunch of Log-Lines but you should find our message `CountPortals v0.0.0`, quite at the beginning. |
13 | 10 |
|
14 | | -We already made use of it: `console.log("CountPortals " + VERSION);` |
15 | | -If you scroll through the console messages (when the iitc-page and our tutorial plugin is loaded) |
16 | | -there should be a line "CountPortals v0.0.0". This was generated by that command. |
| 11 | +The console supports different message levels: `debug`, `info`, `log`, `warn`, and `error`. Which to use is up to you, but `log` is usually sufficient for general-purpose messages. Use `console.warn(...)` and `console.error(...)` for important issues — they are highlighted in the console. |
17 | 12 |
|
18 | | -Not an amazing message, but it tells use: plugin is loaded and 'init' was called. |
19 | | - |
20 | | -There are different 'level' of messages you can generate: `debug`, `info`, `log`, `warn`, `error` |
21 | | -Where to use debug, info or log is upto you. Usually you can stay with 'log' and ignore the others. |
22 | | - |
23 | | -Keep `console.warn("data is missing");` and `console.error("this should never happen");` for terrible cases. They are also displayed in different colors to draw your attention. |
24 | | -There is another one I like to use: `console.assert( t > 0, "t must be greater then 0");` a conditional error message. |
25 | | - |
26 | | -Instead of pure text you can output objects too. |
| 13 | +You can also log objects to inspect them: |
27 | 14 |
|
28 | 15 | ```typescript |
29 | 16 | doCount(): void { |
30 | | - console.log("Current portal:"); |
31 | | - console.log(window.portals[selectedPortal]); |
| 17 | + console.log("Current portal:", window.portals[selectedPortal]); |
32 | 18 | [...] |
33 | 19 | ``` |
34 | | -When you now open our dialog you'll see the data of current selected portal. If one is selected else you'll only get an "undefined". |
35 | | -
|
36 | | -In the last line of this tab you can directly run javascript commands. |
37 | | -All iitc-plugins are stored in the object plugin, so for calling our dialog we can run: |
38 | | -`> plugin.CountPortals.doCount(); ` |
| 20 | +Opening the dialog after logging the portal object will show its data in the console (or `undefined` if nothing is selected). |
39 | 21 |
|
40 | | -When searching for a bug: put `console.log` commands on every line that could provide a hint. |
41 | | -Watch the log while the script is running and check if everything is like expected. |
| 22 | +You can also execute JavaScript directly from the console prompt. For example, plugins are exposed on the global `plugin` object, so you can call: |
| 23 | +`> plugin.CountPortals.doCount();` |
42 | 24 |
|
43 | | -## Tab-Source |
| 25 | +When hunting bugs, sprinkle `console.log` statements in places that might reveal the problem and watch the output as the code runs. |
44 | 26 |
|
45 | | -While logging is easy it can still be hard to understand why something went wrong. |
| 27 | +## Sources |
46 | 28 |
|
47 | | -The 'Source' (or 'Debugger') Tab let us investigate our code directly. |
48 | | -Search our code in the tree view on the left side: iitc_plugin_CountPortals/main.ts (in firefox:webpack/iitc_plugin_CountPortals/main.ts) |
| 29 | +Logging helps, but stepping through code with a debugger often reveals the root cause faster. The `Sources` (or `Debugger` in Firefox) tab lets you browse your source files. |
49 | 30 |
|
50 | | -When selecting it you'll see our code appearing in the middle window. Okay, we know our source. That doesn't help much BUT we can set breakpoints. |
51 | | -Scroll to our 'doCount' function and click on the line number of the first line in that function (or press ctrl-b). |
| 31 | +Find `iitc_plugin_CountPortals/main.ts` (in Firefox look under `webpack/iitc_plugin_CountPortals/main.ts`) and open it. You can set breakpoints by clicking the line numbers (or using your browser's shortcut to toggle a breakpoint). |
52 | 32 |
|
53 | | -Now hit our "count" link in IITC to trigger that function and the debugger should stop executing at our breakpoint. |
54 | | -On the lower right side you'll see the "callstack" (= which function calls were done till reaching that point) and "watches". |
55 | | -There you can add variables of interest (or just hover the mouse over your code). |
56 | | -Above that are shortcuts for stepping through your code (f8->run; f10->execute one line; f11->follow deeper into the next function) |
| 33 | +Trigger the action (e.g., click the plugin's count link) and execution will pause at the breakpoint. The debugger shows the call stack and lets you inspect variables — add watches or hover variables to view values. Use the step controls to run, step over, or step into functions (browser shortcuts vary: e.g., F8 to resume, F10 to step over, F11 to step into). |
57 | 34 |
|
58 | 35 | ## Mobile |
59 | 36 |
|
60 | | -Sometimes bugs only appear on mobile devices. |
| 37 | +Some bugs only surface on mobile devices. IITCm provides a built-in console you can enable once via `Settings -> Advanced Settings -> Configure IITCm menu -> check 'debug'`. |
61 | 38 |
|
62 | | -Here we got a console too! How to make it visible: |
63 | | -enable it once: settings -> adv. settings -> Configure IITCm menu -> check 'debug' |
| 39 | +The `debug` menu item opens a compact console that shows logs and provides a prompt to run JavaScript. |
64 | 40 |
|
65 | | -The `debug` in your IITC Menu will open a small version of the console you know from your desktop browser. On the bottom line is a button to minimize the log and a line to execute js-commands. |
| 41 | +For a more powerful setup, use remote debugging. |
66 | 42 |
|
67 | | -This helps a little bit, but wait, we do it even better: |
| 43 | +### Remote debugging |
68 | 44 |
|
69 | | -### Remote debug |
| 45 | +To debug a live mobile session, connect your phone via USB and enable USB debugging (on Android: tap the build number several times to enable Developer Options, then enable USB debugging). |
70 | 46 |
|
71 | | -Connect your phone via USB to your PC. |
72 | | -- Enable USB debugging (settings->about-> click "build-number" multiple times; then system->adv->Dev.Options->USB-Debug) |
73 | | -- start 'chrome' and open the page: "chrome://inspect/#devices" |
74 | | -- on your phone an "allow connection?" dialog should pop up -> allow |
75 | | -- if you now run IITC on your phone it should appear under your device in chrome |
76 | | -(if you got connection problems check if other USB-debuggers are running or switch USB port) |
77 | | -- hit "inspect" and enjoy |
| 47 | +Open Chrome on your desktop and go to `chrome://inspect/#devices`. Approve the connection on your phone when prompted. Your running IITC page should appear under the device; click `inspect` to open the remote DevTools for the mobile page. |
78 | 48 |
|
79 | | -The view is a little bit different, but you've all debug possibility as it would run on your desktop. |
| 49 | +The remote tools provide the same debugging capabilities as on desktop. If the device doesn't appear try another USB port. |
80 | 50 |
|
81 | | -Getting your plugin to the mobile device can be tricky sometimes. |
82 | | -I prefer the adb way (once installed) -> `adb push dist/myplugin.dev.user.js /mnt/sdcard/IITC_Mobile/plugins` |
| 51 | +The Android-SDK has a tool (ADB) that can help copy your plugin to the device: |
| 52 | +`adb push dist/myplugin.dev.user.js /mnt/sdcard/IITC_Mobile/plugins` |
83 | 53 |
|
0 commit comments