在后台打开新标签,将焦点放在当前标签上 - Chrome

时间:2023-01-14 21:17:01

How to execute in JavaScript a CTRL + click on a link that works in the latest version of Chrome (v68)?

如何在JavaScript中执行CTRL +点击最新版Chrome(v68)中的链接?

Context - I'm running a JavaScript script that opens a certain tab at certain hours of the day (and closes it after a few minutes). I'm trying to get it to open the tab in background leaving the focus on the current tab that I'm using.

上下文 - 我正在运行一个JavaScript脚本,它在一天中的某些时间打开某个选项卡(并在几分钟后关闭它)。我正试图让它在后台打开选项卡,将焦点放在我正在使用的当前选项卡上。

The tab opened programmatically leads Chrome to pop up even when minimized, quite disrupting.

以编程方式打开的标签会导致Chrome弹出,即使在最小化时也会出现中断。

These old solutions that I found here on Stack Overflow don't work with the latest version of Chrome.

我在Stack Overflow上找到的这些旧解决方案无法与最新版本的Chrome一起使用。

Manually CTRL + clicking a link achieves the effect that I want (tab is opened in background). Can this be achieved programmatically on the latest version of Chrome?

手动CTRL +单击链接可实现我想要的效果(选项卡在后台打开)。这可以在最新版本的Chrome上以编程方式实现吗?


The following code does not work anymore with the latest version of Chrome..

以下代码不再适用于最新版本的Chrome ..

const openNewBackgroundTab = (url) => {
  const anchor = document.createElement("a");
  anchor.href = url;
  document.body.appendChild(anchor);
  const evt = document.createEvent("MouseEvents");    
  // the tenth parameter of initMouseEvent sets ctrl key
  evt.initMouseEvent(
    "click", true, true, window, 0, 0, 0, 0, 0,
    true, false, false, false, 0, null
  );
  anchor.dispatchEvent(evt);
}
openNewBackgroundTab('https://*.com');

.. the new tab still gets the focus.

..新标签仍然成为焦点。


STEPS to reproduce:

重现步骤:

  • Open window 1 and in console execute:
  • 打开窗口1并在控制台中执行:

let winStacko; setTimeout(() => { winStacko = open('https://www.*.com'); }, 30 * 1000); setTimeout(() => winStacko.close(), 2 * 60 * 1000);

让winStacko; setTimeout(()=> {winStacko = open('https://www.*.com');},30 * 1000); setTimeout(()=> winStacko.close(),2 * 60 * 1000);

  • Open window 2 within 30 seconds after executing the script
  • 执行脚本后30秒内打开窗口2

DESIRED behavior:

  • Window 2 has the focus for the whole time while the background tab is opened and then closed.
  • 窗口2在整个时间内具有焦点,而背景选项卡打开然后关闭。

4 个解决方案

#1


1  

As you want this for personal usage, and you do not mind using an extension, then you can write your own one.

由于您希望将其用于个人用途,并且您不介意使用扩展程序,因此您可以编写自己的扩展程序。

Writing a chrome extension to achieve your required behavior is actually super easy, All you need to know is how to open/close tabs from the extension. The following page describes the whole API

编写chrome扩展来实现所需的行为实际上非常简单,您只需知道如何打开/关闭扩展中的选项卡。以下页面描述了整个API

Here is an example :

这是一个例子:

1 - Create a manifest.json file, and ask for the tabs permission

1 - 创建manifest.json文件,并请求tabs权限

{
  "name": "blabla",
  "version": "0.1",
  "permissions": ["tabs"],
  "background": {
    "persistent": false,
    "scripts": ["background.js"]
  },
  "browser_action": {
    "default_title": "Open a background tab every x time"
  },
  "manifest_version": 2
}

2 - Create background.js script in the same folder

2 - 在同一文件夹中创建background.js脚本

const INTERVAL = 5000;
setTimeout(function(){
    chrome.tabs.create({url: "https://www.*.com", active: false }, tab =>{
        setTimeout(function(){
            chrome.tabs.remove(tab.id);
        },INTERVAL);
    }); 
},INTERVAL);

You can download it from here too

你也可以从这里下载

Note: Please note the active parameter here , it defines whether the tab should become the active tab in the window. Does not affect whether the window is focused

注意:请注意此处的active参数,它定义选项卡是否应成为窗口中的活动选项卡。不影响窗口是否聚焦

3 - Use the extension in chrome

3 - 使用chrome中的扩展名

  1. If you are using the download link, then unpack the archive
  2. 如果您使用的是下载链接,请解压缩存档

  3. Navigate from chrome menu to extensions
  4. 从chrome菜单导航到扩展程序

  5. Enable the developer mode in the top right corner
  6. 启用右上角的开发人员模式

  7. Click on Load Unpacked button to select the extension folder
  8. 单击“加载解压缩”按钮以选择扩展文件夹

  9. The extension will run automatically
  10. 扩展程序将自动运行

