I have a Chorme extension and I need to create a button that opens some Google searches. As google limits searches to 32 keywords, so I needed to create several links, for example:
https://www.google.com.br/search?q=test
https://www.google.com.br/search?q=test2
https://www.google.com.br/search?q=test3
When I click on the button I need it to open a new window (chrome.windows.create) with the first link. Already the second and third link open with tab, but within that same new window.
If I put everything in windows.create, everything opens in separate windows and that's not what I want. If I put 2 and 3 in the search (chrome.tabs.create), it will open 1 link in a new window and 2 links in 2 tabs, but from the active window, that's not what I want either. I want everything to open in a new window, with the rest of the links in tabs within this new window. Can anyone help me? Thanks.
You can use the chrome.tabs.create
method to open the first link in a new window, and set the "active
" property to true
. This will make the new tab the active tab in the new window. Then, you can use a loop to open the remaining links in new tabs within the same window by passing the window ID of the new window as the "windowId
" property in the chrome.tabs.create method for the remaining links.
chrome.windows.create({
url: "https://www.google.com.br/search?q=test",
focused: true
}, function(newWindow) {
chrome.tabs.create({
url: "https://www.google.com.br/search?q=test2",
windowId: newWindow.id
});
chrome.tabs.create({
url: "https://www.google.com.br/search?q=test3",
windowId: newWindow.id
});
});
You can use this script as a function, and call it when you click on the button.