diff --git a/src/scripts/background.js b/src/scripts/background.js index f7c022672..a675b0605 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -2136,21 +2136,22 @@ window.onbeforeunload = function () { if (!FF) { TogglButton.checkPermissions(); +} - // Check whether new version is installed - browser.runtime.onInstalled.addListener(function (details) { - if (details.reason === 'install') { - TogglButton.checkPermissions(0); - } else if (details.reason === 'update') { - if ( - details.previousVersion[0] === '0' && - process.env.VERSION[0] === '1' - ) { - TogglButton.checkPermissions(1); - } +// Check whether new version is installed +browser.runtime.onInstalled.addListener(function (details) { + if (details.reason === 'install') { + if (!FF) TogglButton.checkPermissions(0); + } else if (details.reason === 'update') { + console.info(`Updated from ${details.previousVersion} to ${process.env.VERSION}.`); + const [ prevMajor, prevMinor ] = details.previousVersion.split('.').map(Number); + const [ nextMajor, nextMinor ] = process.env.VERSION.split('.').map(Number); + if (prevMajor === 1 && prevMinor <= 22 && nextMajor === 1 && nextMinor >= 23) { + // Attempt to migrate legacy localstorage settings to storage.sync settings + db._migrateToStorageSync(); } - }); -} + } +}); if (browser.commands) { browser.commands.onCommand.addListener(function (command) { diff --git a/src/scripts/lib/db.js b/src/scripts/lib/db.js index 7613e6e51..734ffaeb1 100644 --- a/src/scripts/lib/db.js +++ b/src/scripts/lib/db.js @@ -1,3 +1,4 @@ +import bugsnagClient from './bugsnag'; import origins from '../origins'; const browser = require('webextension-polyfill'); @@ -195,6 +196,19 @@ export default class Db { }); } + setMultiple (settings) { + return browser.storage.sync + .set(settings) + .catch((e) => { + console.error(`Error attempting to save settings:`, settings, e); + }) + .finally(() => { + if (process.env.DEBUG) { + console.info(`Saved multiple settings :`, settings); + } + }); + } + getLocalCollection (key) { let collection = localStorage.getItem(key); if (!collection) { @@ -244,4 +258,39 @@ export default class Db { callback(); } } + + _migrateToStorageSync () { + console.info('Migrating settings to v2'); + bugsnagClient.leaveBreadcrumb('Attempting settings migration to v2'); + + try { + const allSettings = { ...DEFAULT_SETTINGS, ...CORE_SETTINGS }; + const oldSettings = Object.keys(allSettings) + .reduce((accumulator, key) => { + const defaultValue = allSettings[key]; + let value = localStorage.getItem(key); + if (value && typeof defaultValue === 'boolean') { + value = JSON.parse(value); + } + accumulator[key] = (typeof value === 'undefined' ? defaultValue : value); + return accumulator; + }, {}); + + if (process.env.DEBUG) { + console.log('Found old settings: ', oldSettings); + } + + this.setMultiple(oldSettings) + .then(() => { + console.info('Succesully migrated old settings to v2'); + bugsnagClient.leaveBreadcrumb('Migrated settings to v2'); + }) + .catch((e) => { + console.error('Failed to migrate settings to v2; '); + bugsnagClient.notify(e); + }); + } catch (e) { + bugsnagClient.notify(e); + } + } }