Verified Commit 3ab8560e authored by Thomas Rientjes's avatar Thomas Rientjes
Browse files

Implement preference prioritization logic

parent 33ac7dc3
......@@ -507,7 +507,7 @@ interceptor.relatedSettings.push(Setting.AMOUNT_INJECTED);
interceptor.relatedSettings.push(Setting.XHR_TEST_DOMAIN);
interceptor.relatedSettings.push(Setting.BLOCK_MISSING);
chrome.storage.local.get(interceptor.relatedSettings, function (items) {
helpers.getStoredSettings(interceptor.relatedSettings, function (items) {
interceptor.amountInjected = items.amountInjected || 0;
interceptor.xhrTestDomain = items.xhrTestDomain || Address.DECENTRALEYES;
......
......@@ -25,21 +25,17 @@ var main = {};
main._initializeSettings = function () {
let settingDefaults = {
[Setting.XHR_TEST_DOMAIN]: Address.DECENTRALEYES,
[Setting.SHOW_ICON_BADGE]: true,
[Setting.BLOCK_MISSING]: false,
[Setting.DISABLE_PREFETCH]: true,
[Setting.ENFORCE_STAGING]: false,
[Setting.STRIP_METADATA]: true,
[Setting.WHITELISTED_DOMAINS]: {}
};
chrome.storage.local.get(settingDefaults, function (items) {
if (items === null) {
items = settingDefaults; // Restore setting defaults.
}
let settings = [
Setting.XHR_TEST_DOMAIN,
Setting.SHOW_ICON_BADGE,
Setting.BLOCK_MISSING,
Setting.DISABLE_PREFETCH,
Setting.ENFORCE_STAGING,
Setting.STRIP_METADATA,
Setting.WHITELISTED_DOMAINS
];
helpers.getStoredSettings(settings, function (items) {
if (items.blockMissing === true || items.enforceStaging === true) {
stateManager.updateEnvironment(Environment.STAGING);
......@@ -53,14 +49,12 @@ main._initializeSettings = function () {
'value': false
});
}
chrome.storage.local.set(items);
});
};
main._showReleaseNotes = function (details) {
let location, previousVersion;
let location, previousVersion, setting;
location = chrome.extension.getURL('pages/welcome/welcome.html');
......@@ -68,26 +62,26 @@ main._showReleaseNotes = function (details) {
details.reason === chrome.runtime.OnInstalledReason.UPDATE) {
previousVersion = details.previousVersion;
setting = Setting.SHOW_RELEASE_NOTES;
if (previousVersion && previousVersion.charAt(0) === '2') {
return; // Do not show release notes after minor updates.
}
if (details.temporary !== true) {
if (details.temporary === true) {
return; // Only show release notes on full installations.
}
chrome.storage.local.get({
[Setting.SHOW_RELEASE_NOTES]: true
}, function (items) {
helpers.getStoredSettings(setting, function (items) {
if (items.showReleaseNotes === true) {
if (items.showReleaseNotes === true) {
chrome.tabs.create({
'url': location,
'active': false
});
}
});
}
chrome.tabs.create({
'url': location,
'active': false
});
}
});
}
};
......
......@@ -132,7 +132,7 @@ requestAnalyzer._findLocalTarget = function (resourceMappings, basePath, channel
requestAnalyzer._applyWhitelistedDomains = function () {
chrome.storage.local.get(Setting.WHITELISTED_DOMAINS, function (items) {
helpers.getStoredSettings(Setting.WHITELISTED_DOMAINS, function (items) {
requestAnalyzer.whitelistedDomains = items.whitelistedDomains || {};
});
};
......
......@@ -65,7 +65,7 @@ requestSanitizer._stripMetadata = function (requestDetails) {
* Initializations
*/
chrome.storage.local.get({[Setting.STRIP_METADATA]: true}, function (items) {
helpers.getStoredSettings(Setting.STRIP_METADATA, function (items) {
if (items === null || items.stripMetadata !== false) {
requestSanitizer.enable();
......
......@@ -51,7 +51,7 @@ stateManager.registerInjection = function (tabIdentifier, injection) {
if (isNaN(interceptor.amountInjected)) {
chrome.storage.local.get(Setting.AMOUNT_INJECTED, function (items) {
helpers.getStoredSettings(Setting.AMOUNT_INJECTED, function (items) {
interceptor.amountInjected = items.amountInjected;
......@@ -90,7 +90,7 @@ stateManager.updateEnvironment = function (preferredEnvironment) {
let requiredItems = [Setting.BLOCK_MISSING, Setting.ENFORCE_STAGING];
chrome.storage.local.get(requiredItems, function (items) {
helpers.getStoredSettings(requiredItems, function (items) {
if (items.blockMissing === true || items.enforceStaging === true) {
stateManager.setEnvironment(Environment.STAGING);
......@@ -324,7 +324,7 @@ chrome.tabs.query({}, function (tabs) {
tabs.forEach(stateManager._createTab);
});
chrome.storage.local.get(Setting.SHOW_ICON_BADGE, function (items) {
helpers.getStoredSettings(Setting.SHOW_ICON_BADGE, function (items) {
if (items.showIconBadge === undefined) {
items.showIconBadge = true;
......
......@@ -23,6 +23,52 @@ var helpers = {};
* Public Methods
*/
helpers.getStoredSettings = function (keys, callback) {
helpers.getSettingDefaults(keys, function (defaults) {
chrome.storage.local.get(defaults, function (localItems) {
if (localItems === null) {
return callback(defaults);
}
callback(localItems);
});
});
};
helpers.getSettingDefaults = function (keys, callback) {
let defaults, defaultKeys, matches;
defaults = {
[Setting.AMOUNT_INJECTED]: 0,
[Setting.BLOCK_MISSING]: false,
[Setting.DISABLE_PREFETCH]: true,
[Setting.ENFORCE_STAGING]: false,
[Setting.SHOW_ICON_BADGE]: true,
[Setting.SHOW_RELEASE_NOTES]: true,
[Setting.STRIP_METADATA]: true,
[Setting.WHITELISTED_DOMAINS]: {},
[Setting.XHR_TEST_DOMAIN]: Address.DECENTRALEYES,
};
defaultKeys = Object.keys(defaults);
matches = defaultKeys.reduce(function (accumulator, currentValue) {
if (keys.includes(currentValue)) {
accumulator[currentValue] = defaults[currentValue];
}
return accumulator;
}, {});
callback(matches);
};
helpers.insertI18nContentIntoDocument = function (document) {
let scriptDirection, i18nElements;
......
......@@ -121,7 +121,7 @@ options._determineOptionValues = function () {
let optionKeys = Object.keys(options._optionElements);
chrome.storage.local.get(optionKeys, function (items) {
helpers.getStoredSettings(optionKeys, function (items) {
options._optionValues = items;
resolve();
......
......@@ -228,7 +228,7 @@ popup._determineAmountInjected = function () {
return new Promise((resolve) => {
chrome.storage.local.get(Setting.AMOUNT_INJECTED, function (items) {
helpers.getStoredSettings(Setting.AMOUNT_INJECTED, function (items) {
popup._amountInjected = items.amountInjected || 0;
resolve();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment