import { MUSEME_API_ORIGIN } from './origins' function dataURLtoBlob(dataURL) { let arr = dataURL.split(',') let mime = arr[0].match(/:(.*?);/)[1] let bstr = atob(arr[1]) let n = bstr.length let u8arr = new Uint8Array(n) while (n--) { u8arr[n] = bstr.charCodeAt(n) } return new Blob([u8arr], { type: mime }) } chrome.runtime.onConnect.addListener((port) => {}) chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { ;(async () => { const { type, data, x, y, width, height, videoId, t, q } = message switch (type) { case 'search': { const formData = new FormData() const blob = dataURLtoBlob(data) formData.append('image', blob) formData.append('question', q) // formData.append('rect', `${x},${y},${width},${height}`) // formData.append('videoId', videoId) // formData.append('t', t) let response: Response try { response = await fetch(`${MUSEME_API_ORIGIN}:8000/ask`, { method: 'POST', body: formData, }) } catch (err) { // eslint-disable-next-line no-console console.log('error', 'search', err) return } const result = await response.json() sendResponse(result) } break case 'select': { const formData = new FormData() const blob = dataURLtoBlob(data) formData.append('file', blob) formData.append('click_list', `${x},${y}`) formData.append('type', `1`) const response = await fetch( `https://gpueusrq3y8q40-8800.proxy.runpod.net/click1`, { method: 'POST', body: formData, } ) const result = await response.json() sendResponse(result) } break default: { sendResponse({ farewell: 'goodbye' }) } } })() return true }) export default null