浏览代码

fix: add back svg icons

Gerald 8 年之前
父节点
当前提交
35f56dbff7
共有 9 个文件被更改,包括 85 次插入111 次删除
  1. 2 2
      gulpfile.js
  2. 13 42
      src/background/sync/base.js
  3. 0 17
      src/background/utils/script.js
  4. 0 32
      src/cache.js
  5. 46 0
      src/common/index.js
  6. 9 0
      src/common/sprite.js
  7. 1 0
      src/options/app.js
  8. 13 18
      src/options/views/confirm.vue
  9. 1 0
      src/popup/app.js

+ 2 - 2
gulpfile.js

@@ -43,13 +43,13 @@ gulp.task('clean', () => del(['dist']));
 
 gulp.task('pack', ['manifest', 'copy-files', 'copy-i18n']);
 
-gulp.task('watch', ['pack', 'js-dev'], () => {
+gulp.task('watch', ['pack', 'js-dev', 'svg'], () => {
   gulp.watch(paths.manifest, ['manifest']);
   gulp.watch(paths.copy, ['copy-files']);
   gulp.watch(paths.locales.concat(paths.templates), ['copy-i18n']);
 });
 
-gulp.task('build', ['pack', 'js-prd']);
+gulp.task('build', ['pack', 'js-prd', 'svg']);
 
 gulp.task('js-dev', () => webpack(webpackConfig).watch({}, webpackCallback));
 gulp.task('js-prd', () => webpack(webpackConfig, webpackCallback));

+ 13 - 42
src/background/sync/base.js

@@ -1,4 +1,4 @@
-import { debounce, normalizeKeys, noop } from 'src/common';
+import { debounce, normalizeKeys, noop, request } from 'src/common';
 import getEventEmitter from '../utils/events';
 import { getOption, setOption, hookOptions } from '../utils/options';
 import { getScriptsByIndex, parseScript, saveScript, removeScript } from '../utils/db';
@@ -254,52 +254,23 @@ export const BaseService = serviceFactory({
     this.lastFetch = lastFetch;
     progress.total += 1;
     this.onStateChange();
-    return lastFetch.then(() => new Promise((resolve, reject) => {
-      const xhr = new XMLHttpRequest();
+    return lastFetch.then(() => {
       let { prefix } = options;
       if (prefix == null) prefix = this.urlPrefix;
-      xhr.open(options.method || 'GET', prefix + options.url, true);
       const headers = Object.assign({}, this.headers, options.headers);
-      if (options.body && typeof options.body === 'object') {
-        headers['Content-Type'] = 'application/json';
-        options.body = JSON.stringify(options.body);
-      }
-      Object.keys(headers).forEach(key => {
-        const value = headers[key];
-        if (value) xhr.setRequestHeader(key, value);
-      });
-      xhr.onloadend = () => {
+      return request(prefix + options.url, {
+        headers,
+        method: options.method,
+        body: options.body,
+      })
+      .then(data => ({ data }), error => ({ error }))
+      .then(({ data, error }) => {
         progress.finished += 1;
-        let data = xhr.responseText;
-        if (options.responseType === 'json') {
-          try {
-            data = JSON.parse(data);
-          } catch (e) {
-            // Invalid JSON data
-          }
-        }
         this.onStateChange();
-        // TODO Too Many Requests
-        // if (xhr.status === 503) {
-        // }
-        // net error: xhr.status === 0
-        if (xhr.status >= 200 && xhr.status < 300) {
-          resolve(data);
-        } else {
-          requestError(data);
-        }
-      };
-      xhr.send(options.body);
-
-      function requestError(data) {
-        reject({
-          xhr,
-          data,
-          url: options.url,
-          status: xhr.status,
-        });
-      }
-    }));
+        if (error) return Promise.reject(error);
+        return data;
+      });
+    });
   },
   sync() {
     this.progress = {

+ 0 - 17
src/background/utils/script.js

@@ -2,23 +2,6 @@ export function isRemote(url) {
   return url && !(/^(file|data):/.test(url));
 }
 
-export function fetch(url, type, headers) {
-  return new Promise((resolve, reject) => {
-    const xhr = new XMLHttpRequest();
-    xhr.open('GET', url, true);
-    if (type) xhr.responseType = type;
-    if (headers) {
-      Object.keys(headers).forEach(key => {
-        xhr.setRequestHeader(key, headers[key]);
-      });
-    }
-    xhr.onloadend = () => {
-      (xhr.status > 300 ? reject : resolve)(xhr);
-    };
-    xhr.send();
-  });
-}
-
 export function parseMeta(code) {
   // initialize meta, specify those with multiple values allowed
   const meta = {

+ 0 - 32
src/cache.js

@@ -1,32 +0,0 @@
-function getCache() {
-  function put(key, value) {
-    if (key in data) {
-      throw 'Key {' + key + '} already exists!';
-    }
-    data[key] = value;
-  }
-  function get(key) {
-    if (key in data) return data[key];
-    throw 'Cache not found: ' + key;
-  }
-  var data = {};
-  return {get: get, put: put};
-}
-
-var _ = require('./common');
-Vue.prototype.i18n = _.i18n;
-
-!function () {
-  var xhr = new XMLHttpRequest;
-  xhr.open('GET', '/public/sprite.svg', true);
-  xhr.onload = function () {
-    var div = document.createElement('div');
-    div.style.display = 'none';
-    div.innerHTML = xhr.responseText;
-    document.body.insertBefore(div, document.body.firstChild);
-  };
-  xhr.send();
-}();
-
-/* eslint-disable no-unused-vars */
-var cache = module.exports = getCache();

+ 46 - 0
src/common/index.js

@@ -106,3 +106,49 @@ export function getLocaleString(meta, key) {
   const langKey = navigator.languages.map(lang => `${key}:${lang}`).find(item => item in meta);
   return (langKey ? meta[langKey] : meta[key]) || '';
 }
+
+/**
+ * Make a request.
+ * @param {String} url
+ * @param {Object} headers
+ * @return Promise
+ */
+export function request(url, options = {}) {
+  return new Promise((resolve, reject) => {
+    const xhr = new XMLHttpRequest();
+    const { responseType } = options;
+    xhr.open(options.method || 'GET', url, true);
+    if (responseType) xhr.responseType = responseType;
+    const headers = Object.assign({}, options.headers);
+    let { body } = options;
+    if (body && typeof body === 'object') {
+      headers['Content-Type'] = 'application/json';
+      body = JSON.stringify(body);
+    }
+    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 > 300 ? reject : resolve)({
+        url,
+        data,
+        status: xhr.status,
+        // xhr,
+      });
+    };
+    xhr.send(body);
+  });
+}

+ 9 - 0
src/common/sprite.js

@@ -0,0 +1,9 @@
+import { request } from '.';
+
+request('/public/sprite.svg')
+.then(({ data }) => {
+  const div = document.createElement('div');
+  div.style.display = 'none';
+  div.innerHTML = data;
+  document.body.insertBefore(div, document.body.firstChild);
+});

+ 1 - 0
src/options/app.js

@@ -1,4 +1,5 @@
 import Vue from 'vue';
+import 'src/common/sprite';
 import { sendMessage, i18n } from 'src/common';
 import options from 'src/common/options';
 import { store, features } from './utils';

+ 13 - 18
src/options/views/confirm.vue

@@ -30,7 +30,7 @@
 </template>
 
 <script>
-import { sendMessage, zfill } from 'src/common';
+import { sendMessage, zfill, request } from 'src/common';
 import options from 'src/common/options';
 import VmCode from './code';
 import { store } from '../utils';
@@ -131,23 +131,18 @@ export default {
       window.close();
     },
     getFile(url, isBlob) {
-      return new Promise((resolve, reject) => {
-        const xhr = new XMLHttpRequest();
-        xhr.open('GET', url, true);
-        if (isBlob) xhr.responseType = 'blob';
-        xhr.onloadend = () => {
-          if (xhr.status > 300) return reject(url);
-          if (isBlob) {
-            const reader = new FileReader();
-            reader.onload = function onload() {
-              resolve(window.btoa(this.result));
-            };
-            reader.readAsBinaryString(xhr.response);
-          } else {
-            resolve(xhr.responseText);
-          }
-        };
-        xhr.send();
+      return request(url, {
+        responseType: isBlob ? 'blob' : null,
+      })
+      .then(data => {
+        if (!isBlob) return data;
+        return new Promise(resolve => {
+          const reader = new FileReader();
+          reader.onload = function onload() {
+            resolve(window.btoa(this.result));
+          };
+          reader.readAsBinaryString(data);
+        });
       });
     },
     getScript(url) {

+ 1 - 0
src/popup/app.js

@@ -1,4 +1,5 @@
 import Vue from 'vue';
+import 'src/common/sprite';
 import options from 'src/common/options';
 import { i18n, sendMessage } from 'src/common';
 import App from './views/app';