Files
extension/src/background/messages/prepareStream.ts
2024-03-15 18:49:12 +00:00

35 lines
1022 B
TypeScript

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<string, string>;
responseHeaders?: Record<string, string>;
}
const handler: PlasmoMessaging.MessageHandler<Request, BaseResponse> = 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;