简体   繁体   中英

Google Chrome Extensions: create a window only once

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.

  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM