javascript - 使用 Javascript 更改 Chrome 选项卡

标签 javascript html google-chrome

我正在寻找一种通过网页上的代码更改选项卡(转到下一个打开的选项卡,这基本上就像按住 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/

相关文章:

javascript - 在 jQuery 中使用 .then() 链接 promises/Deferreds

javascript - 条形图 D3.js 错误 : Invalid value for <rect> attribute

javascript - 货币的 JS Intl.NumberFormat 不能有 maximumFractionDigits 0

javascript - 将对象数组转换为数组列表(javascript)

javascript - 如何在 Node.js 中通用化 http 响应处理程序?

javascript - iframe动态src

html - 使用 meyers reset 后,我​​不知道如何让我的 ul 水平显示

JavaScript 变量 - HTML5 Canvas 问题

javascript - Google Chrome 扩展程序的 storage.sync 上出现 QUOTA_BYTES_PER_ITEM 错误

javascript - 调试繁重的单页应用程序