Преглед изворни кода

Fix issue when hosted in vscode

Gary Hsu пре 6 година
родитељ
комит
472475d61b
1 измењених фајлова са 9 додато и 6 уклоњено
  1. 9 6
      src/Misc/tools.ts

+ 9 - 6
src/Misc/tools.ts

@@ -760,20 +760,19 @@ export class Tools {
         Tools.SetCorsBehavior(url, img);
 
         const loadHandler = () => {
-            if (usingObjectURL && img.src) {
-                URL.revokeObjectURL(img.src);
-            }
-
             img.removeEventListener("load", loadHandler);
             img.removeEventListener("error", errorHandler);
+
             onLoad(img);
-        };
 
-        const errorHandler = (err: any) => {
+            // Must revoke the URL after calling onLoad to avoid security exceptions in
+            // certain scenarios (e.g. when hosted in vscode).
             if (usingObjectURL && img.src) {
                 URL.revokeObjectURL(img.src);
             }
+        };
 
+        const errorHandler = (err: any) => {
             img.removeEventListener("load", loadHandler);
             img.removeEventListener("error", errorHandler);
 
@@ -782,6 +781,10 @@ export class Tools {
             if (onError) {
                 onError("Error while trying to load image: " + input, err);
             }
+
+            if (usingObjectURL && img.src) {
+                URL.revokeObjectURL(img.src);
+            }
         };
 
         img.addEventListener("load", loadHandler);