我正在寻找一种通过网页上的代码更改选项卡(转到下一个打开的选项卡,这基本上就像按住 CTRL 键并按 TAB 键)的方法。我希望能够单击页面上的任意位置,它会转到下一个选项卡?我了解点击网页部分,只是不知道如何访问 chrome 标签页。
这可能吗?
谢谢,
最佳答案
是也不是。
如果您的意思是您可以在网站中使用 JavaScript 前进到下一个选项卡:如果您没有通过 window.open
打开其他选项卡(例如,尝试前进到另一个未打开的选项卡直接从当前网站),那么不,这是不可能的。如果可能的话,这将是一个安全风险,并为攻击者提供另一个向量来“识别”用户或可能找到一种方法来访问有关用户打开的其他选项卡的信息。
如果您确实打开了网站内的选项卡,您可以 focus对他们:
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 浏览器扩展程序,那么,是的,您可以使用 Tabs API 前进到下一个选项卡。 .注意:这可能行不通,我没有测试它,但似乎符合我所看到的文档和示例。如果您尝试找到更好的解决方案,请告诉我,我会更新):
// 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})
});
});
关于javascript - 使用 Javascript 更改 Chrome 选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46285381/