You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

迁移至Manifest V3后无法加载chrome://favicon本地资源的解决方法

Chrome扩展Manifest V3中获取书签/标签页Favicon的修复方案

问题背景

将Chrome扩展从Manifest V2迁移到V3后,出现错误:

Not allowed to load local resource: chrome://favicon/size/16@1x/https://google.com/

Manifest V3中限制了网页内容(如新标签页、popup)直接访问chrome://协议资源,原V2中使用chrome://favicon路径加载图标的方式不再生效。

修复步骤

1. 确认Manifest权限

确保manifest.json已声明favicon权限(你的配置已包含,可跳过此步):

"permissions": [ "bookmarks", "storage", "sessions", "tabs", "favicon" ]

2. 替换chrome://favicon为chrome.favicon.get API

V3中必须通过Chrome官方提供的favicon API动态获取图标地址,修改代码如下:

/* append (almost) every tab */
for (let tab of tabs) {
  // 先创建基础元素结构
  const faviconImg = el('img');
  const element = el(
    'div.link.truncate',
    el('div.favicon', faviconImg),
    `${tab.deviceName} › ${tab.title}`,
    {
      href: '#',
      title: tab.title,
      'data-type': 'shortcut',
      click: () => chrome.sessions.restore(tab.sessionId),
      dragstart: (event) => event.dataTransfer.setData('text/plain', tab.url)
    }
  );

  // 调用API获取对应网站的favicon
  chrome.favicon.get({ url: tab.url }, (icons) => {
    if (icons?.length) {
      // 匹配需要的尺寸,优先取16px和32px的图标
      const icon16 = icons.find(icon => icon.size === 16) || icons[0];
      const icon32 = icons.find(icon => icon.size === 32) || icons[0];
      faviconImg.src = icon16.url;
      faviconImg.srcset = `${icon16.url}, ${icon32.url} 2x`;
    }
  });

  if (box.children.length < 5) {
    box.appendChild(element);
  }
}

3. 关键说明

  • chrome.favicon.get是异步API,需在回调中设置图片的src和srcset属性
  • API返回的icons数组包含不同尺寸的图标资源,可根据需求筛选对应size的条目
  • 若需获取书签的favicon,只需将tab.url替换为书签对象的url字段,调用逻辑完全一致

内容的提问来源于stack exchange,提问作者bread

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 07:25:39