Prechádzať zdrojové kódy

fix: utomaFetch function not returning data (#1491)

Ahmad Kholid 1 rok pred
rodič
commit
109e9a4930
1 zmenil súbory, kde vykonal 35 pridanie a 0 odobranie
  1. 35 0
      src/background/index.js

+ 35 - 0
src/background/index.js

@@ -253,4 +253,39 @@ if (!isMV2) {
   sandboxIframe.id = 'sandbox';
 
   document.body.appendChild(sandboxIframe);
+
+  window.addEventListener('message', async ({ data }) => {
+    if (data?.type !== 'automa-fetch') return;
+
+    const sendResponse = (result) => {
+      sandboxIframe.contentWindow.postMessage(
+        {
+          type: 'fetchResponse',
+          data: result,
+          id: data.data.id,
+        },
+        '*'
+      );
+    };
+
+    const { type, resource } = data.data;
+    try {
+      const response = await fetch(resource.url, resource);
+      if (!response.ok) throw new Error(response.statusText);
+
+      let result = null;
+
+      if (type === 'base64') {
+        const blob = await response.blob();
+        const base64 = await readFileAsBase64(blob);
+
+        result = base64;
+      } else {
+        result = await response[type]();
+      }
+      sendResponse({ isError: false, result });
+    } catch (error) {
+      sendResponse({ isError: true, result: error.message });
+    }
+  });
 }