浏览代码

fix: upload file block not working

Ahmad Kholid 2 年之前
父节点
当前提交
86647c77eb
共有 3 个文件被更改,包括 59 次插入26 次删除
  1. 1 1
      src/content/blocksHandler/handlerUploadFile.js
  2. 7 4
      src/newtab/workflowEngine/injectContentScript.js
  3. 51 21
      src/utils/getFile.js

+ 1 - 1
src/content/blocksHandler/handlerUploadFile.js

@@ -32,7 +32,7 @@ export default async function (block) {
       const name = file.path?.replace(/^.*[\\/]/, '') || '';
       const blob = await fetch(file.objUrl).then((response) => response.blob());
 
-      URL.revokeObjectURL(file.objUrl);
+      if (file.objUrl.startsWith('blob')) URL.revokeObjectURL(file.objUrl);
 
       fileObject = new File([blob], name, { type: file.type });
     }

+ 7 - 4
src/newtab/workflowEngine/injectContentScript.js

@@ -28,10 +28,13 @@ export default function (tabId, frameId = 0) {
 
         tryCount += 1;
 
-        await browser.tabs.executeScript(tabId, {
-          allFrames: true,
-          runAt: 'document_start',
-          file: './contentScript.bundle.js',
+        await browser.scripting.executeScript({
+          target: {
+            tabId,
+            allFrames: true,
+          },
+          injectImmediately: true,
+          files: ['./contentScript.bundle.js'],
         });
         const isScriptExists = await contentScriptExist(tabId, currentFrameId);
 

+ 51 - 21
src/utils/getFile.js

@@ -24,30 +24,60 @@ function getLocalFile(path, options) {
 
     const fileUrl = path?.startsWith('file://') ? path : `file://${path}`;
 
-    const xhr = new XMLHttpRequest();
-    xhr.responseType = options.responseType || 'blob';
-    xhr.onreadystatechange = () => {
-      if (xhr.readyState === XMLHttpRequest.DONE) {
-        if (xhr.status === 0 || xhr.status === 200) {
+    /* eslint-disable-next-line */
+    if ('XMLHttpRequest' in self) {
+      const xhr = new XMLHttpRequest();
+      xhr.responseType = options.responseType || 'blob';
+      xhr.onreadystatechange = () => {
+        if (xhr.readyState === XMLHttpRequest.DONE) {
+          if (xhr.status === 0 || xhr.status === 200) {
+            if (options.returnValue) {
+              resolve(xhr.response);
+              return;
+            }
+
+            const objUrl = URL.createObjectURL(xhr.response);
+            resolve({ path, objUrl, type: xhr.response.type });
+          } else {
+            reject(new Error(xhr.statusText));
+          }
+        }
+      };
+      xhr.onerror = function () {
+        reject(
+          new Error(xhr.statusText || `Can't find a file with "${path}" path`)
+        );
+      };
+      xhr.open('GET', fileUrl);
+      xhr.send();
+    } else {
+      fetch(fileUrl)
+        .then((response) => {
+          if (!response.ok) throw new Error(response.statusText);
+
           if (options.returnValue) {
-            resolve(xhr.response);
-            return;
+            resolve(response);
+            return Promise.resolve(null);
           }
 
-          const objUrl = URL.createObjectURL(xhr.response);
-          resolve({ path, objUrl, type: xhr.response.type });
-        } else {
-          reject(new Error(xhr.statusText));
-        }
-      }
-    };
-    xhr.onerror = function () {
-      reject(
-        new Error(xhr.statusText || `Can't find a file with "${path}" path`)
-      );
-    };
-    xhr.open('GET', fileUrl);
-    xhr.send();
+          return response.blob();
+        })
+        .then((blob) => {
+          if (!blob) return;
+
+          if (URL.createObjectURL) {
+            const objUrl = URL.createObjectURL(blob);
+            resolve({ path, objUrl, type: blob.type });
+          } else {
+            const reader = new FileReader();
+            reader.onload = () => {
+              resolve({ path, objUrl: reader.result, type: blob.type });
+            };
+            reader.readAsDataURL(blob);
+          }
+        })
+        .catch(reject);
+    }
   });
 }