Browse Source

fix requests to local files

Gerald 8 years ago
parent
commit
ce3f42d6f0
2 changed files with 31 additions and 22 deletions
  1. 30 21
      src/common/index.js
  2. 1 1
      src/options/views/confirm.vue

+ 30 - 21
src/common/index.js

@@ -128,28 +128,37 @@ export function request(url, options = {}) {
     Object.keys(headers).forEach(key => {
       xhr.setRequestHeader(key, headers[key]);
     });
-    xhr.onloadend = () => {
-      let data;
-      if (responseType === 'blob') {
-        data = xhr.response;
-      } else {
-        data = xhr.responseText;
-      }
-      if (responseType === 'json') {
-        try {
-          data = JSON.parse(data);
-        } catch (e) {
-          // Ignore invalid JSON
-        }
-      }
-      // xhr.status may be 0 or -1 if connection failed
-      (xhr.status >= 200 && xhr.status < 300 ? resolve : reject)({
-        url,
-        data,
-        status: xhr.status,
-        // xhr,
-      });
+    xhr.onload = () => {
+      const res = getResponse(xhr);
+      // status for `file:` protocol will always be `0`
+      res.status = xhr.status || 200;
+      resolve(res);
     };
+    xhr.onerror = () => {
+      const res = getResponse(xhr);
+      reject(res);
+    };
+    xhr.ontimeout = xhr.onerror;
     xhr.send(body);
   });
+  function getResponse(xhr) {
+    const { responseType } = options;
+    let data;
+    if (responseType === 'blob') {
+      data = xhr.response;
+    } else {
+      data = xhr.responseText;
+    }
+    if (responseType === 'json') {
+      try {
+        data = JSON.parse(data);
+      } catch (e) {
+        // Ignore invalid JSON
+      }
+    }
+    return {
+      url,
+      data,
+    };
+  }
 }

+ 1 - 1
src/options/views/confirm.vue

@@ -135,7 +135,7 @@ export default {
     close() {
       window.close();
     },
-    getFile(url, { isBlob, useCache }) {
+    getFile(url, { isBlob, useCache } = {}) {
       const cacheKey = isBlob ? `blob+${url}` : `text+${url}`;
       if (useCache && cache.has(cacheKey)) {
         return Promise.resolve(cache.get(cacheKey));