5

I'm opening a new window by clicking on the extension button near the search bar. I'd like to open a new window only if it's not already opened; in that case, I'd prefer showing the old one.

Here is my code, but it doesn't work.

  var v = null;
  var vid = null;
  chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.windows.getAll({}, function(list) {
      // check if already exists
      for(window in window_list)
        if(window.id == vid) { window.focus(); return; }

      chrome.windows.getCurrent(function(w) {
        v = chrome.windows.create({'url': 'my_url', 'type': 'panel', 'focused': true});
        vid = w.id;
      });
    });
  });

Can someone explain me how to fix it?

Most probably, both v and vid values are deleted after closing the app (after it finish to execute the script), but how can I fix it? If possible, without using localStorage or cookies.

I've tried specifying the tabId properties while creating the window, but it doesn't work. I've also tried using the chrome.windows.onRemoved.addListener functionality, but it doesn't work too.

auino
  • 1,644
  • 5
  • 23
  • 43

2 Answers2

6
  1. Change window to another variable name.
  2. Be consistent in variable names. window_list and list are different things.
  3. Use chrome.windows.update instead of window.focus(), because the latter does not work.
  4. Use chrome.windows.get to see whether the window exists, instead of maintaining a list of windows.
  5. The details of the new window are available in the callback of chrome.windows.create. Use this method in the correct way:

Code:

chrome.windows.get(vid, function(chromeWindow) {
    if (!chrome.runtime.lastError && chromeWindow) {
        chrome.windows.update(vid, {focused: true});
        return;
    }
    chrome.windows.create(
        {'url': 'my_url', 'type': 'panel', 'focused': true},
        function(chromeWindow) {
            vid = chromeWindow.id;
        }
    );
});

Or, instead of checking whether the window exists, just update the window, and when an error occurs, open a new one:

chrome.windows.update(vid, {focused: true}, function() {
    if (chrome.runtime.lastError) {
        chrome.windows.create(
            {'url': 'my_url', 'type': 'panel', 'focused': true},
            function(chromeWindow) {
                vid = chromeWindow.id;
            });
    }
});
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • @auino Your whole code was broken, I have updated my answer, and expanded "// Rest of code" – Rob W Mar 13 '12 at 15:05
  • Hey. I got one bug in there. instead of for( in ) use simple for loop. It takes index number as id sometimes. for(j=0; j – Ganesh Bhosale Aug 29 '15 at 07:07
-1
chrome.windows.getAll({}, function(window_list) {
    var extWindow = '';
    window_list.forEach(function(chromeWindow) {
        //Check windows by type
        if (chromeWindow.type == 'panel') {
            extWindow = chromeWindow.id;
            //Update opened window
            chrome.windows.update(extWindow, {focused: true});
            return;
        }
    });

    if (extWindow == '') {
        //Open window
        chrome.windows.create(
            {
                'url'       : 'my_url',
                'type'      : 'panel',
                'focused'   : true
            },
            function(chromeWindow) {
                extWindow = chromeWindow.id;
            }
        );
    }
});

It is an alternative code that works for me

Nestek
  • 1
  • 1
  • Can you provide a better explanation of this fix? – Necreaux Apr 15 '15 at 12:56
  • It's best to explain your code, how it's different from "wrong" code in the question. Just a couple of comments is probably not enough. Especially since there already is a very similar accepted answer - how is yours different and why/when it's better? However, I disagree with the downvote - it's not useless. – Xan Apr 15 '15 at 12:58
  • Actually, I might reconsider. Unless a specific experimental flag is set, I don't think windows opened this way retain type `"panel"`. – Xan Apr 15 '15 at 13:24