#2


2  

In the old days, you could do this using stuff like :

在过去,您可以使用以下内容执行此操作:

if(window.focus == false)
{
    this.focus();
}


Now you have to use extra plugins.
due to abusers.

现在你必须使用额外的插件。由于滥用者。

#3


1  

I PARTIALLY resolved using a Chrome plugin: Force Background Tab

我部分解决了使用Chrome插件:强制背景标签的问题

https://chrome.google.com/webstore/detail/force-background-tab/gidlfommnbibbmegmgajdbikelkdcmcl/related?hl=en

Using that add-on the tab is opened in background without focus. The only issue is that the last used tab of the window (can be different from the tab that launches the new tab) still pops up in front on any other application in use.

使用该加载项,选项卡在后台打开,没有焦点。唯一的问题是窗口的最后一个使用的选项卡(可以与启动新选项卡的选项卡不同)仍然在使用中的任何其他应用程序的前面弹出。

#4


0  

You can try a kind of pop under, where you open the same url in a new tab, then change the location.href of current tab to the desired new url.

您可以尝试使用某种弹出窗口,在新选项卡中打开相同的URL,然后将当前选项卡的location.href更改为所需的新URL。

window.open(window.location.href);
window.location.href = 'http://www.google.com';

#1


1  

As you want this for personal usage, and you do not mind using an extension, then you can write your own one.

由于您希望将其用于个人用途,并且您不介意使用扩展程序,因此您可以编写自己的扩展程序。

Writing a chrome extension to achieve your required behavior is actually super easy, All you need to know is how to open/close tabs from the extension. The following page describes the whole API

编写chrome扩展来实现所需的行为实际上非常简单,您只需知道如何打开/关闭扩展中的选项卡。以下页面描述了整个API

Here is an example :

这是一个例子:

1 - Create a manifest.json file, and ask for the tabs permission

1 - 创建manifest.json文件,并请求tabs权限

{
  "name": "blabla",
  "version": "0.1",
  "permissions": ["tabs"],
  "background": {
    "persistent": false,
    "scripts": ["background.js"]
  },
  "browser_action": {
    "default_title": "Open a background tab every x time"
  },
  "manifest_version": 2
}

2 - Create background.js script in the same folder

2 - 在同一文件夹中创建background.js脚本

const INTERVAL = 5000;
setTimeout(function(){
    chrome.tabs.create({url: "https://www.*.com", active: false }, tab =>{
        setTimeout(function(){
            chrome.tabs.remove(tab.id);
        },INTERVAL);
    }); 
},INTERVAL);

You can download it from here too

你也可以从这里下载

Note: Please note the active parameter here , it defines whether the tab should become the active tab in the window. Does not affect whether the window is focused

注意:请注意此处的active参数,它定义选项卡是否应成为窗口中的活动选项卡。不影响窗口是否聚焦

3 - Use the extension in chrome

3 - 使用chrome中的扩展名

  1. If you are using the download link, then unpack the archive
  2. 如果您使用的是下载链接,请解压缩存档

  3. Navigate from chrome menu to extensions
  4. 从chrome菜单导航到扩展程序

  5. Enable the developer mode in the top right corner
  6. 启用右上角的开发人员模式

  7. Click on Load Unpacked button to select the extension folder
  8. 单击“加载解压缩”按钮以选择扩展文件夹

  9. The extension will run automatically
  10. 扩展程序将自动运行

#2


2  

In the old days, you could do this using stuff like :

在过去,您可以使用以下内容执行此操作:

if(window.focus == false)
{
    this.focus();
}


Now you have to use extra plugins.
due to abusers.

现在你必须使用额外的插件。由于滥用者。

#3


1  

I PARTIALLY resolved using a Chrome plugin: Force Background Tab

我部分解决了使用Chrome插件:强制背景标签的问题

https://chrome.google.com/webstore/detail/force-background-tab/gidlfommnbibbmegmgajdbikelkdcmcl/related?hl=en

Using that add-on the tab is opened in background without focus. The only issue is that the last used tab of the window (can be different from the tab that launches the new tab) still pops up in front on any other application in use.

使用该加载项,选项卡在后台打开,没有焦点。唯一的问题是窗口的最后一个使用的选项卡(可以与启动新选项卡的选项卡不同)仍然在使用中的任何其他应用程序的前面弹出。

#4


0  

You can try a kind of pop under, where you open the same url in a new tab, then change the location.href of current tab to the desired new url.

您可以尝试使用某种弹出窗口,在新选项卡中打开相同的URL,然后将当前选项卡的location.href更改为所需的新URL。

window.open(window.location.href);
window.location.href = 'http://www.google.com';