Non-working dynamic loading system and new message API.

This commit is contained in:
Alexey Skobkin 2015-01-07 06:36:06 +04:00
parent cb2ca9766b
commit 180d58b464
2 changed files with 226 additions and 125 deletions

View File

@ -8,15 +8,47 @@ chrome.storage.sync.get('options_version', function(data) {
}
});
// Crutches and bikes
/**
* Inject several JS files
* @param {number} tabId Unique ID of tab which requested injection
* @param {Object[]} files Array of objects of files to inject
* @callback onAllInjected Callback function running when injection ends
*/
function injectJS(tabId, files, onAllInjected) {
var item = files.shift();
if (item) {
console.log('Injecting JS "%s" to the tab #%s', item.file, tabId);
if ('file' in item) {
chrome.tabs.executeScript(tabId ? tabId : null, {
file: item.file,
runAt: item.runAt || 'document_start'
}, function(result) {
console.info('"%s" injected to the tab #%s', item.file, tabId);
injectJS(tabId, files, onAllInjected);
});
}
} else {
onAllInjected();
}
}
// @todo Implement injectCSS (because JS execution working always after CSS injection)
// Message listener
chrome.extension.onMessage.addListener(function(message, sender) {
console.log('Received message: %O', message);
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
// @todo Check if sender.tab may be undefined in some cases
console.log('Received message from tab #%i: %O', sender.tab.id, message);
if (message) {
switch (message.type) {
case 'showPageAction':
var tab = sender.tab;
chrome.pageAction.show(tab.id);
chrome.pageAction.show(sender.tab.id);
sendResponse(true);
console.log('Showed pageAction for tab #%s', sender.tab.id);
break;
case 'showNotification':
@ -28,11 +60,13 @@ chrome.extension.onMessage.addListener(function(message, sender) {
message: message.text,
priority: 0,
isClickable: true
},
function() { /* Error checking goes here */}
}, function(notificationId) {
console.info('Notification "%s" created', notificationId);
sendResponse(true);
}
);
console.log('Showing notification %s', message.notificationId);
break;
case 'listenNotificationClicks':
@ -52,27 +86,69 @@ chrome.extension.onMessage.addListener(function(message, sender) {
});
}
});
break;
sendResponse(true);
break;
/**
* @deprecated since 1.19.1
*/
case 'injectJSFile':
console.log('Injecting JS: %s', message.file);
chrome.tabs.executeScript(null, {
file: message.file
//,runAt: 'document_end'
console.log('Executing JS: %s', message.file);
chrome.tabs.executeScript(sender.tab.id ? sender.tab.id : null, {
file: message.file,
runAt: message.runAt || 'document_start'
}, function() {
sendResponse(true);
console.info('JS file executed: "%s"', message.file);
});
break;
// Inject several files
case 'executeJSFiles':
//console.debug('Received JS file list: %O', message.files);
if (message.files.length) {
injectJS(sender.tab.id ? sender.tab.id : null, message.files, function() {
// @fixme does not sending response now!
sendResponse(true);
console.info('All scripts executed');
});
} else {
/*
* May be not?
* But I don't want to block some shit-code execution
*/
sendResponse(false);
console.warn('No scripts executed (empty script array)');
}
break;
/**
* @deprecated since 1.19.1
*/
case 'injectCSSFile':
console.log('Injecting CSS: %s', message.file);
chrome.tabs.insertCSS(null, {
console.log('Injecting CSS: "%s"', message.file);
chrome.tabs.insertCSS(sender.tab.id ? sender.tab.id : null, {
file: message.file
}, function() {
sendResponse(true);
console.info('CSS file "%s" injected', message.file);
});
break;
case 'injectCSSCode':
if (message.code !== undefined) {
chrome.tabs.insertCSS(null, {
chrome.tabs.insertCSS(sender.tab.id ? sender.tab.id : null, {
code: message.code
}, function() {
sendResponse(true);
console.info('CSS code injected: \n%s', message.file);
});
}

View File

@ -1,6 +1,8 @@
// Showing page action
chrome.extension.sendMessage({
chrome.runtime.sendMessage({
type: 'showPageAction'
}, null, function(response) {
console.debug('showPageAction response: %O', response);
});
/**
@ -68,7 +70,7 @@ $(document).ready(function() {
// Options debug
try {
console.debug('Options loaded: %O', options.getOptions());
}catch(e){}
} catch(e){}
create_tag_system();
// Embedding
@ -94,38 +96,44 @@ $(document).ready(function() {
// Soundcloud
if (options.is('option_embedding_soundcloud')) {
// Injecting JS API
chrome.extension.sendMessage({
type: 'injectJSFile',
file: 'vendor/soundcloud/soundcloud.player.api.js'
});
// Executing Soundcloud player JS API
chrome.runtime.sendMessage({
type: 'executeJSFiles',
files: [{
file: 'vendor/soundcloud/soundcloud.player.api.js',
runAt: 'document_end'
}]
}, null, function(response) {
console.debug('Soundcloud injection response: %O', response);
// If scripts are executed
if (response) {
// Processing links
$('.post .post-content a[href*="\\:\\/\\/soundcloud\\.com\\/"]').each(function(index) {
console.log($(this));
// Processing links
$('.post .post-content a[href*="\\:\\/\\/soundcloud\\.com\\/"]').each(function(index) {
console.log($(this));
// @todo: вынести в отдельный шаблон
$player = $('<div class="pp-soundcloud">\
<object height="81" width="100%" id="pp-soundcloud-' + index + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">\
<param name="movie" value="//player.soundcloud.com/player.swf?url=' + encodeURIComponent($(this).prop('href'))
+ '&enable_api=true&object_id=pp-soundcloud-' + index + '">\
<param name="allowscriptaccess" value="always">\
<embed allowscriptaccess="always" height="81" src="//player.soundcloud.com/player.swf?url='
+ encodeURIComponent($(this).prop('href')) + '&enable_api=true&object_id=pp-soundcloud-' + index
+ '" type="application/x-shockwave-flash" width="100%" name="pp-soundcloud-' + index + '"></embed>\
</object>\
</div>');
// @todo: вынести в отдельный шаблон
$player = $('<div class="pp-soundcloud">\
<object height="81" width="100%" id="pp-soundcloud-' + index + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">\
<param name="movie" value="//player.soundcloud.com/player.swf?url=' + encodeURIComponent($(this).prop('href'))
+ '&enable_api=true&object_id=pp-soundcloud-' + index + '">\
<param name="allowscriptaccess" value="always">\
<embed allowscriptaccess="always" height="81" src="//player.soundcloud.com/player.swf?url='
+ encodeURIComponent($(this).prop('href')) + '&enable_api=true&object_id=pp-soundcloud-' + index
+ '" type="application/x-shockwave-flash" width="100%" name="pp-soundcloud-' + index + '"></embed>\
</object>\
</div>');
// Replace or prepend
if (options.is('option_embedding_soundcloud_orig_link')) {
// Before
$(this).before($player);
} else {
// Replace
$(this).replaceWith($player);
// Replace or prepend
if (options.is('option_embedding_soundcloud_orig_link')) {
// Before
$(this).before($player);
} else {
// Replace
$(this).replaceWith($player);
}
});
}
});
}
// Parse pleer.com links and create audio instead
@ -142,66 +150,77 @@ $(document).ready(function() {
// Fancybox
if (options.is('option_fancybox')) {
// Injecting Fancybox to the page
chrome.extension.sendMessage({
type: 'injectJSFile',
file: 'vendor/fancybox/source/jquery.fancybox.pack.js'
});
chrome.extension.sendMessage({
type: 'injectJSFile',
file: 'vendor/fancybox/source/helpers/jquery.fancybox-media.js'
});
chrome.extension.sendMessage({
// CSS
chrome.runtime.sendMessage({
type: 'injectCSSFile',
file: 'vendor/fancybox/source/jquery.fancybox.css'
});
if (options.is('option_fancybox_bind_images_to_one_flow')) {
// Linking images in posts to the galleries
$('.post-content .text').each(function() {
$(this).find('a.postimg:not(.youtube)').attr('data-fancybox-group', 'one_flow_gallery');
});
} else {
$('.post-content .text').each(function(idxPost) {
$(this).find('a.postimg:not(.youtube)').attr('data-fancybox-group', 'post' + idxPost);
});
}
// JS
chrome.runtime.sendMessage(null, {
type: 'executeJSFiles',
files: [{
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) {
// If all JS are executed
console.debug('Fancybox injection response: %O', response);
if (response) {
console.log('Fancybox executed. Processing...')
if (options.is('option_fancybox_bind_images_to_one_flow')) {
// Linking images in posts to the galleries
$('.post-content .text').each(function() {
$(this).find('a.postimg:not(.youtube)').attr('data-fancybox-group', 'one_flow_gallery');
});
} else {
$('.post-content .text').each(function(idxPost) {
$(this).find('a.postimg:not(.youtube)').attr('data-fancybox-group', 'post' + idxPost);
});
}
// Images
if (options.is('option_fancybox_images')) {
// Init fancybox
$('.postimg:not(.youtube)').fancybox({
type: 'image'
});
}
// Images
if (options.is('option_fancybox_images')) {
// Init fancybox
$('.postimg:not(.youtube)').fancybox({
type: 'image'
});
}
// Правим хинты у фансибокса
if (options.is('option_fancybox_smart_hints')) {
fancybox_set_smart_hints();
} else {
$('.post .postimg').attr('data-fancybox-title', ' ');
}
// Правим хинты у фансибокса
if (options.is('option_fancybox_smart_hints')) {
fancybox_set_smart_hints();
} else {
$('.post .postimg').attr('data-fancybox-title', ' ');
}
// Videos
if (options.is('option_fancybox_videos')) {
$('.postimg.youtube').addClass('fancybox-media').fancybox({
helpers: {
media: {
youtube: {
params: {
autoplay: 1
// Videos
if (options.is('option_fancybox_videos')) {
$('.postimg.youtube').addClass('fancybox-media').fancybox({
helpers: {
media: {
youtube: {
params: {
autoplay: 1
}
}
}
}
}
});
}
});
}
// Posts
if (options.is('option_fancybox_posts')) {
// Excluding some sort of piece-of-shit makeup
$('.post-id a').not('#comments .post-id a, #top-post .post-id a').attr('data-fancybox-type', 'iframe').fancybox({
maxWidth: 780
});
}
// Posts
if (options.is('option_fancybox_posts')) {
// Excluding some sort of piece-of-shit makeup
$('.post-id a').not('#comments .post-id a, #top-post .post-id a').attr('data-fancybox-type', 'iframe').fancybox({
maxWidth: 780
});
}
}
});
}
// NSFW Filtering
@ -282,41 +301,47 @@ $(document).ready(function() {
}
// Visual editor
if (options.is('option_visual_editor_post')) {
// Injecting editor JS
chrome.extension.sendMessage({
type: 'injectJSFile',
file: 'vendor/markitup/markitup/jquery.markitup.js'
});
// Getting mySettings from set.js
chrome.extension.sendMessage({
type: 'injectJSFile',
file: 'js/markitup/sets/markdown/set.js'
});
// Add classes
$('#new-post-form #text-input, .post-content #text-input').addClass('markitup').css('height', '20em');
// CSS
chrome.extension.sendMessage({
chrome.runtime.sendMessage({
type: 'injectCSSFile',
file: 'vendor/markitup/markitup/skins/markitup/style.css'
});
chrome.extension.sendMessage({
chrome.runtime.sendMessage({
type: 'injectCSSFile',
file: 'css/markitup/sets/markdown/style.css'
});
// Add classes
$('#new-post-form #text-input, .post-content #text-input').addClass('markitup').css('height', '20em');
// Init
$('.markitup').markItUp(mySettings);
// JS
chrome.runtime.sendMessage({
type: 'executeJSFiles',
files: [{
file: 'vendor/markitup/markitup/jquery.markitup.js',
runAt: 'document_end'
}, {
file: 'js/markitup/sets/markdown/set.js',
runAt: 'document_end'
}]
}, null, function(response) {
console.debug('MarkItUp injection response: %O', response);
// If scripts are executed
if (response) {
// Init MarkItUp
$('.markitup').markItUp(mySettings);
// Send by CTRL+Enter
if (options.is('option_ctrl_enter')) {
// New post
$('#new-post-form #text-input, .post-content #text-input').on('keydown.point_plus', function(e) {
if (e.ctrlKey && (e.keyCode == 10 || e.keyCode == 13)) {
e.preventDefault();
$(this).parents('#new-post-form,#post-edit-form').submit();
// Send by CTRL+Enter
if (options.is('option_ctrl_enter')) {
// New post
$('#new-post-form #text-input, .post-content #text-input').on('keydown.point_plus', function(e) {
if (e.ctrlKey && (e.keyCode == 10 || e.keyCode == 13)) {
e.preventDefault();
$(this).parents('#new-post-form,#post-edit-form').submit();
}
});
}
});
}
}
});
}
// Google search
if (options.is('option_search_with_google')) {
@ -335,7 +360,7 @@ $(document).ready(function() {
// @todo: унести в опцию
// Adding event listener for notification click
chrome.extension.sendMessage({
chrome.runtime.sendMessage({
type: 'listenNotificationClicks',
protocol: getProtocol()
});
@ -491,7 +516,7 @@ $(document).ready(function() {
// Desktop notifications
if (options.is('option_ws_comments_notifications')) {
console.log('Showing desktop notification');
chrome.extension.sendMessage({
chrome.runtime.sendMessage({
type: 'showNotification',
notificationId: 'comment_' + wsMessage.post_id + '#' + wsMessage.comment_id,
avatarUrl: getProtocol() + userAvatar + '/80',
@ -550,7 +575,7 @@ $(document).ready(function() {
}
// @ before username
if (options.is('option_at_before_username')) {
chrome.extension.sendMessage({
chrome.runtime.sendMessage({
type: 'injectCSSFile',
file: 'css/modules/at_before_username.css'
});