From 8167ad4b337376526ab812d5ce28920b1bcf54c9 Mon Sep 17 00:00:00 2001 From: Kris Erickson Date: Wed, 23 May 2012 10:25:39 -0700 Subject: [PATCH 1/6] Update log.js --- log.js | 287 +++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 217 insertions(+), 70 deletions(-) diff --git a/log.js b/log.js index 6071756..cad478c 100644 --- a/log.js +++ b/log.js @@ -3,7 +3,7 @@ * * @author Craig Campbell */ -var ChromePhpLogger = function() +var ChromePhpLogger = function () { /** * @var string @@ -40,6 +40,11 @@ var ChromePhpLogger = function() return local_storage.show_line_numbers == "true"; } + function _expandObject() + { + return local_storage.expand_object == 'true'; + } + /** * should we show upgrade notification messages? * @@ -47,7 +52,8 @@ var ChromePhpLogger = function() */ function _showUpgradeMessages() { - if (local_storage.show_upgrade_messages === undefined) { + if (local_storage.show_upgrade_messages === undefined) + { return true; } return local_storage.show_upgrade_messages == "true"; @@ -62,7 +68,8 @@ var ChromePhpLogger = function() function _logCleanData(data, callback) { column_map = {}; - for (var key in data.columns) { + for (var key in data.columns) + { column_name = data.columns[key]; column_map[column_name] = key; } @@ -71,27 +78,32 @@ var ChromePhpLogger = function() i = 0, length = rows.length; - for (i = 0; i < length; ++i) { + for (i = 0; i < length; ++i) + { var row = rows[i], backtrace = row[column_map.backtrace], label = row[column_map.label], log = row[column_map.log], type = row[column_map.type] || 'log'; - if (_showLineNumbers() && backtrace !== null) { + if (_showLineNumbers() && backtrace !== null) + { console.log(backtrace); } var show_label = label && typeof label === "string"; - if (!label) { + if (!label) + { label = ""; } - if (log && typeof log === 'object' && log['___class_name']) { + if (log && typeof log === 'object' && log['___class_name']) + { show_label = true; - if (label) { + if (label) + { label += " "; } @@ -99,7 +111,13 @@ var ChromePhpLogger = function() delete log['___class_name']; } - switch (type) { + if (_expandObject()) + { + log = _dumpObject(log); + } +d + switch (type) + { case 'group': case 'groupEnd': case 'groupCollapsed': @@ -111,7 +129,8 @@ var ChromePhpLogger = function() case 'error': case 'info': case 'log': - if (show_label) { + if (show_label) + { console[type](label, log); break; } @@ -123,6 +142,93 @@ var ChromePhpLogger = function() callback(); } + + function _addSpaces(depth) + { + var spaces = ""; + for (var i = 0; i < depth; i++) + { + spaces += " "; + } + return spaces; + } + + function _dumpObject(object, depth, addNewLine) + { + depth = depth || 0; + addNewLine = addNewLine || false; + var newline = false, + dump = '', + content = '', + item, + key; + + if (typeof(object) == "undefined") + { + dump += "undefined"; + } + else if (typeof(object) == "boolean" || typeof(object) == "number") + { + dump += object.toString(); + } + else if (typeof(object) == "string") + { + dump += '"' + object + '"'; + } + else if (object == null) + { + dump += "null" + } + else if (object instanceof(Array)) + { + if (object.length > 0) + { + if (addNewLine) + { + newline = true + } + for (item in object) + { + if (object.hasOwnProperty(item)) + { + content += _dumpObject(object[item], depth + 1) + ",\n" + _addSpaces(depth + 1); + } + } + content = content.replace(/,\n\s*$/, "").replace(/^\s*/, ""); + dump += "[ " + content + "\n" + spacer(depth) + "]"; + } else { + dump += "[]" + } + } + else if (typeof(object) == "object") + { + if (Object.keys(object).length > 0) + { + if (addNewLine) + { + newline = true + } + for (key in object) + { + if (object.hasOwnProperty(key)) + { + content += spacer(depth + 1) + key.toString() + ": " + _dumpObject(object[key], depth + 2, true) + ",\n"; + } + } + content = content.replace(/,\n\s*$/, "").replace(/^\s+/, ""); + dump += "{ " + content + "\n" + spacer(depth) + "}"; + } else { + dump += "{}"; + } + } + else + { + dump += object.toString() + } + + return ((newline ? "\n" + spacer(depth) : "") + dump) + } + /** * handles data logging and determining which method to use to log data * @@ -131,7 +237,8 @@ var ChromePhpLogger = function() */ function _logData(data) { - if (data.version < "3.0") { + if (data.version < "3.0") + { console.warn("You are using version " + data.version + " of the ChromePHP Server Side Library. The latest version of the extension requires version 3.0 or later. Please upgrade at http://www.chromephp.com."); return; } @@ -139,11 +246,14 @@ var ChromePhpLogger = function() return _logCleanData(data, _complete); } - function _complete() {} + function _complete() + { + } function _processQueue(callback) { - for (var i = 0; i < queue.length; ++i) { + for (var i = 0; i < queue.length; ++i) + { _process(queue[i]); } @@ -163,24 +273,29 @@ var ChromePhpLogger = function() return data; } - function _decode(header) { + function _decode(header) + { return _jsonDecode(Base64.decode(header)); } - function _process(details) { + function _process(details) + { var headers = details.responseHeaders, match = false, header = ''; - for (var i = 0; i < headers.length; ++i) { - if (headers[i].name.toLowerCase() == HEADER_NAME) { + for (var i = 0; i < headers.length; ++i) + { + if (headers[i].name.toLowerCase() == HEADER_NAME) + { header = headers[i].value; match = true; break; } } - if (!match) { + if (!match) + { return; } @@ -188,13 +303,16 @@ var ChromePhpLogger = function() _logData(data); } - function _handleHeaderUpdate(request, sender, sendResponse) { + function _handleHeaderUpdate(request, sender, sendResponse) + { // if this is not a cookie update don't do anything - if (request.name != "header_update") { + if (request.name != "header_update") + { return; } - if (use_queue) { + if (use_queue) + { queue.push(request.details); return sendResponse("done"); } @@ -203,11 +321,13 @@ var ChromePhpLogger = function() return sendResponse("done"); } - function _listenForLogMessages() { + function _listenForLogMessages() + { chrome.extension.onRequest.addListener(_handleHeaderUpdate); } - function _stopListening() { + function _stopListening() + { chrome.extension.onRequest.removeListener(_handleHeaderUpdate); } @@ -217,9 +337,10 @@ var ChromePhpLogger = function() * * @return void */ - run : function() + run:function () { - _processQueue(function() { + _processQueue(function () + { use_queue = false; }); }, @@ -229,9 +350,10 @@ var ChromePhpLogger = function() * * @return void */ - initStorage : function() + initStorage:function () { - chrome.extension.sendRequest("localStorage", function(response) { + chrome.extension.sendRequest("localStorage", function (response) + { local_storage = response; ChromePhpLogger.run(); }); @@ -242,18 +364,20 @@ var ChromePhpLogger = function() * * @return void */ - init : function() + init:function () { _listenForLogMessages(); - chrome.extension.sendRequest("isActive", function(response) { - if (response === false) { + chrome.extension.sendRequest("isActive", function (response) + { + if (response === false) + { return _stopListening(); } return ChromePhpLogger.initStorage(); }); } }; -} (); +}(); ChromePhpLogger.init(); @@ -267,7 +391,7 @@ var Util = { * @param string * @return string */ - trim : function(text) + trim:function (text) { return (text || "").replace(/^(\s|\u00A0)+|(\s|\u00A0)+$/g, ""); }, @@ -280,9 +404,9 @@ var Util = { * @param int offset - start searching only after this point in the string * @return int or false */ - strpos : function(haystack, needle, offset) + strpos:function (haystack, needle, offset) { - var i = (haystack+'').indexOf(needle, (offset || 0)); + var i = (haystack + '').indexOf(needle, (offset || 0)); return i === -1 ? false : i; }, @@ -292,16 +416,19 @@ var Util = { * @param string name of cookie * @return value */ - getCookie : function(name) + getCookie:function (name) { var cookie_value = null; - if (document.cookie && document.cookie != '') { + if (document.cookie && document.cookie != '') + { var cookies = document.cookie.split(';'); - for (var i = 0; i < cookies.length; i++) { + for (var i = 0; i < cookies.length; i++) + { var cookie = this.trim(cookies[i]); // Does this cookie string begin with the name we want? - if (cookie.substring(0, name.length + 1) == (name + '=')) { + if (cookie.substring(0, name.length + 1) == (name + '=')) + { cookie_value = decodeURIComponent(cookie.substring(name.length + 1)); break; } @@ -316,7 +443,7 @@ var Util = { * @param string name of cookie * @return void */ - eatCookie : function(name) + eatCookie:function (name) { document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;'; }, @@ -327,15 +454,17 @@ var Util = { * @param mixed needle * @param array haystack */ - inArray : function(needle, haystack) - { - for (var key in haystack) { - if (haystack[key] == needle) { + inArray:function (needle, haystack) + { + for (var key in haystack) + { + if (haystack[key] == needle) + { return true; } } return false; - } + } }; /** @@ -347,17 +476,19 @@ var Util = { var Base64 = { // private property - _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", + _keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", // public method for encoding - encode : function (input) { + encode:function (input) + { var output = ""; var chr1, chr2, chr3, enc1, enc2, enc3, enc4; var i = 0; input = Base64._utf8_encode(input); - while (i < input.length) { + while (i < input.length) + { chr1 = input.charCodeAt(i++); chr2 = input.charCodeAt(i++); @@ -368,15 +499,17 @@ var Base64 = { enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); enc4 = chr3 & 63; - if (isNaN(chr2)) { + if (isNaN(chr2)) + { enc3 = enc4 = 64; - } else if (isNaN(chr3)) { + } else if (isNaN(chr3)) + { enc4 = 64; } output = output + - this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + - this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4); + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4); } @@ -384,7 +517,8 @@ var Base64 = { }, // public method for decoding - decode : function (input) { + decode:function (input) + { var output = ""; var chr1, chr2, chr3; var enc1, enc2, enc3, enc4; @@ -392,7 +526,8 @@ var Base64 = { input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); - while (i < input.length) { + while (i < input.length) + { enc1 = this._keyStr.indexOf(input.charAt(i++)); enc2 = this._keyStr.indexOf(input.charAt(i++)); @@ -405,10 +540,12 @@ var Base64 = { output = output + String.fromCharCode(chr1); - if (enc3 != 64) { + if (enc3 != 64) + { output = output + String.fromCharCode(chr2); } - if (enc4 != 64) { + if (enc4 != 64) + { output = output + String.fromCharCode(chr3); } @@ -421,22 +558,27 @@ var Base64 = { }, // private method for UTF-8 encoding - _utf8_encode : function (string) { - string = string.replace(/\r\n/g,"\n"); + _utf8_encode:function (string) + { + string = string.replace(/\r\n/g, "\n"); var utftext = ""; - for (var n = 0; n < string.length; n++) { + for (var n = 0; n < string.length; n++) + { var c = string.charCodeAt(n); - if (c < 128) { + if (c < 128) + { utftext += String.fromCharCode(c); } - else if((c > 127) && (c < 2048)) { + else if ((c > 127) && (c < 2048)) + { utftext += String.fromCharCode((c >> 6) | 192); utftext += String.fromCharCode((c & 63) | 128); } - else { + else + { utftext += String.fromCharCode((c >> 12) | 224); utftext += String.fromCharCode(((c >> 6) & 63) | 128); utftext += String.fromCharCode((c & 63) | 128); @@ -448,31 +590,36 @@ var Base64 = { }, // private method for UTF-8 decoding - _utf8_decode : function (utftext) { + _utf8_decode:function (utftext) + { var string = ""; var i = 0; var c = c1 = c2 = 0; - while ( i < utftext.length ) { + while (i < utftext.length) + { c = utftext.charCodeAt(i); - if (c < 128) { + if (c < 128) + { string += String.fromCharCode(c); i++; } - else if((c > 191) && (c < 224)) { - c2 = utftext.charCodeAt(i+1); + else if ((c > 191) && (c < 224)) + { + c2 = utftext.charCodeAt(i + 1); string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); i += 2; } - else { - c2 = utftext.charCodeAt(i+1); - c3 = utftext.charCodeAt(i+2); + else + { + c2 = utftext.charCodeAt(i + 1); + c3 = utftext.charCodeAt(i + 2); string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); i += 3; } } return string; } -}; +}; \ No newline at end of file From ebabc295995ef6e62afb47049aa0417801a23326 Mon Sep 17 00:00:00 2001 From: Kris Erickson Date: Wed, 23 May 2012 10:26:20 -0700 Subject: [PATCH 2/6] Update options.html --- options.html | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/options.html b/options.html index 6f1a575..40bc158 100644 --- a/options.html +++ b/options.html @@ -58,11 +58,13 @@ function save_options() { - var line_numbers = document.getElementById("show_line_numbers"); - localStorage["show_line_numbers"] = line_numbers.checked; - var upgrade_messages = document.getElementById("show_upgrade_messages"); - localStorage["show_upgrade_messages"] = upgrade_messages.checked; + localStorage["show_line_numbers"] = document.getElementById("show_line_numbers").checked; + + localStorage["show_upgrade_messages"] = document.getElementById("show_upgrade_messages").checked; + + localStorage["expand_object"] = document.getElementById("expand_object").checked; + var info_div = document.getElementById("info"); info_div.innerHTML = "your settings have been saved"; @@ -80,6 +82,10 @@ var upgrade_messages = document.getElementById("show_upgrade_messages"); upgrade_messages.checked = localStorage["show_upgrade_messages"] == "true" ? true : false; + var expand_object = document.getElementById("expand_object"); + expand_object.checked = localStorage["expand_object"] == "true" ? true : false; + + if (localStorage["show_upgrade_messages"] === undefined) { upgrade_messages.checked = true; } @@ -94,6 +100,12 @@ var upgrade_messages = document.getElementById("show_upgrade_messages"); localStorage["show_upgrade_messages"] = true; upgrade_messages.checked = localStorage["show_upgrade_messages"] == "true" ? true : false; + + var expand_object = document.getElementById("expand_object"); + localStorage['expand_object'] = false; + expand_object.checked = localStorage["expand_object"] == "true" ? true : false; + + } @@ -116,10 +128,16 @@

ChromePhp Options

show upgrade messages: + +

expand object when displaying the object

+
- + \ No newline at end of file From 801d2715af0fc60e58460273a06f2889d2ec3316 Mon Sep 17 00:00:00 2001 From: Kris Date: Fri, 12 Apr 2013 15:39:50 -0700 Subject: [PATCH 3/6] Merge from upstream and Add Toast on warnings. --- chromelogger-options.js | 2 + chromelogger.js | 15 ++++- log.js | 130 ++++++++++------------------------------ manifest.json | 4 ++ options.html | 10 +++- 5 files changed, 56 insertions(+), 105 deletions(-) diff --git a/chromelogger-options.js b/chromelogger-options.js index 538b0c5..2af259e 100644 --- a/chromelogger-options.js +++ b/chromelogger-options.js @@ -5,6 +5,8 @@ var defaults = { show_upgrade_messages: true, show_line_numbers: false, + toast_warnings: false, + toast_errors: false, color1: "#888", color2: "#0563ad" }; diff --git a/chromelogger.js b/chromelogger.js index 89ec390..ea4f540 100644 --- a/chromelogger.js +++ b/chromelogger.js @@ -130,13 +130,24 @@ }, {urls: [""]}, ["responseHeaders"]); chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { - if (request === "localStorage") { + if (request.type === "localStorage") { return sendResponse(localStorage); } - if (request === "isActive") { + if (request.type === "isActive") { return sendResponse(active); } + if (request.type == 'toast') { + var notification = webkitNotifications.createNotification( + 'icon48.png', // icon url - can be relative + request.toastType, // notification title + request.message + ); + + // Then show the notification. + notification.show(); + } + }); } diff --git a/log.js b/log.js index be8af89..6928432 100644 --- a/log.js +++ b/log.js @@ -49,10 +49,27 @@ return local_storage.show_line_numbers === "true"; } + + function _toastWarnings() + { + return local_storage.toast_warnings === "true"; + } + + function _toastErrors() + { + return local_storage.toast_errors === "true"; + } + + function _toast(type, logs) { + var message = logs[logs.length - 1].substr(0, 50); + chrome.extension.sendMessage({type: 'toast', toastType: type, message: message}); + } + /** * logs nicely formatted data in new format * - * @param Object + * @param data Object + * @param callback function * @return void */ function _logData(data, callback) @@ -61,12 +78,14 @@ var column_name; for (var key in data.columns) { - column_name = data.columns[key]; - column_map[column_name] = key; + if (data.columns.hasOwnProperty(key)) { + column_name = data.columns[key]; + column_map[column_name] = key; + } } var rows = data.rows, - i = 0, + i, length = rows.length; for (i = 0; i < length; i++) { @@ -110,11 +129,6 @@ last_log = logs[logs.length - 1]; - if (_expandObject()) { - log = _dumpObject(log); - } - - if (typeof current_log === 'object' && current_log['___class_name']) { new_string = '%c' + current_log['___class_name']; @@ -141,8 +155,10 @@ } console[type].apply(console, logs); - if (type == 'warn') { - + if (type == 'warn' && _toastWarnings()) { + _toast('Warning', logs) + } else if (type == 'error' && _toastErrors()) { + _toast('Error', logs) } } @@ -152,92 +168,6 @@ } - function _addSpaces(depth) - { - var spaces = ""; - for (var i = 0; i < depth; i++) - { - spaces += " "; - } - return spaces; - } - - function _dumpObject(object, depth, addNewLine) - { - depth = depth || 0; - addNewLine = addNewLine || false; - var newline = false, - dump = '', - content = '', - item, - key; - - if (typeof(object) == "undefined") - { - dump += "undefined"; - } - else if (typeof(object) == "boolean" || typeof(object) == "number") - { - dump += object.toString(); - } - else if (typeof(object) == "string") - { - dump += '"' + object + '"'; - } - else if (object == null) - { - dump += "null" - } - else if (object instanceof(Array)) - { - if (object.length > 0) - { - if (addNewLine) - { - newline = true - } - for (item in object) - { - if (object.hasOwnProperty(item)) - { - content += _dumpObject(object[item], depth + 1) + ",\n" + _addSpaces(depth + 1); - } - } - content = content.replace(/,\n\s*$/, "").replace(/^\s*/, ""); - dump += "[ " + content + "\n" + spacer(depth) + "]"; - } else { - dump += "[]" - } - } - else if (typeof(object) == "object") - { - if (Object.keys(object).length > 0) - { - if (addNewLine) - { - newline = true - } - for (key in object) - { - if (object.hasOwnProperty(key)) - { - content += spacer(depth + 1) + key.toString() + ": " + _dumpObject(object[key], depth + 2, true) + ",\n"; - } - } - content = content.replace(/,\n\s*$/, "").replace(/^\s+/, ""); - dump += "{ " + content + "\n" + spacer(depth) + "}"; - } else { - dump += "{}"; - } - } - else - { - dump += object.toString() - } - - return ((newline ? "\n" + spacer(depth) : "") + dump) - } - function _processQueue(callback) { for (var i = 0; i < queue.length; ++i) { @@ -251,7 +181,7 @@ /** * converts a string to json * - * @param string cookie + * @param json_string string * @return Object */ function _jsonDecode(json_string) @@ -315,7 +245,7 @@ } function _initStorage() { - chrome.extension.sendMessage("localStorage", function(response) { + chrome.extension.sendMessage({type: "localStorage"}, function(response) { local_storage = response; color1 = 'color1' in local_storage ? local_storage['color1'] : color1; color2 = 'color2' in local_storage ? local_storage['color2'] : color2; @@ -325,7 +255,7 @@ function _init() { _listenForLogMessages(); - chrome.extension.sendMessage("isActive", function(response) { + chrome.extension.sendMessage({type: "isActive"}, function(response) { if (response === false) { return _stopListening(); } diff --git a/manifest.json b/manifest.json index 6665414..9b3e9fe 100644 --- a/manifest.json +++ b/manifest.json @@ -27,8 +27,12 @@ "permissions": [ "webRequest", + "notifications", "tabs", "http://*/*", "https://*/*" + ], + "web_accessible_resources": [ + "icon48.png" ] } diff --git a/options.html b/options.html index 8c7364b..026a876 100644 --- a/options.html +++ b/options.html @@ -105,11 +105,15 @@

Chrome Logger Options

show upgrade messages when your server side library is out of date

+ -

expand object when displaying the object

+

show alerts for

+

show alerts for

- + +

max alerts per page