在使用chrome.tabs.onUpdated.addListener后访问给定的URL时删除侦听器 [英] Remove listener when given URL is visited after using chrome.tabs.onUpdated.addListener

查看:192
本文介绍了在使用chrome.tabs.onUpdated.addListener后访问给定的URL时删除侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:我正在开发Gmail的Chrome扩展程序,并且需要在更新扩展程序时应用一些更改。

例如,我想要请确保扩展程序更新后,扩展程序将在Gmail中显示警告对话框。这意味着在更新时检查Gmail是否已在Chrome窗口中打开,如果不想创建侦听器以等待将来加载Gmail,然后显示警报对话框。



下面的代码就是这样。但是,当发现新的Gmail选项卡时,我无法设法删除侦听器( chrome.tabs.onUpdated.removeListener ?)。



background.js


$ b

  // [...] 
//重新加载选项卡,其中Gmail已激活
函数reloadTab(order){
chrome.windows.getCurrent(function(win){
var cwin = win.id;
chrome.tabs.query({windowId:cwin},function(tabs){
var countGmailTabs = 0;
for(var i = 0; i< tabs.length; i ++){$ (t.match('mail.google.com')){
countGmailTabs + = 1;
if(order =
);
var t = tabs [i] .url; =='set'){
chrome.tabs.reload(tabs [i] .id);
} else if(order!=='set'){
var GmailTab =标签[i];
NewGmailURL(订单,GmailTab);
return;
}
}
}
//找不到Gmail!
if(countGmailTabs< 1&& order!=='set'){
chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab){
if( tab.url.match('mail.google.com')&&; changeInfo.status ==='loading'){
NewGmailURL('update',tab);
return;
//一切正常,现在移除监听器。如何?
}
});
}
});
});
}
// [...]

感谢您的帮助!



有关详情,请使用URL参数显示警告对话框。


$ b

  //修改Gmail URL 
