浏览代码

fix: fetch non-cached icons, broken in d38a6a0e

tophf 3 年之前
父节点
当前提交
91e7f2dad6
共有 2 个文件被更改,包括 13 次插入6 次删除
  1. 7 2
      src/background/utils/icon.js
  2. 6 4
      src/common/load-script-icon.js

+ 7 - 2
src/background/utils/icon.js

@@ -1,13 +1,18 @@
-import { i18n, noop } from '@/common';
+import { i18n, makeDataUri, noop } from '@/common';
 import { ICON_PREFIX, INJECTABLE_TAB_URL_RE } from '@/common/consts';
 import { objectPick } from '@/common/object';
 import { postInitialize } from './init';
 import { commands, forEachTab } from './message';
 import { getOption, hookOptions } from './options';
 import { testBlacklist } from './tester';
+import storage from './storage';
 
 Object.assign(commands, {
-  GetImageData: async path => (await getOwnIcon(path)).uri,
+  GetImageData: async url => (
+    url.startsWith(ICON_PREFIX)
+      ? (await getOwnIcon(url)).uri
+      : (await storage.cache.fetch(url), makeDataUri(await storage.cache.getOne(url)))
+  ),
   SetBadge: setBadge,
 });
 

+ 6 - 4
src/common/load-script-icon.js

@@ -1,5 +1,5 @@
 import { ICON_PREFIX } from '@/common/consts';
-import { isDataUri, sendCmdDirectly } from '@/common/index';
+import { isDataUri, isRemote, sendCmdDirectly } from '@/common/index';
 
 // TODO: convert this into a component tag e.g. <safe-icon>
 const KEY = 'safeIcon';
@@ -11,9 +11,11 @@ const KEY = 'safeIcon';
  * @param {number} [defSize] - show default icon of this size, -1 = auto, falsy = no
  */
 export async function loadScriptIcon(script, cache = {}, defSize) {
+  let def;
   const { icon } = script.meta;
-  const url = script.custom?.pathMap?.[icon] || icon
-    || defSize && `${ICON_PREFIX}${defSize > 0 && defSize || (script.config.removed ? 32 : 38)}.png`;
+  const url = script.custom?.pathMap?.[icon] || icon || defSize && (
+    def = `${ICON_PREFIX}${defSize > 0 && defSize || (script.config.removed ? 32 : 38)}.png`
+  );
   if (!url || url !== script[KEY]) {
     // creates an observable property so Vue will see the change after `await`
     if (!(KEY in script)) {
@@ -22,7 +24,7 @@ export async function loadScriptIcon(script, cache = {}, defSize) {
     if (url) {
       script[KEY] = cache[url]
         || isDataUri(url) && url
-        || await sendCmdDirectly('GetImageData', url)
+        || (def || isRemote(url)) && await sendCmdDirectly('GetImageData', url)
         || null;
     }
   }