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": {
"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": {

View file

@ -13,6 +13,9 @@
"options_text_saved": {
"message": "Для применения изменений перезагрузите страницу."
},
"options_text_new_version": {
"message": "Point+ только что обновился.\nПроверьте настройки и оцените новые функции!"
},
"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
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;
}
}
});
});
// 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 = {};
// 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 <a href="https://skobkin-ru.point.im/" target="_blank">@skobkin-ru</a><br>\n\
& <a href="https://nokitakaze.point.im/" target="_blank">@NokitaKaze</a>'
);
});
}
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() {

View file

@ -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",