diff --git a/src/background/messages/hello.ts b/src/background/messages/hello.ts index ffce4c3..3dc8697 100644 --- a/src/background/messages/hello.ts +++ b/src/background/messages/hello.ts @@ -3,20 +3,21 @@ import type { PlasmoMessaging } from '@plasmohq/messaging'; import { getVersion } from '~hooks/useVersion'; import type { BaseRequest } from '~types/request'; import type { BaseResponse } from '~types/response'; -import { assertDomainWhitelist } from '~utils/storage'; +import { isDomainWhitelisted } from '~utils/storage'; type Response = BaseResponse<{ version: string; + allowed: boolean; }>; const handler: PlasmoMessaging.MessageHandler = async (req, res) => { try { - await assertDomainWhitelist(req.body.requestDomain); const version = getVersion(); res.send({ success: true, version, + allowed: await isDomainWhitelisted(req.body.requestDomain), }); } catch (err) { res.send({ diff --git a/src/utils/storage.ts b/src/utils/storage.ts index 7b52595..6314da6 100644 --- a/src/utils/storage.ts +++ b/src/utils/storage.ts @@ -3,7 +3,7 @@ import { useStorage } from '@plasmohq/storage/hook'; import { makeUrlIntoDomain } from '~utils/domains'; -export const DEFAULT_DOMAIN_WHITELIST = ['https://movie-web.app', 'http://localhost:5173']; +export const DEFAULT_DOMAIN_WHITELIST = ['movie-web.app', 'localhost:5173']; export const storage = new Storage(); @@ -16,9 +16,13 @@ export function useDomainStorage() { return useStorage('domainWhitelist', (v) => v ?? DEFAULT_DOMAIN_WHITELIST); } -export const assertDomainWhitelist = async (url: string) => { +export const isDomainWhitelisted = async (url: string) => { const domain = makeUrlIntoDomain(url); - if (!domain) throw new Error('Domain is from a normal tab'); - const isWhiteListed = await domainIsInWhitelist(domain); + if (!domain) return false; + return domainIsInWhitelist(domain); +}; + +export const assertDomainWhitelist = async (url: string) => { + const isWhiteListed = await isDomainWhitelisted(url); if (!isWhiteListed) throw new Error('Domain is not whitelisted'); };