From 5293443c864051e72ac8d960c83da7f2b1483988 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Wed, 26 Nov 2014 22:25:56 +0400 Subject: [PATCH 1/5] Options fix. --- chrome_point_plus/_locales/en/messages.json | 3 + chrome_point_plus/_locales/ru/messages.json | 3 + chrome_point_plus/js/background.js | 21 +++- chrome_point_plus/js/options.js | 126 ++++++++++---------- chrome_point_plus/manifest.json | 2 +- 5 files changed, 93 insertions(+), 62 deletions(-) diff --git a/chrome_point_plus/_locales/en/messages.json b/chrome_point_plus/_locales/en/messages.json index 8bef84c..c3ddffa 100644 --- a/chrome_point_plus/_locales/en/messages.json +++ b/chrome_point_plus/_locales/en/messages.json @@ -13,6 +13,9 @@ "options_text_saved": { "message": "Reload page to apply changes." }, + "options_text_new_version": { + "message": "Point+ just updated.\nCheck your settings and try some new functions!" + }, "options_tabs_media": { diff --git a/chrome_point_plus/_locales/ru/messages.json b/chrome_point_plus/_locales/ru/messages.json index 9278359..117b315 100644 --- a/chrome_point_plus/_locales/ru/messages.json +++ b/chrome_point_plus/_locales/ru/messages.json @@ -13,6 +13,9 @@ "options_text_saved": { "message": "Для применения изменений перезагрузите страницу." }, + "options_text_new_version": { + "message": "Point+ только что обновился.\nПроверьте настройки и оцените новые функции!" + }, "options_tabs_media": { diff --git a/chrome_point_plus/js/background.js b/chrome_point_plus/js/background.js index 3fcce1a..6acddc7 100644 --- a/chrome_point_plus/js/background.js +++ b/chrome_point_plus/js/background.js @@ -1,3 +1,13 @@ +// Maintaining options version +chrome.storage.sync.get('options_version', function(data) { + var pp_version = getVersion(); + console.info('Point+ %s. Options are for %s.', pp_version, data.options_version); + + if (data.options_version != pp_version) { + chrome.tabs.create({url: 'options.html'}); + } +}); + // Message listener chrome.extension.onMessage.addListener(function(message, sender) { console.log('Received message: %O', message); @@ -69,4 +79,13 @@ chrome.extension.onMessage.addListener(function(message, sender) { break; } } -}); \ No newline at end of file +}); + +// Getting version from manifest.json +function getVersion() { + var xhr = new XMLHttpRequest(); + xhr.open('GET', chrome.extension.getURL('manifest.json'), false); + xhr.send(null); + var manifest = JSON.parse(xhr.responseText); + return manifest.version; +} \ No newline at end of file diff --git a/chrome_point_plus/js/options.js b/chrome_point_plus/js/options.js index 26544fe..1da0a12 100644 --- a/chrome_point_plus/js/options.js +++ b/chrome_point_plus/js/options.js @@ -1,37 +1,62 @@ var ppOptions = {}; -// Initializing full options structure -// Saves options to localStorage. -function pp_init_options() { - $('.option-node').find('input').each(function(idx, $input) { - console.log($(this)); - - // Using option types - if ($(this).hasClass('option-boolean')) { - ppOptions[$(this).prop('id').replace(/-/g, '_')] = { - 'type': 'boolean', - 'value': $(this).prop('checked') - }; - } else if ($(this).hasClass('option-enum')) { - if ($(this).prop('checked')) { - ppOptions[$(this).prop('name').replace(/-/g, '_')] = { - 'type': 'enum', - 'value': $(this).val() - } - } - } - }); +// Binding event listeners +$(function() { + pp_restore_options(); - console.log('Saving options: %O', ppOptions); + // Delegating events + $('#tabs-content').on('click', 'input', function() { + pp_save_options(); + }); +}); - // Saving parameters - chrome.storage.sync.set({'options': ppOptions}, function() { - console.log('Default options initialized'); +// Initializing full options structure +function pp_init_options() { + var pp_version = getVersion(); + + chrome.storage.sync.get('options_version', function(data) { + console.info('Point+ %s, local options are for %s', pp_version, data.options_version); + + // Checking last options version + if (data.options_version != getVersion()) { + console.log('Initializing options...'); + + $('.option-node').find('input').each(function(idx, $input) { + console.debug($(this)); + + // Using option types + if ($(this).hasClass('option-boolean')) { + ppOptions[$(this).prop('id').replace(/-/g, '_')] = { + type: 'boolean', + value: $(this).prop('checked') + }; + } else if ($(this).hasClass('option-enum')) { + if ($(this).prop('checked')) { + ppOptions[$(this).prop('name').replace(/-/g, '_')] = { + type: 'enum', + value: $(this).val() + }; + } + } + }); + + // Updating options + chrome.storage.sync.set({ + options: ppOptions, + options_version: getVersion() + }, function() { + console.log('Default options initialized. Version upgraded to %s.', pp_version); + + if (!confirm('Point+ just updated!\nCheck out new options.')) { + window.close(); + } + }); + } }); } -// Saves options to localStorage. -// @todo: optimize it! +// Saves options to sync storage. +// @todo: optimize it! (merge) function pp_save_options() { $('.option-node').find('input').each(function(idx, $input) { console.log($(this)); @@ -39,15 +64,15 @@ function pp_save_options() { // Using option types if ($(this).hasClass('option-boolean')) { ppOptions[$(this).prop('id').replace(/-/g, '_')] = { - 'type': 'boolean', - 'value': $(this).prop('checked') + type: 'boolean', + value: $(this).prop('checked') }; } else if ($(this).hasClass('option-enum')) { if ($(this).prop('checked')) { ppOptions[$(this).prop('name').replace(/-/g, '_')] = { - 'type': 'enum', - 'value': $(this).val() - } + type: 'enum', + value: $(this).val() + }; } } }); @@ -55,7 +80,7 @@ function pp_save_options() { console.log('Saving options: %O', ppOptions); // Saving parameters - chrome.storage.sync.set({'options': ppOptions}, function() { + chrome.storage.sync.set({options: ppOptions}, function() { // Update status to let user know options were saved. $('#status').html(chrome.i18n.getMessage('options_text_saved')); }); @@ -65,12 +90,12 @@ function pp_save_options() { function pp_restore_options() { // Cleaning old style options // Delete after some time - chrome.storage.sync.get('option_fancybox', function(value) { - if (value === true || value === false) { + chrome.storage.sync.get('option_fancybox', function(data) { + if ((data.option_fancybox === true) || (data.option_fancybox === false)) { console.log('Found old-style options. Cleaning...'); - chrome.storage.sync.get(null, function(old_options) { - console.log('Old data: %O', old_options); - for (option in old_options) { + chrome.storage.sync.get(null, function(data) { + console.log('Old data: %O', data); + for (option in data) { chrome.storage.sync.remove(option); } console.log('All old data removed'); @@ -80,13 +105,6 @@ function pp_restore_options() { // Loading options chrome.storage.sync.get('options', function(options) { - // Initializing clean options - // @todo: rewrite for all options - if (options.option_embedding === undefined) { - console.warn('Clean options detected. Initializing...'); - pp_init_options(); - } - // Setting options in DOM $.each(options.options, function(key, data) { switch (data.type) { @@ -111,23 +129,11 @@ function pp_restore_options() { + ' by @skobkin-ru
\n\ & @NokitaKaze' ); - }); - -} -document.addEventListener('DOMContentLoaded', pp_restore_options); -var point_plus_options_save_button = document.querySelector('#save'); -if (point_plus_options_save_button !== null) { - point_plus_options_save_button.addEventListener('click', pp_save_options); -} - -// Binding event listeners -$(function() { - // Delegating events - $('#tabs-content').on('click', 'input', function() { - pp_save_options(); + // Initializing new options + pp_init_options(); }); -}); +} // Getting version from manifest.json function getVersion() { diff --git a/chrome_point_plus/manifest.json b/chrome_point_plus/manifest.json index 789a906..3e11b7e 100644 --- a/chrome_point_plus/manifest.json +++ b/chrome_point_plus/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Point+", - "version": "1.17.1", + "version": "1.17.2", "default_locale": "ru", "author": "__MSG_ext_author__", "homepage_url": "https://bitbucket.org/skobkin/chrome_point_plus", From 699fd726c46b8e19d6beb59f6a27228ac1ee19fe Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Wed, 26 Nov 2014 23:00:32 +0400 Subject: [PATCH 2/5] Showing icon as soon as possible. --- chrome_point_plus/js/point-plus.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/chrome_point_plus/js/point-plus.js b/chrome_point_plus/js/point-plus.js index 0917d26..678f5ba 100644 --- a/chrome_point_plus/js/point-plus.js +++ b/chrome_point_plus/js/point-plus.js @@ -1,8 +1,13 @@ +// Showing page action +chrome.extension.sendMessage({ + type: 'showPageAction' +}); + $(document).ready(function() { // Grouping console log console.group('point-plus'); console.info('Point+ %s', getVersion()); - + // Проверяем, загрузились ли мы // @todo: Убрать это говно и нормально пилить расширение не принося пользователям костыли // Хочешь детект? Делай невидимый элемент где-нибудь в подвале. И айдишник, а не класс. Их искать быстрее. @@ -27,8 +32,8 @@ $(document).ready(function() { draft_restore(); // Loading options - chrome.storage.sync.get('options', function(options_data) { - var options = options_data.options; + chrome.storage.sync.get('options', function(sync_data) { + var options = sync_data.options; // Options debug console.debug('Options loaded: %O', options); @@ -526,11 +531,6 @@ $(document).ready(function() { $('.point-plus-debug').fadeOut(500); }); - - // Showing page action - chrome.extension.sendMessage({ - type: 'showPageAction' - }); }); function getProtocol() { From b607c04045dc9699b49bb6e1fb99c84569473951 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Wed, 26 Nov 2014 23:07:12 +0400 Subject: [PATCH 3/5] Fix fix. --- chrome_point_plus/js/options.js | 42 +++++++++++++++++++-------------- chrome_point_plus/manifest.json | 2 +- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/chrome_point_plus/js/options.js b/chrome_point_plus/js/options.js index 1da0a12..b2d5324 100644 --- a/chrome_point_plus/js/options.js +++ b/chrome_point_plus/js/options.js @@ -103,26 +103,32 @@ function pp_restore_options() { } }); - // Loading options - chrome.storage.sync.get('options', function(options) { - // Setting options in DOM - $.each(options.options, function(key, data) { - switch (data.type) { - case 'boolean': - if (data.value) { - $('#' + key.replace(/_/g, '-')).prop('checked', true); + // Loading options + chrome.storage.sync.get('options', function(options) { + + try { + // Setting options in DOM + $.each(options.options, function(key, data) { + switch (data.type) { + case 'boolean': + if (data.value) { + $('#' + key.replace(/_/g, '-')).prop('checked', true); + } + break; + + case 'enum': + $('.option-node .option-enum[name="' + key.replace(/_/g, '-') + '"][value="' + data.value + '"]').prop('checked', true); + break; + + default: + console.warn('Invalid option "%s" type: %O', key, data); + break; } - break; - - case 'enum': - $('.option-node .option-enum[name="' + key.replace(/_/g, '-') + '"][value="' + data.value + '"]').prop('checked', true); - break; - - default: - console.warn('Invalid option "%s" type: %O', key, data); - break; + }); + } catch (ex) { + console.error('Error while loading extension options: %O', ex); } - }); + // Showing version $('#pp-version').html('Point+ ' + getVersion() diff --git a/chrome_point_plus/manifest.json b/chrome_point_plus/manifest.json index 3e11b7e..4bb9aa8 100644 --- a/chrome_point_plus/manifest.json +++ b/chrome_point_plus/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Point+", - "version": "1.17.2", + "version": "1.17.3", "default_locale": "ru", "author": "__MSG_ext_author__", "homepage_url": "https://bitbucket.org/skobkin/chrome_point_plus", From fbec7b4a2d07c8be14ce8bfe8d484b028c81e7d8 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Wed, 26 Nov 2014 23:10:54 +0400 Subject: [PATCH 4/5] i18n fix for fix for fix. --- chrome_point_plus/js/options.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chrome_point_plus/js/options.js b/chrome_point_plus/js/options.js index b2d5324..da6464c 100644 --- a/chrome_point_plus/js/options.js +++ b/chrome_point_plus/js/options.js @@ -47,7 +47,7 @@ function pp_init_options() { }, function() { console.log('Default options initialized. Version upgraded to %s.', pp_version); - if (!confirm('Point+ just updated!\nCheck out new options.')) { + if (!confirm(chrome.i18n.getMessage('options_text_new_version'))) { window.close(); } }); From b4a039843d17009611d4ca88aaca623dc6f810c0 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Thu, 27 Nov 2014 00:24:34 +0400 Subject: [PATCH 5/5] Some refucktoring. --- chrome_point_plus/css/point-plus.css | 23 +++++++---------------- chrome_point_plus/js/point-plus.js | 22 +++++++--------------- 2 files changed, 14 insertions(+), 31 deletions(-) diff --git a/chrome_point_plus/css/point-plus.css b/chrome_point_plus/css/point-plus.css index 5957d16..e71bd12 100644 --- a/chrome_point_plus/css/point-plus.css +++ b/chrome_point_plus/css/point-plus.css @@ -213,14 +213,6 @@ div#markItUpText-input { -webkit-filter: blur(30px); } -/* point-plus-debug */ -.point-plus-debug{ - float: right; - display: block; - color: rgba(255,255,255,.75); - padding: 10px 0px; -} - .post-tag-nsfw.hide-nsfw-posts, .post-tag-сиськи.hide-nsfw-posts{ display: none; @@ -237,11 +229,10 @@ div#markItUpText-input { color: green; } - - - - - - - - +/* point-plus-debug */ +#point-plus-debug{ + float: right; + display: block; + color: rgba(255,255,255,.75); + padding: 10px 0px; +} \ No newline at end of file diff --git a/chrome_point_plus/js/point-plus.js b/chrome_point_plus/js/point-plus.js index 678f5ba..6f20cde 100644 --- a/chrome_point_plus/js/point-plus.js +++ b/chrome_point_plus/js/point-plus.js @@ -9,23 +9,15 @@ $(document).ready(function() { console.info('Point+ %s', getVersion()); // Проверяем, загрузились ли мы - // @todo: Убрать это говно и нормально пилить расширение не принося пользователям костыли - // Хочешь детект? Делай невидимый элемент где-нибудь в подвале. И айдишник, а не класс. Их искать быстрее. - // Хочешь показать пользователю обработку - делай индикатор и встраивай в интерфейс сайта. - var point_plus_debug = $('.point-plus-debug'); - if (point_plus_debug.length > 0){ - console.info('Point+ already loaded, version: %s', point_plus_debug.attr('data-point-plus-version')); + var point_plus_debug = $('#point-plus-debug'); + if (point_plus_debug.length > 0) { + console.error('Point+ %s already loaded.', point_plus_debug.data('point-plus-version')); return; } - point_plus_debug = null; - var new_div = document.createElement('div'); - $(new_div).attr({ + $('
').attr({ 'data-point-plus-version': getVersion() - }).addClass('point-plus-debug').text('Point+ v' + getVersion() + ' loading...'); - var obj = $('#user-menu-cb')[0]; - obj.parentElement.insertBefore(new_div, obj); - new_div = null; - obj = null; + }).text('Point+ ' + getVersion() + ' loading...') + .insertBefore('#user-menu-cb'); // Черновики. Ставим хандлер и восстанавливаем предыдущее состояние draft_set_save_handler(); @@ -529,7 +521,7 @@ $(document).ready(function() { set_space_key_skip_handler(); } - $('.point-plus-debug').fadeOut(500); + $('#point-plus-debug').fadeOut(1000); }); });