admin管理员组文章数量:1332896
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
本文标签: javascriptGoogle Chrome Extensions create a window only onceStack Overflow
版权声明:本文标题:javascript - Google Chrome Extensions: create a window only once - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742297860a2449089.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论