Chrome extension open new window behind the current window

梦想的初衷 提交于 2019-12-24 16:08:14

问题


i have a chrome extension which automatically open a window using window.open(); when user open some specific websites. what i want is the new window which i open from the background script through window.open() should be displayed behind the current webiste opened by the user.

i have tried window.open properties like alwaysLowered=1, z-lock=1 etc. but not working. Also tried.....

var w = window.open('url');
if(w){
w.blur();
window.focus();
}

all these have no effect on chrome. can anybody help?


回答1:


If you don't need the handle to the opened window (returned by window.open), you can use the chrome.windows API's methods create and update:

In background.js:

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.windows.create({ url: "http://www.google.com/" }, function(win) {
        chrome.windows.update(win.id, { focused: false });
    });
});

Theoretically, passing the focused: false property in the createInfo argument should achieve the same result in one step, but it is not working for me with Chrome version 31.0.1650.57 on Windows.


UPDATE:
The above code seems to not "blur" the window on MACs. To overcome this (while determining the position and size of the new window - as per OP's comment) use the following code:

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.windows.create({ 
        url: "http://www.google.com/",
        width:  430,
        height: 150,
        top:    top_popup,
        left:   left_popup
    }, function(win) {
        chrome.windows.update(tab.windowId, { focused: true });
    });
});



回答2:


chrome.windows.getCurrent(function(winCurrent){
        chrome.windows.create({url:url, function(win){
            chrome.windows.update(winCurrent.id, {focused: true});
        });
    });

Try this, its fast so viewer doesn't feel much



来源:https://stackoverflow.com/questions/20399798/chrome-extension-open-new-window-behind-the-current-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!