2

I have an extension that was originally made awhile back with manifest version 1. It uses a chrome.tabs.getSelected and chrome.tabs.getAllInWindow which were deprecated in Chrome 33. We're practically on Chrome 54 now, so I wanted to make it newer. The current permission in the manifest is tabs. I heard this may have to do with it being asynchronous.

The extension as it exists is a button that closes ALL tabs to the left of the selected or active tab.

function closeLeftTabs() { 
    var curTab;
    chrome.tabs.getSelected( null , function(tab) {  
        curTab=tab;
    });
    chrome.tabs.getAllInWindow(null,function(tabs) {
        for (var i = 0; i < tabs.length; i++) {
            if (tabs[i].index<curTab.index){
                chrome.tabs.remove(tabs[i].id, null);
            }
        }
    });
} 
chrome.browserAction.onClicked.addListener(function(tab) {    
      closeLeftTabs();
  });

I'm updating it to ignore pinned tabs (not close them) using the queryInfo parameter of chrome.tabs.query, but the JavaScript no longer works when pressing the button. Here's the idea I have so far.

function closeLeftTabs() { 
    var curTab;
    chrome.tabs.query({highlighted: true}, function(tab) {  
        curTab=tab;
    });
    chrome.tabs.query({pinned: false},function(tabs) {
        for (var i = 0; i < tabs.length; i++) {
            if (tabs[i].index<curTab.index){
                chrome.tabs.remove(tabs[i].id, null);
            }
        }
    });
} 
chrome.browserAction.onClicked.addListener(function(tab) {    
      closeLeftTabs();
  });

Clicking the button with the above code now seems to do nothing. I don't have much JavaScript experience and I haven't used any of the new parameters yet. I also experimented with currentWindow and lastFocusedWindow booleans and nothing changed, so I can't identify the root of the problem.

Erik Humphrey
  • 345
  • 5
  • 18
  • chrome.tabs.query is async as all chrome API with callbacks so `curTab` is undefined when the next statement is executed. See [How do I return the response from an asynchronous call?](https://stackoverflow.com/q/14220321) Don't forget that such errors can be caught in a few seconds using the awesome builtin devtools debugger by setting breakpoints inside the functions. – wOxxOm Sep 11 '16 at 20:48
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Haibara Ai Sep 11 '16 at 23:47

1 Answers1

2

To get the current active tab "active" property is used. Here is the working code.

chrome.browserAction.onClicked.addListener(function(tab) {  
   closeLeftTabs();
});


function closeLeftTabs() {

  performActionOnCurrentTab(function(activeTab){
     if(activeTab) {
        chrome.tabs.query({currentWindow : true,pinned: false},function(tabs) {
             for (var i = 0; i < tabs.length; i++) {
                 if (tabs[i].index < activeTab.index){
                     chrome.tabs.remove(tabs[i].id, null);
                 }
             }
         });
     }
 });


}

function performActionOnCurrentTab(tabCallback) {
   chrome.tabs.query({ currentWindow : true, active : true },function (tabArray) { tabCallback(tabArray[0]); });
}
yogesh kumar
  • 940
  • 6
  • 10
  • 2
    The code in the question operates on the `highlighted` tab, not the active tab (it is unclear if this is an intentional UI choice by the OP). For your listener, why do you wrap `closeLeftTabs()` in an anonymous function (you could just use it directly)? Why do you do a `chrome.tabs.query` to find the active tab when you are already passed the [Tab.tab](https://developer.chrome.com/extensions/tabs#type-Tab) for the active tab in the `chrome.browserAction.onClicked` handler (i.e. you already have it, why not use it)? – Makyen Sep 12 '16 at 15:50