javascript - Google Chrome Extensions: create a window only once - Stack Overflow

I'm opening a new window by clicking on the extension button near the search bar.I'd like to

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.

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.

Share Improve this question edited Mar 13, 2012 at 14:59 auino asked Mar 13, 2012 at 14:45 auinoauino 1,6565 gold badges23 silver badges44 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7
  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;
            });
    }
});
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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742297860a4417481.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信