Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google chrome 如何在当前选项卡之后打开新选项卡?_Google Chrome_Google Chrome Extension - Fatal编程技术网

Google 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

我正在开发一个chrome扩展,我想打开一个新的选项卡,但在当前选项卡之后,该用户处于打开状态。这就是我试图做的:

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
应该没问题