Skip to content

Commit

Permalink
fix: move code for migration to the background script #251 (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
Robot-Inventor authored Oct 21, 2023
1 parent b4a2410 commit c174a02
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
3 changes: 3 additions & 0 deletions src/ts/background.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { ONBOARDING_URL, RELEASE_NOTE_URL } from "./common/constants";
import browser from "webextension-polyfill";
import { migrateFromV1ToV2 } from "./migrator";

browser.runtime.onInstalled.addListener((details) => {
const isJapanese = browser.i18n.getUILanguage() === "ja";

if (details.reason === "update") {
void migrateFromV1ToV2();

const releaseNoteURL = isJapanese ? RELEASE_NOTE_URL.ja : RELEASE_NOTE_URL.en;
void browser.tabs.create({ url: releaseNoteURL });

Expand Down
27 changes: 12 additions & 15 deletions src/ts/browserAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import "@material/web/checkbox/checkbox.js";
import { DEFAULT_SETTINGS } from "./common/defaultSettings";
import browser from "webextension-polyfill";
import { isSettings } from "./@types/common/settings.guard";
import { migrateFromV1ToV2 } from "./common/migrator";

const translationTargets: NodeListOf<HTMLElement> = document.querySelectorAll("[data-translation]");
for (const translationTarget of translationTargets) {
Expand All @@ -20,22 +19,20 @@ for (const translationLink of translationLinks) {
translationLink.setAttribute("href", url);
}

void migrateFromV1ToV2().then(() => {
const checkboxElements = document.querySelectorAll("md-checkbox");
const checkboxElements = document.querySelectorAll("md-checkbox");

for (const checkbox of checkboxElements) {
void browser.storage.local.get(DEFAULT_SETTINGS).then((currentSettings) => {
if (!isSettings(currentSettings))
// eslint-disable-next-line curly, nonblock-statement-body-position
throw new Error(`Failed to get ${checkbox.name} from storage`);
checkbox.checked = currentSettings[checkbox.name as keyof typeof DEFAULT_SETTINGS];
});
for (const checkbox of checkboxElements) {
void browser.storage.local.get(DEFAULT_SETTINGS).then((currentSettings) => {
if (!isSettings(currentSettings))
// eslint-disable-next-line curly, nonblock-statement-body-position
throw new Error(`Failed to get ${checkbox.name} from storage`);
checkbox.checked = currentSettings[checkbox.name as keyof typeof DEFAULT_SETTINGS];
});

checkbox.addEventListener("change", () => {
void browser.storage.local.set({ [checkbox.name]: checkbox.checked });
});
}
});
checkbox.addEventListener("change", () => {
void browser.storage.local.set({ [checkbox.name]: checkbox.checked });
});
}

const { version } = browser.runtime.getManifest();
const versionElement = document.getElementById("version-number");
Expand Down
3 changes: 0 additions & 3 deletions src/ts/contentScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import { EVENT_GENERATOR_ID, EVENT_GENERATOR_SETTINGS_ATTRIBUTE } from "./common
import { DEFAULT_SETTINGS } from "./common/defaultSettings";
import { Translator } from "./common/translator";
import browser from "webextension-polyfill";
import { migrateFromV1ToV2 } from "./common/migrator";

// eslint-disable-next-line max-statements
const main = async () => {
await migrateFromV1ToV2();

const settings = await browser.storage.local.get(DEFAULT_SETTINGS);

const translator = new Translator((key) => browser.i18n.getMessage(key), browser.runtime.getURL("dist/image/"));
Expand Down
File renamed without changes.

0 comments on commit c174a02

Please sign in to comment.