Skip to content

Commit

Permalink
migrate options to chrome storage when get
Browse files Browse the repository at this point in the history
  • Loading branch information
hogashi committed Jan 4, 2023
1 parent 5cf8659 commit 5d56804
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,38 @@ import { MessageRequest, MessageResponseBool } from './utils';

// バックグラウンドで実行される

const MIGRATED_TO_CHROME_STORAGE = 'MIGRATED_TO_CHROME_STORAGE';

window.chrome.runtime.onMessage.addListener(
(request: MessageRequest, _, sendResponse: (res: MessageResponseBool) => void) => {
// console.log(chrome.runtime.lastError);
if (request.method === GET_LOCAL_STORAGE) {
// chrome.storageから取ってきつつ,
// ない値はlocalStorageあるいは初期値で埋める
// TODO: むずい感じになってきたので, テストできるようにlocalStorageとchrome.storageだけ返すことにしたい
chrome.storage.sync.get(OPTION_KEYS, (got) => {
const newOptions = { ...initialOptions, ...localStorage };
// 真偽値にして返す
const newOptionsBool = { ...initialOptionsBool };
OPTION_KEYS.forEach((key) => {
newOptionsBool[key] = newOptions[key] === isTrue;
});
sendResponse({ data: { ...newOptionsBool, ...got } });
// まだ移行してないときはlocalStorageあるいは初期値を移行
chrome.storage.sync.get(MIGRATED_TO_CHROME_STORAGE, (isMigrated) => {
if (!isMigrated[MIGRATED_TO_CHROME_STORAGE]) {
const newOptions = { ...initialOptions, ...localStorage };
// 真偽値にして移行する
const newOptionsBool = { ...initialOptionsBool };
OPTION_KEYS.forEach((key) => {
newOptionsBool[key] = newOptions[key] === isTrue;
});
chrome.storage.sync.set(
{
...newOptionsBool,
[MIGRATED_TO_CHROME_STORAGE]: true,
},
() => {
// 移行できたら新しい値を返す
sendResponse({ data: newOptionsBool });
},
);
} else {
chrome.storage.sync.get(OPTION_KEYS, (got) => {
// 初期値をフォールバックとしておく
sendResponse({ data: { ...initialOptionsBool, ...got } });
});
}
});
} else {
sendResponse({ data: null });
Expand Down

0 comments on commit 5d56804

Please sign in to comment.