From 633410061a359b63c88d22f3dd2ea057030d96df Mon Sep 17 00:00:00 2001 From: isqua Date: Thu, 8 Jan 2015 19:42:32 +0300 Subject: [PATCH] Add MessageSender module --- chrome_point_plus/js/message-sender.js | 20 ++++++ chrome_point_plus/js/point-plus.js | 92 ++++++++++++-------------- chrome_point_plus/manifest.json | 1 + 3 files changed, 64 insertions(+), 49 deletions(-) create mode 100644 chrome_point_plus/js/message-sender.js diff --git a/chrome_point_plus/js/message-sender.js b/chrome_point_plus/js/message-sender.js new file mode 100644 index 0000000..38af910 --- /dev/null +++ b/chrome_point_plus/js/message-sender.js @@ -0,0 +1,20 @@ +function MessageSender() {} +function stub() {} + +MessageSender.prototype.css = function(files, callback) { + this.sendMessage({ + type: 'injectCSSFiles', + files: files + }, callback || stub); +}; + +MessageSender.prototype.js = function(files, callback) { + this.sendMessage({ + type: 'executeJSFiles', + files: files + }, callback || stub); +}; + +MessageSender.prototype.sendMessage = function() { + chrome.runtime.sendMessage.apply(chrome.runtime, arguments); +}; diff --git a/chrome_point_plus/js/point-plus.js b/chrome_point_plus/js/point-plus.js index bcc7b9c..8eea6cd 100644 --- a/chrome_point_plus/js/point-plus.js +++ b/chrome_point_plus/js/point-plus.js @@ -1,19 +1,23 @@ +var messenger = new MessageSender(); + // Showing page action -chrome.runtime.sendMessage({ +messenger.sendMessage({ type: 'showPageAction' -}, null, function(response) { +}, function(response) { console.debug('showPageAction response: %O', response); }); -var ppVersion; - -chrome.runtime.sendMessage(null, { - type: 'getManifestVersion' -}, null, function(response) { - ppVersion = response.version || 'undefined'; +messenger.sendMessage({ + type: 'getManifestVersion' +}, function(response) { + $(document).ready(function() { + PointPlus(response.version || 'undefined') + }); }); -$(document).ready(function() { + +function PointPlus(ppVersion) { + // Grouping console log console.group('point-plus'); console.info('Point+ %s', ppVersion); @@ -33,6 +37,7 @@ $(document).ready(function() { draft_set_save_handler(); draft_restore(); + // Loading options chrome.storage.sync.get('options', function(sync_data) { var options = new OptionsManager(sync_data.options); @@ -67,13 +72,10 @@ $(document).ready(function() { // Soundcloud if (options.is('option_embedding_soundcloud')) { // Executing Soundcloud player JS API - chrome.runtime.sendMessage({ - type: 'executeJSFiles', - files: [{ - file: 'vendor/soundcloud/soundcloud.player.api.js', - runAt: 'document_end' - }] - }, null, function(response) { + messenger.js({ + file: 'vendor/soundcloud/soundcloud.player.api.js', + runAt: 'document_end' + }, function(response) { console.debug('Soundcloud injection response: %O', response); // If scripts are executed if (response) { @@ -122,27 +124,25 @@ $(document).ready(function() { // Injecting Fancybox to the page // CSS // @todo message response callback processing - chrome.runtime.sendMessage({ - type: 'injectCSSFiles', - files: [ - 'vendor/fancybox/source/jquery.fancybox.css', - 'css/fancybox/style.css' - ] - }); + messenger.css([ + 'vendor/fancybox/source/jquery.fancybox.css', + 'css/fancybox/style.css' + ]); // JS - chrome.runtime.sendMessage(null, { - type: 'executeJSFiles', - files: [{ + messenger.js([ + { file: 'vendor/fancybox/source/jquery.fancybox.pack.js', runAt: 'document_end' - }, { + }, + { // @todo Move to the option_fancybox_videos section file: 'vendor/fancybox/source/helpers/jquery.fancybox-media.js', runAt: 'document_end' - }] - }, null, function(response) { + } + ], function(response) { // If all JS are executed + console.debug('Fancybox injection response: %O', response); if (response) { console.log('Fancybox executed. Processing...'); @@ -281,26 +281,23 @@ $(document).ready(function() { // CSS // @todo message response callback processing - chrome.runtime.sendMessage({ - type: 'injectCSSFiles', - files: [ - 'vendor/markitup/markitup/skins/markitup/style.css', - 'css/markitup/skins/markitup/style.css', - 'css/markitup/sets/markdown/style.css' - ] - }); + messenger.css([ + 'vendor/markitup/markitup/skins/markitup/style.css', + 'css/markitup/skins/markitup/style.css', + 'css/markitup/sets/markdown/style.css' + ]); // JS - chrome.runtime.sendMessage({ - type: 'executeJSFiles', - files: [{ + messenger.js([ + { file: 'vendor/markitup/markitup/jquery.markitup.js', runAt: 'document_end' - }, { + }, + { file: 'js/markitup/sets/markdown/set.js', runAt: 'document_end' - }] - }, null, function(response) { + } + ], function(response) { console.debug('MarkItUp injection response: %O', response); // If scripts are executed if (response) { @@ -420,7 +417,7 @@ $(document).ready(function() { // Desktop notifications if (options.is('option_ws_comments_notifications')) { console.log('Showing desktop notification'); - chrome.runtime.sendMessage({ + messenger.sendMessage({ type: 'showNotification', notificationId: 'comment_' + wsMessage.post_id + '#' + wsMessage.comment_id, avatarUrl: getProtocol() + '//point.im/avatar/' + wsMessage.author + '/80', @@ -493,10 +490,7 @@ $(document).ready(function() { // @ before username if (options.is('option_at_before_username')) { // @todo message response callback processing - chrome.runtime.sendMessage({ - type: 'injectCSSFiles', - files: 'css/modules/at_before_username.css' - }); + messenger.css('css/modules/at_before_username.css'); } if (options.is('option_ajax')) { @@ -620,7 +614,7 @@ $(document).ready(function() { $('#point-plus-debug').fadeOut(1000); }); -}); +} function getProtocol() { return ((location.protocol == 'http:') ? 'http:' : 'https:'); diff --git a/chrome_point_plus/manifest.json b/chrome_point_plus/manifest.json index 8030e2c..a4e88b2 100644 --- a/chrome_point_plus/manifest.json +++ b/chrome_point_plus/manifest.json @@ -32,6 +32,7 @@ "js/bquery_ajax.js", "js/options-manager.js", + "js/message-sender.js", "js/point-plus.js" ],