最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Closing Tab in Chrome Extension - Stack Overflow

programmeradmin2浏览0评论

I have a function called in popup.html that creates a tab, inserts a mailto to trigger a local (or gmail) mail event. It's my desire for it to then close itself. I've tried numerous things, but it seems like I need something that does the equivalent of:

tabId = chrome.tabs.query(I DON'T KNOW!);
chrome.tabs.remove(tabId);

here's the current code:

var query = { active: true, currentWindow: true };
function callback(tabs) {
    var currentTab = tabs[0];
    console.log(currentTab);
}
chrome.tabs.remove(chrome.tabs.query(query, callback));

but it's not working.

if useful, here's how I create the tab (which does work as desired):

    chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
        getTabs(tabs, function(full_mail_link){
          chrome.tabs.create({ url: full_mail_link });
        });
    });

any help would be greatly appreciated!

I have a function called in popup.html that creates a tab, inserts a mailto to trigger a local (or gmail) mail event. It's my desire for it to then close itself. I've tried numerous things, but it seems like I need something that does the equivalent of:

tabId = chrome.tabs.query(I DON'T KNOW!);
chrome.tabs.remove(tabId);

here's the current code:

var query = { active: true, currentWindow: true };
function callback(tabs) {
    var currentTab = tabs[0];
    console.log(currentTab);
}
chrome.tabs.remove(chrome.tabs.query(query, callback));

but it's not working.

if useful, here's how I create the tab (which does work as desired):

    chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
        getTabs(tabs, function(full_mail_link){
          chrome.tabs.create({ url: full_mail_link });
        });
    });

any help would be greatly appreciated!

Share Improve this question asked Dec 19, 2014 at 22:25 Jeremy ToemanJeremy Toeman 811 gold badge1 silver badge7 bronze badges 1
  • Close iself = close the popup? Close the tab you opened? Both? – Xan Commented Dec 20, 2014 at 12:04
Add a ment  | 

3 Answers 3

Reset to default 8

I don't know what your getTabs function does. Yet if you know how to find the tab id of the tab you want all you need to do is

chrome.tabs.remove(tabId, optionalCallback);

this must be work:

chrome.tabs.getSelected(null, function(tab) {
 chrome.tabs.remove(tab.id);
 });

This should work:

//create the tab
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
    getTabs(tabs, function(full_mail_link){
      chrome.tabs.create({ url: full_mail_link }, callBackOnCreate);
    });
});

function callBackOnCreate(tab)
{
     globalCreatedTab = tab.id;
}

chrome.tabs.query({'active': true}, function(tabs) {
      for (var i = 0; i < tabs.length; ++i)
      {
          if (tabs[i].id === globalCreatedTab)
          {
              chrome.tabs.remove(tabs[i].id, [optional callback]);
          }
      }

 });

Solution: use the query function with the callback and execute the remove function in the callback.

It looks like normal window.open and window.close() should also work, The tab-id is an integer or an array containing integers.

发布评论

评论列表(0)

  1. 暂无评论