Chrome扩展程序在当前窗口后面打开新窗口 [英] Chrome extension open new window behind the current window

查看:1946
本文介绍了Chrome扩展程序在当前窗口后面打开新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用
window.open()自动打开窗口的扩展名。
当用户打开一些特定的网站。
我想要的是通过window.open()从后台脚本打开的新窗口应显示在用户打开的当前webiste后面。



我已经尝试window.open属性像alwaysLowered = 1,z锁= 1等,但不工作。
也试过.....

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

所有这些对chrome都没有影响。任何人都可以帮忙吗?

解决方案

如果您不需要打开窗口的句柄(由 window.open ),您可以使用 chrome.windows API 的方法 create
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});
});
});

从理论上讲,通过集中:false createInfo 参数中的c>属性应该一次达到相同的结果,但在Windows上使用Chrome 31.0.1650.57版本时不适用于我。




更新:

上面的代码似乎没有模糊MAC上的窗口。为了克服这个问题(在确定新窗口的位置和大小时 - 根据OP的评论)使用以下代码:

  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});
});
});


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?

解决方案

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 });
    });
});

这篇关于Chrome扩展程序在当前窗口后面打开新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