Chrome扩展程序-无法在chrome.browserAction.setIcon之后更改回默认图标

qlckcl4x  于 2023-08-01  发布在  Go
关注(0)|答案(1)|浏览(97)

我在试着打开和关闭分机。在此期间,图标应该从彩色变为灰色,然后在重新打开后返回彩色。当它从彩色变为灰色时,它工作正常,然后在尝试将其返回后返回错误:

Could not load action icon '/images/non-grey/icon16.png'.
Could not load action icon '/images/non-grey/icon32.png'.
Could not load action icon '/images/non-grey/icon48.png'.
Could not load action icon '/images/non-grey/icon128.png'.

字符串
这就是代码:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  console.log(request.action);
  if (enabled) {
    if (request.action == "check-window") {
      let querying = chrome.tabs.query({
        title: "ImageSafetyBackgroundProcess",               
    }, function(array_of_Tabs) {
        if (array_of_Tabs.length == 0) {
          window.open("popup.html", "popup");
        }
      });
    }
  }
  if (request.action == "enable-disable") {
    if (enabled) {
      enabled = false;
      chrome.browserAction.setIcon({ path: {
        16: "/images/grey/greyedicon16.png",
        32: "/images/grey/greyedicon32.png",
        48: "/images/grey/greyedicon48.png",
        128: "/images/grey/greyedicon128.png"
      }});
      let querying = chrome.tabs.query({
        title: "ImageSafetyBackgroundProcess",               
        }, function(array_of_Tabs) {
        if (array_of_Tabs.length > 0) {
          chrome.tabs.remove(array_of_Tabs[0].id, function() { });
        }
      });
    }
    else {
      enabled = true;
      window.open("popup.html", "popup");
      chrome.browserAction.setIcon({ path: {
        16: "/images/non-grey/icon16.png",
        32: "/images/non-grey/icon32.png",
        48: "/images/non-grey/icon48.png",
        128: "/images/non-grey/icon128.png"
      }});
    }
  } });


Manifest.json:

{
  "manifest_version": 2,
  "name": "Borderify",
  "version": "1.0",
  "description": "Adds a red border to all webpages matching mozilla.org.",
  "web_accessible_resources": [
    "images/question.png"
  ],
  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
  "permissions": [
    "tabs",
    "activeTab",
    "storage",
    "downloads",
    "webRequest",
    "webRequestBlocking",
    "<all_urls>"
  ],
  "browser_action": {
    "default_popup": "button.html",
    "default_icon": {
      "16": "/images/icon16.png",
      "32": "/images/icon32.png",
      "48": "/images/icon48.png",
      "128": "/images/icon128.png"
    }
  },
  "content_scripts": [
    {
      "matches": [
        "http://*/*",
        "https://*/*"
      ],
      "js": [
        "content-script.js"
      ],
      "run_at": "document_end"
    }
  ],
  "background": {
    "scripts": [
      "main.js"
    ]
  }
}

wfauudbj

wfauudbj1#

background.js中使用此代码,以便在刷新时更改图标

chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
    if (changeInfo.status === 'loading') {
      chrome.action.setIcon({ path: 'images/icon-32.png' });
    }
  });

字符串

相关问题