Skip to content

Commit

Permalink
Merge pull request #602 from hymccord/bug/reset-settings
Browse files Browse the repository at this point in the history
Fix settings loss on update
  • Loading branch information
AardWolf authored Dec 21, 2024
2 parents c45a772 + 2788a46 commit b4600cd
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 23 deletions.
13 changes: 11 additions & 2 deletions src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,17 @@ <h2 class="h3 my-4 page-title">MHCT Helper Settings</h2>
</div>
</div>

<button id="save" type="button" class="btn btn-primary w-100 mb-4">Save</button>

<div class="fixed-bottom p-3">
<div class="container">
<div class="row justify-content-center">
<div class="col-12 col-lg-10 col-xl-8 mx-auto">
<button id="save" type="button" class="btn btn-primary w-100">Save</button>
</div>
</div>
</div>
</div>
<!-- Add bottom padding to prevent content from being hidden under fixed button -->
<div class="pb-5 mb-4"></div></div>
</div>
</div>
</div>
Expand Down
88 changes: 67 additions & 21 deletions src/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ chrome.runtime.onInstalled.addListener(() => chrome.tabs.query(

setInterval(updateIcon, 1000);

// Set the default user settings, also holds current
let userSettings = {
const userSettingsDefault = {
version: 1,
'tracking-hunts': true,
'tracking-crowns': true,
Expand All @@ -40,16 +39,7 @@ let userSettings = {
'enhancement-dark-mode': false,
'general-log-level': 'info',
};

function changeUserSettings(settings) {
if (settings.value) {
userSettings = settings.value;
}

chrome.storage.sync.set(userSettings);

return userSettings;
}
let userSettings = Object.assign({}, userSettingsDefault);

function updateIcon() {
chrome.tabs.query({'url': ['*://www.mousehuntgame.com/*', '*://apps.facebook.com/mousehunt/*']},
Expand Down Expand Up @@ -256,13 +246,51 @@ async function submitCrowns(crowns) {
return crowns.bronze + crowns.silver + crowns.gold + crowns.platinum + crowns.diamond;
}

/** Settings */
// TODO: Clean up into a separate file
// Promisify storage API. TODO: Not needed in mv3.
const storageAPI = {
get(key) {
return new Promise(resolve => chrome.storage.sync.get(key, resolve));
},
set(data) {
return new Promise(resolve => chrome.storage.sync.set(data, resolve));
},
clear() {
return new Promise(resolve => chrome.storage.sync.clear(resolve));
},
};

async function migrateUserSettings() {
const defaults = userSettings;
function changeUserSettings(settings) {
let shouldSave = false;

const results = await new Promise(resolve => chrome.storage.sync.get(null, resolve));
// Update user settings with the new values.
if (settings.value) {
// Only update settings that are already in the user settings.
for (const key in settings.value) {
if (Object.prototype.hasOwnProperty.call(userSettings, key) && userSettings[key] !== settings.value[key]) {
userSettings[key] = settings.value[key];
shouldSave = true;
}
}
}

if (shouldSave) {
saveUserSettings();
}

return userSettings;
}

/**
* Migrate user settings from the old format to the new format.
*
* Only modifies the sync storage, and only if the old settings are found.
*/
async function migrateUserSettings() {
const defaults = userSettingsDefault;

const results = await storageAPI.get(null);
if (!(results instanceof Object)) {
return;
}
Expand Down Expand Up @@ -303,23 +331,41 @@ async function migrateUserSettings() {
defaults['enhancement-escape-dismiss'] = results.escape_button_close;
defaults['enhancement-dark-mode'] = results.dark_mode;

chrome.storage.sync.clear();
chrome.storage.sync.set(defaults);
await storageAPI.clear();
await storageAPI.set(defaults);
}

userSettings = defaults;
const currentVersion = userSettingsDefault.version;
if (version < currentVersion) {
await storageAPI.set({version: currentVersion});
}
}

async function loadUserSettings() {
await migrateUserSettings();

const defaults = userSettings;
const results = await new Promise(resolve => chrome.storage.sync.get(Object.assign(defaults), resolve));
const newSettings = results instanceof Object && results || Object.assign(defaults);
const results = await storageAPI.get(Object.assign(userSettingsDefault));
const newSettings = results instanceof Object && results || Object.assign(userSettingsDefault);

userSettings = newSettings;
}

async function saveUserSettings() {
const getModifiedSettings = (edit, orig = {}) => {
const out = {};
for ( const prop in edit ) {
if ( Object.prototype.hasOwnProperty.call(orig, prop) && edit[prop] !== orig[prop] ) {
out[prop] = edit[prop];
}
}
return out;
};

// Make sure to save keys that exist in the default settings.
const toSave = getModifiedSettings(userSettings, userSettingsDefault);
await storageAPI.set(toSave);
}

(async () => {

try {
Expand Down

0 comments on commit b4600cd

Please sign in to comment.