To open a link in a new tab with JavaScript in a Chrome extension, we call the chrome.tabs.create
method.
For instance, we write
{
//...
"background": {
"scripts": ["background.js"]
}
//...
}
in manifest.json to add the background.js script as a background script.
Then we write
chrome.browserAction.onClicked.addListener((activeTab) => {
const url = "http://example.com/";
chrome.tabs.create({ url });
});
in background.js to add a click listener for our Chrome extension with onClicked.addListener
.
In the listener, we call chrome.tabs.create
with { url }
to open a new tab that goes to the url
.