防止window.open聚焦 [英] Prevent window.open from focusing

查看:145
本文介绍了防止window.open聚焦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用window.open()在Google Chrome中的新标签中打开一个页面,但我不希望该窗口在打开后获得焦点,而是停留在后台。

I want to open a page in a new tab in Google Chrome with window.open(), but I don't want that window to gain focus after it's opened, but to stay in the background.

这可能吗?它只需要在谷歌浏览器上工作。它还可以使用谷歌浏览器扩展API。

Is this possible? It only has to work on Google Chrome. It can also use the Google Chrome extension API.

谢谢

Thanks

推荐答案

正确的方法是使用扩展API:

The proper way would be to use extension API:

chrome.tabs.create({url: "http://...", selected: false});

代码应放置在后台页面中。如果您在内容脚本中需要它,您可以将消息传递到后台页面,如下所示:

Code should be placed in a background page. If you need it inside a content script you can pass a message to a background page, like so:

//content script
chrome.runtime.sendMessage({link: link});

//background page
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
    if(message.link) {
        chrome.tabs.create({url: link, selected: false});
    }
});

这篇关于防止window.open聚焦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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