settings: prevent modifying defaultSettings, don't save 'default' (#34)
also use deepCopy when creating copies of objects, and make sure recent data is only being filled after stores are defined
This commit is contained in:
@@ -106,7 +106,7 @@ async function dispatchFill(
|
|||||||
allowForeign = false,
|
allowForeign = false,
|
||||||
allowNoSecret = false
|
allowNoSecret = false
|
||||||
) {
|
) {
|
||||||
fillRequest = Object.assign({}, fillRequest, {
|
fillRequest = Object.assign(deepCopy(fillRequest), {
|
||||||
allowForeign: allowForeign,
|
allowForeign: allowForeign,
|
||||||
allowNoSecret: allowNoSecret
|
allowNoSecret: allowNoSecret
|
||||||
});
|
});
|
||||||
@@ -195,8 +195,8 @@ async function fillFields(tab, login, fields) {
|
|||||||
*
|
*
|
||||||
* @return object Local settings from the extension
|
* @return object Local settings from the extension
|
||||||
*/
|
*/
|
||||||
async function getLocalSettings() {
|
function getLocalSettings() {
|
||||||
var settings = Object.assign({}, defaultSettings);
|
var settings = deepCopy(defaultSettings);
|
||||||
for (var key in settings) {
|
for (var key in settings) {
|
||||||
var value = localStorage.getItem(key);
|
var value = localStorage.getItem(key);
|
||||||
if (value !== null) {
|
if (value !== null) {
|
||||||
@@ -204,34 +204,21 @@ async function getLocalSettings() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var storeId in settings.stores) {
|
|
||||||
var when = localStorage.getItem("recent:" + storeId);
|
|
||||||
if (when) {
|
|
||||||
settings.stores[storeId].when = JSON.parse(when);
|
|
||||||
} else {
|
|
||||||
settings.stores[storeId].when = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fill recent data
|
|
||||||
settings.recent = localStorage.getItem("recent");
|
|
||||||
if (settings.recent) {
|
|
||||||
settings.recent = JSON.parse(settings.recent);
|
|
||||||
} else {
|
|
||||||
settings.recent = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fill current tab info
|
|
||||||
try {
|
|
||||||
settings.tab = (await chrome.tabs.query({ active: true, currentWindow: true }))[0];
|
|
||||||
settings.host = new URL(settings.tab.url).hostname;
|
|
||||||
} catch (e) {
|
|
||||||
throw new Error("Unable to retrieve current tab information");
|
|
||||||
}
|
|
||||||
|
|
||||||
return settings;
|
return settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deep copy an object
|
||||||
|
*
|
||||||
|
* @since 3.0.0
|
||||||
|
*
|
||||||
|
* @param object obj an object to copy
|
||||||
|
* @return object a new deep copy
|
||||||
|
*/
|
||||||
|
function deepCopy(obj) {
|
||||||
|
return JSON.parse(JSON.stringify(obj));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle modal authentication requests (e.g. HTTP basic)
|
* Handle modal authentication requests (e.g. HTTP basic)
|
||||||
*
|
*
|
||||||
@@ -531,15 +518,17 @@ async function receiveMessage(message, sender, sendResponse) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var settings = await getLocalSettings();
|
var settings = getLocalSettings();
|
||||||
try {
|
try {
|
||||||
var configureSettings = Object.assign(settings, { defaultStore: {} });
|
var configureSettings = Object.assign(deepCopy(settings), {
|
||||||
|
defaultStore: {}
|
||||||
|
});
|
||||||
var response = await chrome.runtime.sendNativeMessage(appID, {
|
var response = await chrome.runtime.sendNativeMessage(appID, {
|
||||||
settings: configureSettings,
|
settings: configureSettings,
|
||||||
action: "configure"
|
action: "configure"
|
||||||
});
|
});
|
||||||
settings.version = response.version;
|
settings.version = response.version;
|
||||||
if (settings.stores.length) {
|
if (Object.keys(settings.stores).length > 0) {
|
||||||
// there are user-configured stores present
|
// there are user-configured stores present
|
||||||
for (var storeId in settings.stores) {
|
for (var storeId in settings.stores) {
|
||||||
if (response.data.storeSettings.hasOwnProperty(storeId)) {
|
if (response.data.storeSettings.hasOwnProperty(storeId)) {
|
||||||
@@ -564,6 +553,31 @@ async function receiveMessage(message, sender, sendResponse) {
|
|||||||
settings: response.data.defaultStore.settings
|
settings: response.data.defaultStore.settings
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fill recent data
|
||||||
|
for (var storeId in settings.stores) {
|
||||||
|
var when = localStorage.getItem("recent:" + storeId);
|
||||||
|
if (when) {
|
||||||
|
settings.stores[storeId].when = JSON.parse(when);
|
||||||
|
} else {
|
||||||
|
settings.stores[storeId].when = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
settings.recent = localStorage.getItem("recent");
|
||||||
|
if (settings.recent) {
|
||||||
|
settings.recent = JSON.parse(settings.recent);
|
||||||
|
} else {
|
||||||
|
settings.recent = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fill current tab info
|
||||||
|
try {
|
||||||
|
settings.tab = (await chrome.tabs.query({ active: true, currentWindow: true }))[0];
|
||||||
|
settings.host = new URL(settings.tab.url).hostname;
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error("Unable to retrieve current tab information");
|
||||||
|
}
|
||||||
|
|
||||||
handleMessage(settings, message, sendResponse);
|
handleMessage(settings, message, sendResponse);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// handle error
|
// handle error
|
||||||
@@ -581,6 +595,9 @@ async function receiveMessage(message, sender, sendResponse) {
|
|||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function saveSettings(settings) {
|
function saveSettings(settings) {
|
||||||
|
// 'default' is our reserved name for the default store
|
||||||
|
delete settings.stores.default;
|
||||||
|
|
||||||
for (var key in defaultSettings) {
|
for (var key in defaultSettings) {
|
||||||
if (settings.hasOwnProperty(key)) {
|
if (settings.hasOwnProperty(key)) {
|
||||||
localStorage.setItem(key, JSON.stringify(settings[key]));
|
localStorage.setItem(key, JSON.stringify(settings[key]));
|
||||||
|
Reference in New Issue
Block a user