mirror of
https://bitbucket.org/skobkin/chrome_point_plus.git
synced 2024-11-23 18:56:04 +00:00
Options fix.
This commit is contained in:
parent
ceb01f9d8d
commit
5293443c86
|
@ -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": {
|
||||||
|
|
|
@ -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": {
|
||||||
|
|
|
@ -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);
|
||||||
|
@ -70,3 +80,12 @@ chrome.extension.onMessage.addListener(function(message, sender) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
|
@ -1,37 +1,62 @@
|
||||||
var ppOptions = {};
|
var ppOptions = {};
|
||||||
|
|
||||||
|
// Binding event listeners
|
||||||
|
$(function() {
|
||||||
|
pp_restore_options();
|
||||||
|
|
||||||
|
// Delegating events
|
||||||
|
$('#tabs-content').on('click', 'input', function() {
|
||||||
|
pp_save_options();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// Initializing full options structure
|
// Initializing full options structure
|
||||||
// Saves options to localStorage.
|
|
||||||
function pp_init_options() {
|
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) {
|
$('.option-node').find('input').each(function(idx, $input) {
|
||||||
console.log($(this));
|
console.debug($(this));
|
||||||
|
|
||||||
// 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()
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('Saving options: %O', ppOptions);
|
// Updating options
|
||||||
|
chrome.storage.sync.set({
|
||||||
|
options: ppOptions,
|
||||||
|
options_version: getVersion()
|
||||||
|
}, function() {
|
||||||
|
console.log('Default options initialized. Version upgraded to %s.', pp_version);
|
||||||
|
|
||||||
// Saving parameters
|
if (!confirm('Point+ just updated!\nCheck out new options.')) {
|
||||||
chrome.storage.sync.set({'options': ppOptions}, function() {
|
window.close();
|
||||||
console.log('Default options initialized');
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
|
pp_init_options();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
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();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// Getting version from manifest.json
|
// Getting version from manifest.json
|
||||||
function getVersion() {
|
function getVersion() {
|
||||||
|
|
|
@ -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",
|
||||||
|
|
Loading…
Reference in a new issue