简体   繁体   中英

chrome.tabs.create not working on Chrome startup

I have a simple Chrome extension, with just this little code inside background.js file:

chrome.tabs.create({}, function (tab) {
    console.log(tab.id);
});

When I install it through developer mode, it creates a new tab and writes its id to console.

But when I exit Chrome and start it again, nothing happens on Chrome startup.. I thought background scripts are loaded on each browser startup?

What is wrong with this and is there any other way to do this on browser startup?

Change your manifest and add background property:

"background": {
    "scripts": ["background.js"]
  },

Chrome will run this script "background.js" on browser startup every time(till your extension enabled). So just add you code to background.js script.

 //background.js content
    chrome.tabs.create({}, function (tab) {
        console.log(tab.id);
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM