Files
ozon-seller-kit/extension-v1/entrypoints/background.ts
T

24 lines
820 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Background Service Worker - 唯一出网口(绕 CORS 取图)
export default defineBackground(() => {
console.log('Seller Helper background started');
// 点击扩展图标 → 打开 Side Panel
chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true });
// 代理图片 fetchCORS 绕过)
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.action === 'fetchImage') {
fetchImageAsBlob(msg.url)
.then(blob => sendResponse({ ok: true, blob }))
.catch(err => sendResponse({ ok: false, error: err.message }));
return true; // 保持异步通道
}
});
});
async function fetchImageAsBlob(url: string): Promise<Blob> {
const res = await fetch(url);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.blob();
}