使用Java脚本更改Chrome标签页 [英] Change Chrome Tab with Javascript

查看:147
本文介绍了使用Java脚本更改Chrome标签页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种通过网页上的代码更改选项卡(转到下一个打开的选项卡的方法,本质上类似于按住CTRL并按TAB键).我希望能够单击页面上的任意位置,它将转到下一个选项卡?我了解点击网页部分,只是不了解如何访问Chrome浏览器标签.

I'm looking for a way to change tabs (got to the next open tab, which is essentially like holding CTRL and pressing the TAB key) through code on a webpage. I want to be able to click anywhere on the page and it will go to the next tab? I understand the clicking on the webpage part, just not how to access the chrome tabs.

这有可能吗?

谢谢

推荐答案

是,不是.

如果您是说可以在网站内使用JavaScript前进到下一个标签页:如果您没有通过window.open打开其他标签页(例如,尝试前进到不是直接从当前网站打开的其他标签页),那不行,那是不可能的.如果可能的话,这将带来安全隐患,并给攻击者提供另一个引导用户标识"用户的媒介,或者可能找到一种途径来获取对用户打开的其他选项卡的信息的访问权限.

If you mean can you advance to the next tab using JavaScript within a website: If you did not open the other tabs via window.open (e.g. trying to advance to another tab that was not opened directly from the current website), then no, it is not possible. If it were possible it would be a security risk and give attackers another vector to "ID" a user or potentially find a way to gain access to information about the other tabs a user has open.

如果您确实打开了网站内的标签,则可以 关注他们:

If you did open the tabs within the website, you can focus to them:

var newTabs = [];

newTabs.push( window.open("https://example.com", "_blank") );

// Add more tabs?

// Sorry, no way to figure out what the "next" tab is in the
// browser's list of tabs via straight on-site JavaScript,
// just go by next index in the array...
newTabs[0].focus();


如果您指的是您正在使用的Chrome浏览器扩展程序,那么可以,您可以使用标签API .注意:这可能不起作用,我没有对其进行测试,但似乎符合我所看到的文档和示例.如果您尝试找到更好的解决方案,请告诉我,我会进行更新):


If you are referring to Chrome browser extension that you are working on then, yes, you can advance to the next tab using the Tabs API. NOTE: This may not work, I did not test it but seems to fit with the documentation and examples I have seen. If you try and find a better solution let me know and I will update):

// Query current active tab in the current active window:
chrome.tabs.query({currentWindow: true, active: true}, function(tabsArray) {
    // If there are fewer than 2 tabs, you are on the only tab available.
    // Nothing left to do.
    if( tabsArray.length < 2 ) return;
    // Else query tab with incremented index (e.g. next tab):
    chrome.tabs.query({index: (tabsArray[0].index+1)}, function(nextTabsArray){
        // There is no next tab (only 1 tab or user is on last tab)
        if( nextTabsArray.length < 1 ) return;
        // Else, yay! There is a next tab, lets go!
        chrome.tabs.update(nextTabsArray[0].id, {active: true})
    });  
});

这篇关于使用Java脚本更改Chrome标签页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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