Options fix.

This commit is contained in:
Alexey Skobkin 2014-11-26 22:25:56 +04:00
parent ceb01f9d8d
commit 5293443c86
5 changed files with 93 additions and 62 deletions

View file

@ -13,6 +13,9 @@
"options_text_saved": { "options_text_saved": {
"message": "Reload page to apply changes." "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": { "options_tabs_media": {

View file

@ -13,6 +13,9 @@
"options_text_saved": { "options_text_saved": {
"message": "Для применения изменений перезагрузите страницу." "message": "Для применения изменений перезагрузите страницу."
}, },
"options_text_new_version": {
"message": "Point+ только что обновился.\nПроверьте настройки и оцените новые функции!"
},
"options_tabs_media": { "options_tabs_media": {

View file

@ -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 // Message listener
chrome.extension.onMessage.addListener(function(message, sender) { chrome.extension.onMessage.addListener(function(message, sender) {
console.log('Received message: %O', message); console.log('Received message: %O', message);
@ -69,4 +79,13 @@ chrome.extension.onMessage.addListener(function(message, sender) {
break; break;
} }
} }
}); });
// 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;
}

View file

@ -1,37 +1,62 @@
var ppOptions = {}; var ppOptions = {};
// Initializing full options structure // Binding event listeners
// Saves options to localStorage. $(function() {
function pp_init_options() { pp_restore_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()
}
}
}
});
console.log('Saving options: %O', ppOptions); // Delegating events
$('#tabs-content').on('click', 'input', function() {
pp_save_options();
});
});
// Saving parameters // Initializing full options structure
chrome.storage.sync.set({'options': ppOptions}, function() { function pp_init_options() {
console.log('Default options initialized'); 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. // Saves options to sync storage.
// @todo: optimize it! // @todo: optimize it! (merge)
function pp_save_options() { function pp_save_options() {
$('.option-node').find('input').each(function(idx, $input) { $('.option-node').find('input').each(function(idx, $input) {
console.log($(this)); console.log($(this));
@ -39,15 +64,15 @@ function pp_save_options() {
// Using option types // Using option types
if ($(this).hasClass('option-boolean')) { if ($(this).hasClass('option-boolean')) {
ppOptions[$(this).prop('id').replace(/-/g, '_')] = { ppOptions[$(this).prop('id').replace(/-/g, '_')] = {
'type': 'boolean', type: 'boolean',
'value': $(this).prop('checked') value: $(this).prop('checked')
}; };
} else if ($(this).hasClass('option-enum')) { } else if ($(this).hasClass('option-enum')) {
if ($(this).prop('checked')) { if ($(this).prop('checked')) {
ppOptions[$(this).prop('name').replace(/-/g, '_')] = { ppOptions[$(this).prop('name').replace(/-/g, '_')] = {
'type': 'enum', type: 'enum',
'value': $(this).val() value: $(this).val()
} };
} }
} }
}); });
@ -55,7 +80,7 @@ function pp_save_options() {
console.log('Saving options: %O', ppOptions); console.log('Saving options: %O', ppOptions);
// Saving parameters // 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. // Update status to let user know options were saved.
$('#status').html(chrome.i18n.getMessage('options_text_saved')); $('#status').html(chrome.i18n.getMessage('options_text_saved'));
}); });
@ -65,12 +90,12 @@ function pp_save_options() {
function pp_restore_options() { function pp_restore_options() {
// Cleaning old style options // Cleaning old style options
// Delete after some time // Delete after some time
chrome.storage.sync.get('option_fancybox', function(value) { chrome.storage.sync.get('option_fancybox', function(data) {
if (value === true || value === false) { if ((data.option_fancybox === true) || (data.option_fancybox === false)) {
console.log('Found old-style options. Cleaning...'); console.log('Found old-style options. Cleaning...');
chrome.storage.sync.get(null, function(old_options) { chrome.storage.sync.get(null, function(data) {
console.log('Old data: %O', old_options); console.log('Old data: %O', data);
for (option in old_options) { for (option in data) {
chrome.storage.sync.remove(option); chrome.storage.sync.remove(option);
} }
console.log('All old data removed'); console.log('All old data removed');
@ -80,13 +105,6 @@ function pp_restore_options() {
// Loading options // Loading options
chrome.storage.sync.get('options', function(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 // Setting options in DOM
$.each(options.options, function(key, data) { $.each(options.options, function(key, data) {
switch (data.type) { switch (data.type) {
@ -111,23 +129,11 @@ function pp_restore_options() {
+ ' by <a href="https://skobkin-ru.point.im/" target="_blank">@skobkin-ru</a><br>\n\ + ' by <a href="https://skobkin-ru.point.im/" target="_blank">@skobkin-ru</a><br>\n\
& <a href="https://nokitakaze.point.im/" target="_blank">@NokitaKaze</a>' & <a href="https://nokitakaze.point.im/" target="_blank">@NokitaKaze</a>'
); );
});
} // Initializing new options
document.addEventListener('DOMContentLoaded', pp_restore_options); pp_init_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();
}); });
}); }
// Getting version from manifest.json // Getting version from manifest.json
function getVersion() { function getVersion() {

View file

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "Point+", "name": "Point+",
"version": "1.17.1", "version": "1.17.2",
"default_locale": "ru", "default_locale": "ru",
"author": "__MSG_ext_author__", "author": "__MSG_ext_author__",
"homepage_url": "https://bitbucket.org/skobkin/chrome_point_plus", "homepage_url": "https://bitbucket.org/skobkin/chrome_point_plus",