From 211889cada8935615576ee27588e391c948c74ca Mon Sep 17 00:00:00 2001 From: SimonStnn Date: Mon, 17 Jun 2024 18:28:10 +0200 Subject: [PATCH 1/2] Allow user to grant permissions in header banner #206 --- src/popup/components/Header.tsx | 34 ++++++++++++++++++++++++++++----- src/utils.ts | 9 +++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/popup/components/Header.tsx b/src/popup/components/Header.tsx index 6055909b..314b2796 100644 --- a/src/popup/components/Header.tsx +++ b/src/popup/components/Header.tsx @@ -1,5 +1,5 @@ import React, { useState, useEffect } from 'react'; -import browser from 'webextension-polyfill'; +import browser, { type Permissions } from 'webextension-polyfill'; import { useLocation } from 'react-router-dom'; import { ClassValue } from 'clsx'; import { LucideIcon, RotateCcw } from 'lucide-react'; @@ -7,6 +7,7 @@ import { Link } from 'react-router-dom'; import { ArrowLeft, List, Settings } from 'lucide-react'; import remote from '@/remote'; import { Button } from './ui/button'; +import { askOriginPermissions } from '@/utils'; type iconProps = { to: string; @@ -41,24 +42,30 @@ const HeaderIcon = (props: iconProps) => { const Banner = (props: BannerProps) => { if (!props.remoteAvailable) return ( -
+
Remote not available
); const [alarms, setAlarms] = useState([]); - + const [permissions, setPermissions] = useState( + {} + ); useEffect(() => { const fetchAlarms = async () => { const alarms = await browser.alarms.getAll(); setAlarms(alarms); }; + const fetchPermissions = async () => { + setPermissions(await browser.permissions.getAll()); + }; fetchAlarms(); + fetchPermissions(); }, []); if (alarms.length === 0) return ( -
+
Something went wrong, please restart the extension.
); + if (permissions.origins && permissions.origins.length === 0) + return ( +
+ Pop-a-loon is missing recommended permissions. + +
+ ); + return null; }; @@ -94,7 +118,7 @@ export default (props: HeaderProps) => { return ( <> -
+
{location.pathname !== '/' && }
diff --git a/src/utils.ts b/src/utils.ts index a0e722e4..f5f7bfb4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -130,3 +130,12 @@ export async function importStylesheet(id: string, href: string) { document.head.appendChild(style); } } + +export async function askOriginPermissions() { + const host_permissions = await browser.runtime.getManifest().host_permissions; + if (!host_permissions) return log.error('No host_permissions found'); + const permissions = await browser.permissions.request({ + origins: host_permissions, + }); + log.debug('Permissions granted for', permissions); +} From b3fca3b416cf70a79e7377eaebec676137f41922 Mon Sep 17 00:00:00 2001 From: SimonStnn Date: Mon, 17 Jun 2024 18:28:49 +0200 Subject: [PATCH 2/2] Use ask permission function from utils --- src/popup/components/forms/LocalSettings.tsx | 24 ++++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/popup/components/forms/LocalSettings.tsx b/src/popup/components/forms/LocalSettings.tsx index 5d993e67..b327143f 100644 --- a/src/popup/components/forms/LocalSettings.tsx +++ b/src/popup/components/forms/LocalSettings.tsx @@ -16,6 +16,7 @@ import InfoIcon from '@/components/InfoIcon'; import { Default as DefaultBalloon } from '@/balloons'; import storage from '@/managers/storage'; import log from '@/managers/log'; +import { askOriginPermissions } from '@/utils'; const MIN_POP_VOLUME = 0; const VOLUME_STEP = 20; @@ -77,14 +78,7 @@ export default () => { }; const onGrantOriginPermissionClick = async () => { - const host_permissions = - await browser.runtime.getManifest().host_permissions; - if (!host_permissions) return log.error('No host_permissions found'); - const permissions = await browser.permissions.request({ - origins: host_permissions, - }); - log.debug('Permissions granted for', permissions); - + await askOriginPermissions(); setPermissions(await browser.permissions.getAll()); }; @@ -113,10 +107,10 @@ export default () => { {popVolume} -

+

Pop Volume

-

+

The volume of the pop sound when a balloon is popped.

@@ -148,10 +142,10 @@ export default () => { x{spawnRate} -

+

Spawn Rate

-

+

The rate at which balloons spawn. A lower value means less balloons will spawn.

@@ -184,11 +178,11 @@ export default () => { Host Permission -

+

Host Permission{' '} - *recommended + *recommended

-

+

Pop-a-loon requires host permissions to function properly.