Gerald 10 лет назад
Родитель
Сommit
5512ccaed9
2 измененных файлов с 22 добавлено и 15 удалено
  1. 4 2
      src/background/db.js
  2. 18 13
      src/options/view.js

+ 4 - 2
src/background/db.js

@@ -176,7 +176,8 @@ VMDB.prototype.getData = function () {
       var data = {};
       var cache = {};
       data.scripts = scripts.map(function (script) {
-        if (scriptUtils.isRemote(script.meta.icon)) cache[script.meta.icon] = 1;
+        var icon = script.meta.icon;
+        if (scriptUtils.isRemote(icon)) cache[icon] = 1;
         return scriptUtils.getScriptInfo(script);
       });
       data.cache = Object.keys(cache);
@@ -259,7 +260,8 @@ VMDB.prototype.getCacheB64 = function (urls, tx) {
     });
   })).then(function (data) {
     return data.reduce(function (map, value, i) {
-      map[urls[i]] = value;
+      map[urls[i]] = value.data;
+      return map;
     }, {});
   });
 };

+ 18 - 13
src/options/view.js

@@ -23,9 +23,9 @@ var ScriptView = BaseView.extend({
     var _this = this;
     var icon = _this.model.get('meta').icon;
     if (icon && icon !== _this.model.get('_icon'))
-      _this.loadImage(icon).then(function () {
-        _this.model.set('_icon', icon);
-      }, function () {
+      _this.loadImage(icon).then(function (url) {
+        _this.model.set('_icon', url);
+      }, function (url) {
         _this.model.set('_icon', DEFAULT_ICON);
       });
   },
@@ -55,16 +55,21 @@ var ScriptView = BaseView.extend({
   loadImage: function (url) {
     if (!url) return;
     var promise = this.images[url];
-    if (!promise) promise = this.images[url] = new Promise(function (resolve, reject) {
-      var img = new Image;
-      img.onload = function () {
-        resolve(img);
-      };
-      img.onerror = function () {
-        reject(url);
-      };
-      img.src = url;
-    });
+    if (!promise) {
+      var cache = scriptList.cache[url];
+      promise = cache ? Promise.resolve(cache)
+      : new Promise(function (resolve, reject) {
+        var img = new Image;
+        img.onload = function () {
+          resolve(url);
+        };
+        img.onerror = function () {
+          reject(url);
+        };
+        img.src = url;
+      });
+      this.images[url] = promise;
+    }
     return promise;
   },
   onEdit: function () {