Reputation: 33
What I would like to do is to open a new tab (chrome://newtab) everytime Chrome starts. My javascript code is working fine:
chrome.tabs.create({});
Everytime the script is executed the new tab opens, focuses and places the cursor in the address bar. The problem is, the code isn't always executed - only when no Chrome process was running before Chrome was started.
My second approach was to create an event listener so once executed chrome would know what to do when started. I've tried using this script:
chrome.windows.onCreated.addListener(function (Window window) {
chrome.tabs.create({});
});
But this didn't work at all.
My manifest looks like this:
{
"manifest_version": 2,
...
"background": {
"scripts": ["newtab.js"],
"persistent": false
}
}
... Therefore, what would be the correct way of realizing this?
Upvotes: 0
Views: 1137
Reputation: 20458
function (Window window) {
is invalid syntax.
chrome.windows.onCreated.addListener(function() {
chrome.tabs.create({})
})
will work instead.
However, this may not be what you want, as this will cause new tabs when new windows are created using Menu -> New window.
You can solve this by checking that the latest opened window is the only one.
chrome.windows.onCreated.addListener(function() {
chrome.windows.getAll(function(windows) {
if(windows.length == 1) {
chrome.tabs.create({})
} }) })`
Upvotes: 2