Chrome扩展程序 - 仅更改活动选项卡的default_icon [英] Chrome Extension - Change default_icon for active tab only

查看:292
本文介绍了Chrome扩展程序 - 仅更改活动选项卡的default_icon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个简单的扩展,它要求default_icon根据JS变量是否存在于页面中而发生变化。这是很简单的,至少这个功能。

I'm building a simple extension that requires the default_icon to change depending if a JS variable exists in a page or not. It's as simple as that, at least this feature.

我设法改变每个页面的图片加载这个条件,但我想进一步,并有每当用户更改他所关注的标签时,用户浏览器中所有加载的标签都会更改图片。
我在尝试将default_icon仅更改为活动选项卡时遇到了问题。

I managed to change the picture on each page load with this condition, but I would like to go further and have the picture changed for all loaded tabs in the user's browser, whenever the user changes the tabs he's focusing on. I'm having trouble trying to have the default_icon be changed for the active tab only.

有谁知道我该如何着手实现它?
(我仍然是Chrome扩展程序的初学者)

Would anyone know how I should proceed to make it so ? (I'm still a beginner in writing Chrome Extensions)

我的content.js:

My content.js :

if (test == "OK") {
    // Setting OK Icon
    chrome.runtime.sendMessage({"message": "Existing"});
}
else if (test == "NOK") {
    // Setting NOK Icon
    chrome.runtime.sendMessage({"message": "Not existing"});
}

我的background.js:

My background.js :

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if( request.message === "Existing" ) {
        chrome.browserAction.setIcon({path:"OKicon.png"});
    }

    if( request.message === "Not Existing" ) {
        chrome.browserAction.setIcon({path:"NOKicon.png"});
    }
  }
);


推荐答案

好的,你应该看看 chrome.browserAction.setIcon 文档
$ b

Well, you should take a look at the chrome.browserAction.setIcon documentation.


整数(可选) tabId

限制选择特定选项卡时的更改。当标签关闭时自动重置。

Limits the change to when a particular tab is selected. Automatically resets when the tab is closed.

正是需要的!现在,标签ID是在发件人
$ b

Just what was needed! Now, the tab ID is reported in the sender parameter:

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if( request.message === "Existing" ) {
        chrome.browserAction.setIcon({
            path:"OKicon.png",
            tabId: sender.tab.id
        });
    }

    if( request.message === "Not Existing" ) {
        chrome.browserAction.setIcon({
            path:"NOKicon.png",
            tabId: sender.tab.id
        });
    }
  }
);

这篇关于Chrome扩展程序 - 仅更改活动选项卡的default_icon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