From 09d534c176651244994db24978fa5c084fe70eb5 Mon Sep 17 00:00:00 2001 From: Nihanth Subramanya Date: Thu, 4 Dec 2014 09:25:11 -0500 Subject: [PATCH] Version 0.3 Updated to work with the latest version of Instantbird. Now uses the "preparing-message" observer notification API to replace links. --- bootstrap.js | 52 ++++++++++++++++++++++++++++++---------------------- install.rdf | 6 +++--- 2 files changed, 33 insertions(+), 25 deletions(-) mode change 100644 => 100755 bootstrap.js mode change 100644 => 100755 install.rdf diff --git a/bootstrap.js b/bootstrap.js old mode 100644 new mode 100755 index 1dce0f8..c7bcf56 --- a/bootstrap.js +++ b/bootstrap.js @@ -3,12 +3,12 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ Components.utils.import("resource:///modules/imServices.jsm"); +Components.utils.import("resource:///modules/imWindows.jsm"); -var originalAddConversationFunction; - -function replaceLogLinks (aString) { - if (!aString.contains("log.bezut.info")) - return aString; +function replaceLogLinks(aOutgoingMessage) { + let text = aOutgoingMessage.message; + if (!text.contains("log.bezut.info")) + return; let rewriteDate = function (match, ...params) { // params[1] is the second parenthesized submatch string here. @@ -26,29 +26,37 @@ function replaceLogLinks (aString) { return params[0] + twoChar(date.getUTCFullYear()) + twoChar(date.getUTCMonth() + 1) + twoChar(date.getUTCDate()); }; - aString = aString.replace(/(log\.bezut\.info\/instantbird\/)(today|yesterday)/, + text = text.replace(/(log\.bezut\.info\/instantbird\/)(today|yesterday)/, rewriteDate, "gi" /* global, case-insensitive */); - return aString; + aOutgoingMessage.message = text; +} + +var replaceObserver = { + observe: function(aSubject, aTopic, aData) { + if (aTopic != "preparing-message") + return; + // aSubject is an imIOutgoingMessage. + replaceLogLinks(aSubject); + } +} + +var convObserver = { + observe: function(aSubject, aTopic, aData) { + aSubject.addObserver(replaceObserver); + } } function startup (data, reason) { - let cs = Services.conversations.wrappedJSObject; - originalAddConversationFunction = cs.addConversation; - cs.addConversation = function (aPurpleConversation) { - let wrapper = { - __proto__: aPurpleConversation, - _conv: aPurpleConversation, - sendMsg: function(aMsg) { - this._conv.sendMsg(replaceLogLinks(aMsg)); - } - }; - originalAddConversationFunction.call(cs, wrapper); - }; + Conversations._conversations.forEach(aConv => + aConv.conv.addObserver(replaceObserver)); + Services.obs.addObserver(convObserver, "new-ui-conversation", false); } + function shutdown (data, reason) { - let cs = Services.conversations.wrappedJSObject; - cs.addConversation = originalAddConversationFunction; + Conversations._conversations.forEach(aConv => + aConv.conv.removeObserver(replaceObserver)); + Services.obs.removeObserver(convObserver, "new-ui-conversation"); } function install (data, reason) {} -function uninstall (data, reason) {} \ No newline at end of file +function uninstall (data, reason) {} diff --git a/install.rdf b/install.rdf old mode 100644 new mode 100755 index 3440c5a..5a59dc1 --- a/install.rdf +++ b/install.rdf @@ -6,7 +6,7 @@ nottoday@mic.instantbird.org - 0.2 + 0.3 Not today! What do we say in logs? Not today! - replaces any reference to log.bezut.info/instantbird/(today|yesterday) with the correct date before sending the message 2 @@ -17,8 +17,8 @@ {33cb9019-c295-46dd-be21-8c4936574bee} - 1.4a1pre - 1.4a1pre + 1.6a1pre + 1.6a1pre