import type { PlasmoMessaging } from '@plasmohq/messaging'; import type { BaseRequest } from '~types/request'; import type { BaseResponse } from '~types/response'; import { setDynamicRules } from '~utils/declarativeNetRequest'; import { assertDomainWhitelist } from '~utils/storage'; interface Request extends BaseRequest { ruleId: number; targetDomains?: [string, ...string[]]; targetRegex?: string; requestHeaders?: Record; responseHeaders?: Record; } const handler: PlasmoMessaging.MessageHandler = async (req, res) => { try { if (!req.sender?.tab?.url) throw new Error('No tab URL found in the request.'); if (!req.body) throw new Error('No request body found in the request.'); await assertDomainWhitelist(req.sender.tab.url); await setDynamicRules(req.body); res.send({ success: true, }); } catch (err: any) { res.send({ success: false, error: err.message, }); } }; export default handler;