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.
2 Answers
Reset to default 7- Change
window
to another variable name. - Be consistent in variable names.
window_list
andlist
are different things. - Use
chrome.windows.update
instead ofwindow.focus()
, because the latter does not work. - Use
chrome.windows.get
to see whether the window exists, instead of maintaining a list of windows. - 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条)