Add MessageSender module

This commit is contained in:
isqua 2015-01-08 19:42:32 +03:00
parent 49328a4b4a
commit 633410061a
3 changed files with 64 additions and 49 deletions

View file

@ -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);
};

View file

@ -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:');

View file

@ -32,6 +32,7 @@
"js/bquery_ajax.js",
"js/options-manager.js",
"js/message-sender.js",
"js/point-plus.js"
],