每个选项卡使用chrome.browserAction.setPopup [英] Using chrome.browserAction.setPopup per tab

查看:121
本文介绍了每个选项卡使用chrome.browserAction.setPopup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Chrome扩展程序,它根据当前URL动态更改弹出窗口的内容。 js,它可以正常工作:

  if(domains.contains(request.url)){
chrome.browserAction。 setPopup({
popup:tracking.html
});
} else {
chrome.browserAction.setPopup({
popup:nottracking.html
});
}

问题是,如果我切换选项卡,弹出窗口的内容保持标签之间相同。什么是正确的策略来处理这个问题?




  • 以某种方式挂钩选项卡更改事件(如果存在这种可能性)?

  • 将弹出内容的更改限制为当前选项卡? (我注意到chrome.browserAction.setPopup中有一个可选的 tabId 参数,但文档有点不足)




  • 所有帮助非常感谢!

    解决方案

    选项1,绑定事件监听器:使用 chrome.tabs.onUpdated 监听URI更改,然后加上 chrome.browserAction.setPopup $ c> tabId 设置给定选项卡的弹出窗口。例如:
    $ b

      chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab) {
    if(domains.contains(tab.url)){
    chrome.browserAction.setPopup({
    tabId:tabId,
    popup:'tracking.html'
    });
    } else {
    chrome.browserAction.setPopup({
    tabId:tabId,
    popup:'nottracking.html'
    });
    }
    });


    I'm writing a Chrome extension which dynamically changes the content of the popup window based on the current URL.

    I'm doing something like this in background.js, which works fine:

    if(domains.contains(request.url)){
        chrome.browserAction.setPopup({
            popup: "tracking.html"
        });
    }else{
        chrome.browserAction.setPopup({
            popup: "nottracking.html"
        });
    }
    

    The problem is that if I switch tab, the content of the popup stays the same between tabs. What's the correct strategy to deal with this?

    • Hook into the tab change event somehow (if such a possibility exists)?
    • Limit the change of popup contents to the current tab? (I did notice that there's an optional tabId parameter for chrome.browserAction.setPopup, but the docs are a bit scant)
    • Something else?

    All help very much appreciated!

    解决方案

    Option 1, bind an event listener:

    Use chrome.tabs.onUpdated to listen for URI changes, followed by chrome.browserAction.setPopup with a given tabId to set the popup for the given tab. For example:

    chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
        if (domains.contains(tab.url)) {
            chrome.browserAction.setPopup({
                tabId: tabId,
                popup: 'tracking.html'
            });
        } else {
            chrome.browserAction.setPopup({
                tabId: tabId,
                popup: 'nottracking.html'
            });
        }
    });
    

    这篇关于每个选项卡使用chrome.browserAction.setPopup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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