var updated = false;
功能NewGmailURL(PARAM,ZTAB){
如果(PARAM === '更新'){
urlMyExtension = ztab.url.replace(urlGmail + '查询=更新&安培;?',urlGmail +'?query ='+ param +'&');
} else {
urlMyExtension = ztab.url.replace(urlGmail,urlGmail +'?query ='+ param +'&');
}
if(!done&& param!=='updated'){
chrome.tabs.update(ztab.id,{
url:urlMyExtension,
突出显示:ztab.highlighted
},null);
updated = true;


[已更新]回答

如果有人遇到同样的问题,这里是@ExpertSystem的答案:

  //收听标签URL 
函数myListener(tabId,info,tab){
if(tab.url.match(mail.google。 com)&&(info.status ===loading)){
NewGmailURL(update,tab);
/ *现在,让我们解除我们的监听器职责* /
chrome.tabs.onUpdated.removeListener(myListener);
return;



//重新加载选项卡,其中的Gmail处于活动状态
函数reloadTab(order){
chrome.tabs.query({currentWindow:true },function(tabs){
var countGmailTabs = 0;
for(var i = 0; i< tabs.length; i ++){
var t = tabs [i] .url ;
if(t.match('mail.google.com')){
countGmailTabs + = 1;
if(order ==='set'){
chrome .tabs.reload(tabs [i] .id);
} else if(order!=='set'){
var GmailTab = tabs [i];
NewGmailURL(order, GmailTab);
return;
}
}
}
//找不到Gmail!
if(countGmailTabs< 1&& order! =='set'){
chrome.tabs.onUpdated.addListener(GmailListener);
}
});
}


解决方案

为了能够删除侦听器不要将其实现为匿名函数。将它实现为named函数,允许您在调用 removeListener()时引用它的改变。

例如:

  / *更改:* / 
chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab){...});
$ b $ * * /
函数myListener(tabId,info,tab){
if(tab.url.match(mail.google.com)& ;&(info.status ===loading)){
NewGmailURL(update,tab);
/ *现在,让我们解除我们的监听器职责* /
chrome.tabs.onUpdated.removeListener(myListener);
return;
}
});
chrome.tabs.onUpdated.addListener(myListener);






顺便说一句,(与您的问题无关,但)您正在进行一些超快的调用:

您不需要获取当前窗口,然后获取其标签,就像这样:

  chrome.windows.getCurrent(function(win){
var cwin = win.id;
chrome.tabs.query({windowId:cwin},函数(标签){
...

您可以在一次调用中执行此操作,如下所示:

(另请参阅 chrome.windows.getCurrent()

  chrome.window.getCurrent({populate:true},function(win){
var tabs = win.tabs;
...

甚至像这样:

(另请参见 chrome.tabs.query()

  chrome.tabs.query({currentWindow:true},function(tabs){
...


Here is my problem: I'm developing a Chrome extension for Gmail and needs to apply some changes when the extension is updated.

For example, I want to be sure that the extension will display an alert dialog within Gmail after the extension has been updated. This implies to check whether Gmail is already open in a Chrome window at the time of the update, and if not to create a listener in order to wait for Gmail to be loaded in the future and then to display the alert dialog.

The code below does just that. However, I cannot manage to remove the listener (chrome.tabs.onUpdated.removeListener?) when a new Gmail tab has been discovered.

background.js

     // [...] code before
    // Reload Tabs where Gmail is active
    function reloadTab(order) {
        chrome.windows.getCurrent(function(win) {
            var cwin = win.id;
            chrome.tabs.query({windowId: cwin}, function(tabs) {
                var countGmailTabs = 0;
                for (var i = 0; i < tabs.length; i++) {
                    var t = tabs[i].url;
                    if (t.match('mail.google.com')) {
                        countGmailTabs += 1;
                        if (order === 'set') {
                            chrome.tabs.reload(tabs[i].id);
                        } else if (order !== 'set') {
                            var GmailTab = tabs[i];
                            NewGmailURL(order, GmailTab);
                            return;
                        }
                    }
                }
                // Gmail not found !
                if (countGmailTabs < 1 && order !== 'set') {
                    chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
                        if ( tab.url.match('mail.google.com') && changeInfo.status === 'loading' ) {
                            NewGmailURL('update', tab);
                            return;
                            // Everything OK, now remove listener. How?
                        }
                    });
                }
            });
        });
    }
    // [...]

Thanks for your help!

For information, the alert dialog is displayed thanks to URL parameters.

// Modify Gmail URL
var updated = false;
    function NewGmailURL(param, ztab) {
        if (param === 'updated') {
            urlMyExtension = ztab.url.replace(urlGmail + '?query=update&', urlGmail +'?query=' + param + '&');
        } else {
            urlMyExtension = ztab.url.replace(urlGmail, urlGmail + '?query=' + param + '&');
        }
        if (!done && param !== 'updated') {
        chrome.tabs.update(ztab.id, {
            url: urlMyExtension,
            highlighted: ztab.highlighted
        }, null);
        updated = true;
        }
    }

[UPDATED] ANSWER

In case anyone meets the same problem, here is the answer thanks to @ExpertSystem:

// Listen to Tabs URL
    function myListener(tabId, info, tab) {
        if (tab.url.match("mail.google.com") && (info.status === "loading")) {
            NewGmailURL("update", tab);
            /* Now, let's relieve ourselves from our listener duties */
            chrome.tabs.onUpdated.removeListener(myListener);
            return;
        }
    }

// Reload Tabs where Gmail is active
function reloadTab(order) {
    chrome.tabs.query({ currentWindow: true }, function(tabs) {
            var countGmailTabs = 0;
            for (var i = 0; i < tabs.length; i++) {
                var t = tabs[i].url;
                if (t.match('mail.google.com')) {
                    countGmailTabs += 1;
                    if (order === 'set') {
                        chrome.tabs.reload(tabs[i].id);
                    } else if (order !== 'set') {
                        var GmailTab = tabs[i];
                        NewGmailURL(order, GmailTab);
                        return;
                    }
                }
            }
            // Gmail not found !
            if (countGmailTabs < 1 && order !== 'set') {
                chrome.tabs.onUpdated.addListener(GmailListener);
            }
    });
}

解决方案

In order to be able to remove a listener do not implement it as an anonymous function. Implementing it as "named" function, allows you to reference it alter when calling removeListener().
E.g.:

/* Change this: */
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {...});

/* To this: */
function myListener(tabId, info, tab) {
    if (tab.url.match("mail.google.com") && (info.status === "loading")) {
        NewGmailURL("update", tab);
        /* Now, let's relieve ourselves from our listener duties */
        chrome.tabs.onUpdated.removeListener(myListener);
        return;
    }
});
chrome.tabs.onUpdated.addListener(myListener);


BTW, (not related to your problem, but) you are making a couple of superflous calls:

You don't need to get the current window and then get its tabs, like this:

chrome.windows.getCurrent(function(win) {
    var cwin = win.id;
    chrome.tabs.query({windowId: cwin}, function(tabs) {
    ...

You can do it in one call, like this:
(See, also, chrome.windows.getCurrent())

chrome.window.getCurrent({ populate: true }, function(win) {
    var tabs = win.tabs;
    ...

Or even like this:
(See, also, chrome.tabs.query())

chrome.tabs.query({ currentWindow: true }, function(tabs) {
...

这篇关于在使用chrome.tabs.onUpdated.addListener后访问给定的URL时删除侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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