Google chrome 如何在当前选项卡之后打开新选项卡?
我正在开发一个chrome扩展,我想打开一个新的选项卡,但在当前选项卡之后,该用户处于打开状态。这就是我试图做的:Google chrome 如何在当前选项卡之后打开新选项卡?,google-chrome,google-chrome-extension,Google Chrome,Google Chrome Extension,我正在开发一个chrome扩展,我想打开一个新的选项卡,但在当前选项卡之后,该用户处于打开状态。这就是我试图做的: function clickEvent(info, tab) { update(); var url= "http://google.com"; var id = tab.id+1; chrome.tabs.create({'url': url, 'index': id}); } 但创建的选项卡将在chrome选项卡栏中的选项卡队列末尾打开。从ch
function clickEvent(info, tab) {
update();
var url= "http://google.com";
var id = tab.id+1;
chrome.tabs.create({'url': url, 'index': id});
}
但创建的选项卡将在chrome选项卡栏中的选项卡队列末尾打开。从
chrome.tabs.create
中删除“index”:id
后,结果相同。我不知道怎样才能解决这个问题。有人能帮我吗?由于您使用了错误的参数(id
应该是index
),该选项卡被附加在末尾。id
是一个正整数,它唯一地标识会话中的选项卡。因此,id
的值总是高于选项卡的数量
选项卡的位置可以从索引
属性中读取。因此,将id
替换为index
:
function clickEvent(info, tab) {
update();
var url = "http://google.com/";
var index = tab.index + 1;
chrome.tabs.create({'url': url, 'index': index});
}
听起来您正在创建一个“子”选项卡,在这种情况下,您应该同时设置
索引和OpenerTable
:
函数addChildTab(url,parentTab){
chrome.tabs.create({
“url”:url,
“windowId”:parentTab.windowId,
“索引”:parentTab.index+1,//n.b.索引不是id
“OpenerTab”:parentTab.id//n.b.id不是索引
});
}
设置openerTabId
意味着新选项卡将正确关联为父选项卡的子选项卡,因此:
- 如果在子选项卡处于活动状态时关闭子选项卡,则父选项卡将成为活动选项卡(而不是子选项卡右侧的选项卡)。这使其行为与用户在新选项卡中打开的链接相同
- 在树中显示选项卡的扩展
注意:如果您在后台打开选项卡(通过传递
active:false
),则parentTab.index+1
不太正确,理想情况下,您应该在parentTab
的现有子(和孙子)选项卡之后插入新选项卡:
函数addBackgroundChildTab(url,parentTab){
chrome.tabs.query({'windowId':parentTab.windowId},函数(tabs){
var parentandgroundntids={};
parentandsubgentids[parentTab.id]=true;
var nextIndex=parentTab.index+1;
while(nextIndex<制表符长度){
var tab=选项卡[nextIndex];
if(parentandsubcentids中的tab.openerTabId){
parentandsubgentids[tab.id]=true;
nextIndex++;
}否则{
打破
}
}
chrome.tabs.create({
“url”:url,
“活动”:false,
“windowId”:parentTab.windowId,
“索引”:nextIndex,
“OpenerTab”:parentTab.id
});
});
}
但对于您的目的来说,这可能有点过头了,在这种情况下,像我的第一个代码示例中那样坚持使用parentTab.index+1
应该没问题