Migrate data from v1

This commit is contained in:
Corbin Davenport 2023-02-20 01:32:58 -05:00
parent 3e11a1a4a5
commit f3197ad86c
1 changed files with 22 additions and 3 deletions

View File

@ -1,7 +1,5 @@
// Initialize welcome message and context menu entry on extension load
chrome.runtime.onInstalled.addListener(function (details) {
// Initialize context menu
createContextMenu()
// Show welcome message
if (details.reason === 'install' || details.reason === 'update') {
// Set message
@ -38,6 +36,21 @@ chrome.runtime.onInstalled.addListener(function (details) {
}
})
// Function for migrating data from version 1.0
// Version 1.0 saved a single server in a "userServer" string
async function migrateOldData() {
var data = await chrome.storage.sync.get()
if (data.userServer) {
var oldServer = data.userServer
console('Migrating server selection ' + oldServer + ' to new format...')
// Move data
await chrome.storage.sync.set({serverList: [oldServer]})
// Delete old data
await chrome.storage.sync.remove('userServer')
console.log('Migration complete!')
}
}
// Function for creating context menu entries
async function createContextMenu() {
// Remove existing entries if they exist
@ -125,4 +138,10 @@ function createPopup(serverUrl, shareLink, shareText, tab) {
// Function for action button
chrome.action.onClicked.addListener(async function (tab) {
createPopup('generic', tab.url, tab.title, tab)
})
})
// Initialize context menu
createContextMenu()
// Migrate data if needed
migrateOldData()