我试图在popup.js中调用chrome.tabs.create回调函数,以便可以更改标签的标题。根据另一个StackOverflow问题(chrome extension: callback function not getting called),我需要将代码放在background.js中才能使用回调函数。
但是,我已经尝试过了,但是它不起作用。即使我没有在background.js中使用回调函数,也不会创建该选项卡。
编辑:更新的代码
/* opens tabs if button is clicked */
groupButton.onclick = function()
{
/* opens the tabs */
for (var j = 0; j < group["tabCount" + i]; j++)
{
// cannot use callback function because popup is immediately closed upon
// tab creation, have to use background script
chrome.extension.getBackgroundPage().createTab(group, i, j);
// used to work in popup.js with no callback function
// chrome.tabs.create({"url": group["tabUrls" + i][j], "active": false});
}
}
function createTab(group, i, j)
{
chrome.tabs.create({"url": group["tabUrls" + i][j], "active": false}, function(tab)
{
for (var k = 0; k < group["tabCount" + i]; k++)
{
var tabTitle = group["tabNames" + i][k];
console.log("create tab title: " + tabTitle);
chrome.runtime.sendMessage(tab.id, {getTitle: tabTitle}, function(response){});
}
})
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse)
{
if (request.getTitle)
{
document.title = request.getTitle;
}
})