| 1 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | |||||||||||||
| 4 | package Plack::Middleware::Debug::Ajax; | ||||||||||||
| 5 | { | ||||||||||||
| 6 | 2 | $Plack::Middleware::Debug::Ajax::VERSION = '0.02'; | |||||||||||
| 7 | } | ||||||||||||
| 8 | |||||||||||||
| 9 | 2 | ▶ | use parent 'Plack::Middleware::Debug::Base'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 2 | ▶ | use Plack::Util::Accessor qw(log_limit); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | =head1 NAME | ||||||||||||
| 14 | |||||||||||||
| 15 | Plack::Middleware::Debug::Ajax - Show log of ajax requests/responses | ||||||||||||
| 16 | |||||||||||||
| 17 | =head1 VERSION | ||||||||||||
| 18 | |||||||||||||
| 19 | version 0.02 | ||||||||||||
| 20 | |||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||
| 22 | |||||||||||||
| 23 | To activate this panel: | ||||||||||||
| 24 | |||||||||||||
| 25 | builder { | ||||||||||||
| 26 | enable 'Debug', panels => | ||||||||||||
| 27 | [ | ||||||||||||
| 28 | qw(Parameters Memory), | ||||||||||||
| 29 | [ 'Ajax', | ||||||||||||
| 30 | log_limit => 100, | ||||||||||||
| 31 | ] | ||||||||||||
| 32 | ]; | ||||||||||||
| 33 | $app; | ||||||||||||
| 34 | } | ||||||||||||
| 35 | |||||||||||||
| 36 | If you're using Dancer, you can enable the panel via your config.yml: | ||||||||||||
| 37 | |||||||||||||
| 38 | plack_middlewares: | ||||||||||||
| 39 | - | ||||||||||||
| 40 | - Debug | ||||||||||||
| 41 | - panels | ||||||||||||
| 42 | - | ||||||||||||
| 43 | - Parameters | ||||||||||||
| 44 | - Memory | ||||||||||||
| 45 | - Ajax | ||||||||||||
| 46 | |||||||||||||
| 47 | |||||||||||||
| 48 | =head1 DESCRIPTION | ||||||||||||
| 49 | |||||||||||||
| 50 | Adds a debug panel that logs ajax requests and responses through jQuery, as | ||||||||||||
| 51 | they happen. Only ajax requests that go through jQuery are logged, because the | ||||||||||||
| 52 | logging relies on jQuery's global event handlers. If you make an ajax call with | ||||||||||||
| 53 | global set to false, the event won't be logged: | ||||||||||||
| 54 | |||||||||||||
| 55 | $.ajax({ | ||||||||||||
| 56 | // ... | ||||||||||||
| 57 | global: false, | ||||||||||||
| 58 | }); | ||||||||||||
| 59 | |||||||||||||
| 60 | You could use this feature to selectively log messages, if you don't make use | ||||||||||||
| 61 | of global ajax event handlers elsewhere. | ||||||||||||
| 62 | |||||||||||||
| 63 | Note that ajax events are logged as they happen, so responses won't necessarily | ||||||||||||
| 64 | appear directly above their respective requests. Events are shown newest first. | ||||||||||||
| 65 | |||||||||||||
| 66 | =head1 SETTINGS | ||||||||||||
| 67 | |||||||||||||
| 68 | =over 4 | ||||||||||||
| 69 | |||||||||||||
| 70 | =item log_limit | ||||||||||||
| 71 | |||||||||||||
| 72 | Limit the number of logged ajax requests and responses to the specified number. | ||||||||||||
| 73 | When the log exceeds this size, the oldest items are deleted to make room for | ||||||||||||
| 74 | new items. The default limit is 50, to avoid hogging memory with large | ||||||||||||
| 75 | responses. | ||||||||||||
| 76 | |||||||||||||
| 77 | =back | ||||||||||||
| 78 | |||||||||||||
| 79 | =head1 STANDALONE DEMO | ||||||||||||
| 80 | |||||||||||||
| 81 | There is a complete standalone demo of the Ajax panel in the source of this | ||||||||||||
| 82 | distribution. To run the demo, download the tarball for this distribution, | ||||||||||||
| 83 | extract it, cd into it, and run: | ||||||||||||
| 84 | |||||||||||||
| 85 | $ plackup sample/app.psgi | ||||||||||||
| 86 | |||||||||||||
| 87 | Then point your browser to: | ||||||||||||
| 88 | |||||||||||||
| 89 | http://0:5000/ | ||||||||||||
| 90 | |||||||||||||
| 91 | And play around with it. | ||||||||||||
| 92 | |||||||||||||
| 93 | =head1 JQUERY VERSIONS | ||||||||||||
| 94 | |||||||||||||
| 95 | Requires jQuery 1.4 or later for full functionality, and has been tested with | ||||||||||||
| 96 | the following versions: | ||||||||||||
| 97 | |||||||||||||
| 98 | =over 4 | ||||||||||||
| 99 | |||||||||||||
| 100 | =item 1.9.1 | ||||||||||||
| 101 | |||||||||||||
| 102 | =item 1.8.3 | ||||||||||||
| 103 | |||||||||||||
| 104 | =item 1.6.4 | ||||||||||||
| 105 | |||||||||||||
| 106 | =item 1.5.1 | ||||||||||||
| 107 | |||||||||||||
| 108 | =item 1.4.1 | ||||||||||||
| 109 | |||||||||||||
| 110 | =back | ||||||||||||
| 111 | |||||||||||||
| 112 | It mostly works with jQuery 1.1 and greater, and doesn't work at all with | ||||||||||||
| 113 | jQuery 1.0. | ||||||||||||
| 114 | |||||||||||||
| 115 | =head1 BUGS | ||||||||||||
| 116 | |||||||||||||
| 117 | Report any bugs via F<http://rt.cpan.org>. Pull requests welcome on github: | ||||||||||||
| 118 | F<http://github.com/doublecreations/plack-middleware-debug-ajax/> | ||||||||||||
| 119 | |||||||||||||
| 120 | =head1 AUTHOR | ||||||||||||
| 121 | |||||||||||||
| 122 | Vincent Launchbury <vincent@cpan.org> | ||||||||||||
| 123 | |||||||||||||
| 124 | =head1 COPYRIGHT | ||||||||||||
| 125 | |||||||||||||
| 126 | Copyright (C) 2013 Vincent Launchbury | ||||||||||||
| 127 | |||||||||||||
| 128 | This program is free software; you can redistribute it and/or modify it under | ||||||||||||
| 129 | the same terms as Perl itself. | ||||||||||||
| 130 | |||||||||||||
| 131 | =head1 SEE ALSO | ||||||||||||
| 132 | |||||||||||||
| 133 | L<Plack::Middleware::Debug> | ||||||||||||
| 134 | |||||||||||||
| 135 | =cut | ||||||||||||
| 136 | |||||||||||||
| 137 | 0 | ▶ | sub run { | ||||||||||
|
Subroutine
run: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 138 | 0 | my ($self, $env, $panel) = @_; | |||||||||||
| 139 | |||||||||||||
| 140 | ## | ||||||||||||
| 141 | ## Settings | ||||||||||||
| 142 | ## | ||||||||||||
| 143 | |||||||||||||
| 144 | 0 | ▶ | my $log_limit = $self->log_limit || 50; | ||||||||||
|
Condition0%0/2
$self->log_limit || 50
MC/DC0%0/1
$self->log_limit || 50
$self->log_limit
Truth table0%0/2
A
A= $self->log_limit
| |||||||||||||
| 145 | |||||||||||||
| 146 | ## | ||||||||||||
| 147 | ## Set titles | ||||||||||||
| 148 | ## | ||||||||||||
| 149 | |||||||||||||
| 150 | 0 | $panel->title("Ajax Log"); | |||||||||||
| 151 | 0 | $panel->nav_title("Ajax Log"); | |||||||||||
| 152 | 0 | $panel->nav_subtitle("Live jQuery logging"); | |||||||||||
| 153 | |||||||||||||
| 154 | |||||||||||||
| 155 | ## | ||||||||||||
| 156 | ## Table for our panel | ||||||||||||
| 157 | ## | ||||||||||||
| 158 | |||||||||||||
| 159 | 0 | my $status_table = $self->render_list_pairs( | |||||||||||
| 160 | [ "Status" => "jQuery not present, or document not yet loaded" ], | ||||||||||||
| 161 | ); | ||||||||||||
| 162 | |||||||||||||
| 163 | |||||||||||||
| 164 | ## | ||||||||||||
| 165 | ## Javascript/CSS/HTML | ||||||||||||
| 166 | ## | ||||||||||||
| 167 | |||||||||||||
| 168 | 0 | my $id = $panel->dom_id; | |||||||||||
| 169 | 0 | $panel->content(<<"EOF"); | |||||||||||
| 170 | <script type="text/javascript"> | ||||||||||||
| 171 | // If we have jQuery | ||||||||||||
| 172 | if (typeof jQuery != 'undefined') { | ||||||||||||
| 173 | // On load | ||||||||||||
| 174 | \$(function() { | ||||||||||||
| 175 | |||||||||||||
| 176 | // Log an ajax request/response | ||||||||||||
| 177 | function logAjax(data) { | ||||||||||||
| 178 | \$('#${id}-ajaxTable > tbody').prepend( | ||||||||||||
| 179 | \$('<tr>').append( | ||||||||||||
| 180 | \$('<td>').append(data.type), | ||||||||||||
| 181 | \$('<td>').append(\$('<textarea>').val(data.request)), | ||||||||||||
| 182 | \$('<td>').append(\$('<textarea>').val(data.response)) | ||||||||||||
| 183 | ).css("background-color", data.color) | ||||||||||||
| 184 | ); | ||||||||||||
| 185 | |||||||||||||
| 186 | // Trim log (0-indexed) | ||||||||||||
| 187 | \$('#${id}-ajaxTable > tbody > tr:eq($log_limit)').remove(); | ||||||||||||
| 188 | \$('#${id}-ajaxTable > tbody > tr:gt($log_limit)').remove(); | ||||||||||||
| 189 | } | ||||||||||||
| 190 | |||||||||||||
| 191 | // Make a table | ||||||||||||
| 192 | function mk_table(header, rows) { | ||||||||||||
| 193 | var table = "<table><tr>"; | ||||||||||||
| 194 | \$(header).each(function(k, v) { | ||||||||||||
| 195 | table += "<th>"+v+"</th>"; | ||||||||||||
| 196 | }) | ||||||||||||
| 197 | table += "</tr>"; | ||||||||||||
| 198 | |||||||||||||
| 199 | \$(rows).each(function() { | ||||||||||||
| 200 | table += "<tr>"; | ||||||||||||
| 201 | \$(this).each(function(k , v) { | ||||||||||||
| 202 | table += "<td>"+v+"</td>"; | ||||||||||||
| 203 | }) | ||||||||||||
| 204 | table += "</tr>"; | ||||||||||||
| 205 | }) | ||||||||||||
| 206 | table += "</table>"; | ||||||||||||
| 207 | return table; | ||||||||||||
| 208 | } | ||||||||||||
| 209 | |||||||||||||
| 210 | // For GET ajax requests we want to move the parameters to data | ||||||||||||
| 211 | // so we can display long requests in a textarea | ||||||||||||
| 212 | function move_get_params_to_data(settings) { | ||||||||||||
| 213 | var obj = new Object(); | ||||||||||||
| 214 | obj.url = settings.url; | ||||||||||||
| 215 | obj.data = settings.data; | ||||||||||||
| 216 | |||||||||||||
| 217 | if (settings.type == "GET") { | ||||||||||||
| 218 | var pieces = settings.url.split('?'); | ||||||||||||
| 219 | obj.url = pieces.shift(); | ||||||||||||
| 220 | obj.data = "GET paramaters: " + pieces.join('&'); | ||||||||||||
| 221 | } | ||||||||||||
| 222 | return obj; | ||||||||||||
| 223 | } | ||||||||||||
| 224 | |||||||||||||
| 225 | // On every request | ||||||||||||
| 226 | \$(document).ajaxSend(function(event, xhr, settings) { | ||||||||||||
| 227 | var url_and_data = move_get_params_to_data(settings); | ||||||||||||
| 228 | logAjax({ | ||||||||||||
| 229 | type: mk_table( | ||||||||||||
| 230 | [ "Attr", "Value" ], | ||||||||||||
| 231 | [ | ||||||||||||
| 232 | [ "Type:", "Ajax Request" ], | ||||||||||||
| 233 | [ "Date:", new Date() ], | ||||||||||||
| 234 | [ "URL:", url_and_data.url ], | ||||||||||||
| 235 | [ "Method:", settings.type ], | ||||||||||||
| 236 | [ "Async:", settings.async ] | ||||||||||||
| 237 | ] | ||||||||||||
| 238 | ), | ||||||||||||
| 239 | request: url_and_data.data, | ||||||||||||
| 240 | response: "", | ||||||||||||
| 241 | color: "#EBFCFA" | ||||||||||||
| 242 | }); | ||||||||||||
| 243 | }); | ||||||||||||
| 244 | |||||||||||||
| 245 | // On every success | ||||||||||||
| 246 | \$(document).ajaxSuccess(function(event, xhr, settings) { | ||||||||||||
| 247 | var url_and_data = move_get_params_to_data(settings); | ||||||||||||
| 248 | logAjax({ | ||||||||||||
| 249 | type: mk_table( | ||||||||||||
| 250 | ["Attr", "Value"], | ||||||||||||
| 251 | [ | ||||||||||||
| 252 | [ "Type:", "Ajax Success" ], | ||||||||||||
| 253 | [ "Date:", new Date() ], | ||||||||||||
| 254 | [ "URL:", url_and_data.url], | ||||||||||||
| 255 | [ "Method:", settings.type], | ||||||||||||
| 256 | [ "Status:", xhr.status] | ||||||||||||
| 257 | ] | ||||||||||||
| 258 | ), | ||||||||||||
| 259 | request: url_and_data.data, | ||||||||||||
| 260 | response: xhr.responseText, | ||||||||||||
| 261 | color: "#D6FFD1" | ||||||||||||
| 262 | }); | ||||||||||||
| 263 | }); | ||||||||||||
| 264 | |||||||||||||
| 265 | // On every failure | ||||||||||||
| 266 | \$(document).ajaxError(function(event, xhr, settings, exception) { | ||||||||||||
| 267 | var url_and_data = move_get_params_to_data(settings); | ||||||||||||
| 268 | logAjax({ | ||||||||||||
| 269 | type: mk_table( | ||||||||||||
| 270 | ["Attr", "Value"], | ||||||||||||
| 271 | [ | ||||||||||||
| 272 | [ "Type:", "Ajax Error" ], | ||||||||||||
| 273 | [ "Date:", new Date() ], | ||||||||||||
| 274 | [ "URL:", url_and_data.url], | ||||||||||||
| 275 | [ "Method:", settings.type], | ||||||||||||
| 276 | [ "Status:", xhr.status] | ||||||||||||
| 277 | ] | ||||||||||||
| 278 | ), | ||||||||||||
| 279 | request: url_and_data.data, | ||||||||||||
| 280 | response: exception, | ||||||||||||
| 281 | color: "#FAC0CE" | ||||||||||||
| 282 | }); | ||||||||||||
| 283 | }); | ||||||||||||
| 284 | |||||||||||||
| 285 | // Update status row | ||||||||||||
| 286 | \$('#${id} table:first tbody tr:first td:last').text( | ||||||||||||
| 287 | "jQuery found, listening for ajax events." | ||||||||||||
| 288 | ); | ||||||||||||
| 289 | }); | ||||||||||||
| 290 | } | ||||||||||||
| 291 | </script> | ||||||||||||
| 292 | <style type="text/css"> | ||||||||||||
| 293 | /* attr/val table */ | ||||||||||||
| 294 | #${id}-ajaxTable table { | ||||||||||||
| 295 | width: 100%; | ||||||||||||
| 296 | height: 90%; | ||||||||||||
| 297 | min-height: 100px; | ||||||||||||
| 298 | margin: 10px 0; | ||||||||||||
| 299 | padding:0; | ||||||||||||
| 300 | } | ||||||||||||
| 301 | #${id}-ajaxTable table td:first-child, | ||||||||||||
| 302 | #${id}-ajaxTable table th:first-child { | ||||||||||||
| 303 | width:20%; | ||||||||||||
| 304 | } | ||||||||||||
| 305 | #${id}-ajaxTable table th { | ||||||||||||
| 306 | text-align:left !important; | ||||||||||||
| 307 | } | ||||||||||||
| 308 | #${id}-ajaxTable tbody > tr > td{ | ||||||||||||
| 309 | height: 100%; | ||||||||||||
| 310 | } | ||||||||||||
| 311 | |||||||||||||
| 312 | /* Text area */ | ||||||||||||
| 313 | #${id}-ajaxTable textarea { | ||||||||||||
| 314 | width: 100%; | ||||||||||||
| 315 | height: 90%; | ||||||||||||
| 316 | margin: 10px 0; | ||||||||||||
| 317 | padding:0; | ||||||||||||
| 318 | min-height: 100px; | ||||||||||||
| 319 | border: 1px solid #ccc; | ||||||||||||
| 320 | } | ||||||||||||
| 321 | |||||||||||||
| 322 | \@media (max-width: 768px) { | ||||||||||||
| 323 | /* Hide Type/Request/Response header */ | ||||||||||||
| 324 | #${id}-ajaxTable > thead > tr { | ||||||||||||
| 325 | display:none; | ||||||||||||
| 326 | } | ||||||||||||
| 327 | /* But show info anyway */ | ||||||||||||
| 328 | #${id}-ajaxTable > tbody > tr > td:nth-child(2):before { | ||||||||||||
| 329 | content: "Request:"; | ||||||||||||
| 330 | } | ||||||||||||
| 331 | #${id}-ajaxTable > tbody > tr > td:nth-child(3):before { | ||||||||||||
| 332 | content: "Response:"; | ||||||||||||
| 333 | } | ||||||||||||
| 334 | |||||||||||||
| 335 | /* Display as blocks so <td>'s show below each other */ | ||||||||||||
| 336 | #${id}-ajaxTable > tbody > tr, | ||||||||||||
| 337 | #${id}-ajaxTable > tbody > tr > td { | ||||||||||||
| 338 | display: block; | ||||||||||||
| 339 | text-align: left; | ||||||||||||
| 340 | } | ||||||||||||
| 341 | |||||||||||||
| 342 | /* Show some dividers */ | ||||||||||||
| 343 | #${id}-ajaxTable > tbody > tr { | ||||||||||||
| 344 | border-top: 1px solid black; | ||||||||||||
| 345 | border-bottom: 1px solid black; | ||||||||||||
| 346 | padding-bottom: 10px; | ||||||||||||
| 347 | margin-bottom: 20px; | ||||||||||||
| 348 | } | ||||||||||||
| 349 | } | ||||||||||||
| 350 | |||||||||||||
| 351 | </style> | ||||||||||||
| 352 | $status_table | ||||||||||||
| 353 | <table id="${id}-ajaxTable"> | ||||||||||||
| 354 | <thead> | ||||||||||||
| 355 | <tr> | ||||||||||||
| 356 | <th>Type</th> | ||||||||||||
| 357 | <th>Request</th> | ||||||||||||
| 358 | <th>Response</th> | ||||||||||||
| 359 | </tr> | ||||||||||||
| 360 | <thead> | ||||||||||||
| 361 | <tbody> | ||||||||||||
| 362 | </tbody> | ||||||||||||
| 363 | </table> | ||||||||||||
| 364 | EOF | ||||||||||||
| 365 | } | ||||||||||||
| 366 | |||||||||||||
| 367 | 2 | 1; | |||||||||||