From 47e306dcdb6e64fa0fe5be97f73155d53d364f3f Mon Sep 17 00:00:00 2001 From: Bauke Date: Tue, 23 Feb 2021 16:55:40 +0100 Subject: [PATCH] Only open the extension page when the version gets updated. Firefox "reinstalls" extensions when it gets updated so we have to work around it so we only open the extension when the version actually changes. --- source/background.ts | 24 +++++++++++++++++++++++- source/manifest.json | 2 +- source/settings/defaults.ts | 3 ++- source/settings/index.ts | 10 ++++++---- source/utilities/index.ts | 1 + source/utilities/version.ts | 7 +++++++ 6 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 source/utilities/version.ts diff --git a/source/background.ts b/source/background.ts index 76188f5..903ac45 100644 --- a/source/background.ts +++ b/source/background.ts @@ -1,10 +1,32 @@ import {browser} from 'webextension-polyfill-ts'; +import { + defaultSettings, + getManifest, + getSettings, + setSettings, + versionAsNumber +} from '.'; // Add listeners to open the options page when: // * The extension icon is clicked. // * The extension is installed. browser.browserAction.onClicked.addListener(openOptionsPage); -browser.runtime.onInstalled.addListener(openOptionsPage); +browser.runtime.onInstalled.addListener(async () => { + const manifest = getManifest(); + const settings = await getSettings(); + const versionGotUpdated = + versionAsNumber(manifest.version) > + versionAsNumber(settings.data.version ?? defaultSettings.data.version!); + + if (versionGotUpdated) { + settings.data.version = manifest.version; + await setSettings(settings); + } + + if (versionGotUpdated || manifest.nodeEnv === 'development') { + await openOptionsPage(); + } +}); async function openOptionsPage() { await browser.runtime.openOptionsPage(); diff --git a/source/manifest.json b/source/manifest.json index 54fbec5..b471616 100644 --- a/source/manifest.json +++ b/source/manifest.json @@ -3,7 +3,7 @@ "manifest_version": 2, "name": "Tildes ReExtended", "description": "An updated and reimagined recreation of Tildes Extended to enhance and improve the experience of Tildes.net.", - "version": "1.0.1", + "version": "1.0.2", "permissions": [ "downloads", "storage", diff --git a/source/settings/defaults.ts b/source/settings/defaults.ts index 4685faf..7068108 100644 --- a/source/settings/defaults.ts +++ b/source/settings/defaults.ts @@ -56,7 +56,8 @@ export const defaultSettings: Settings = { '~tv' ], latestActiveFeatureTab: 'debug', - userLabels: [] + userLabels: [], + version: '0.0.0' }, features: { autocomplete: true, diff --git a/source/settings/index.ts b/source/settings/index.ts index 2d0ae5f..05fdd12 100644 --- a/source/settings/index.ts +++ b/source/settings/index.ts @@ -51,10 +51,12 @@ export async function getSettings(): Promise { features: {...defaultSettings.features, ...syncSettings.features} }; - window.TildesReExtended.debug = settings.features.debug; - // If we're in development, force debug output. - if (getManifest().nodeEnv === 'development') { - window.TildesReExtended.debug = true; + if (window?.TildesReExtended !== undefined) { + window.TildesReExtended.debug = settings.features.debug; + // If we're in development, force debug output. + if (getManifest().nodeEnv === 'development') { + window.TildesReExtended.debug = true; + } } return settings; diff --git a/source/utilities/index.ts b/source/utilities/index.ts index 7abd515..61143eb 100644 --- a/source/utilities/index.ts +++ b/source/utilities/index.ts @@ -48,3 +48,4 @@ export * from './groups'; export * from './query-selectors'; export * from './report-a-bug'; export * from './validators'; +export * from './version'; diff --git a/source/utilities/version.ts b/source/utilities/version.ts new file mode 100644 index 0000000..e987900 --- /dev/null +++ b/source/utilities/version.ts @@ -0,0 +1,7 @@ +/** + * Returns a version string as a number by removing the periods. + * @param version + */ +export function versionAsNumber(version: string): number { + return Number(version.replace(/\./g, '')); +}